Skip to content

Commit

Permalink
refactor, better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Jul 15, 2023
1 parent 192fb5b commit 4ab7133
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 109 deletions.
14 changes: 3 additions & 11 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "itty-router";
import { errorHandler } from "@/middleware/errorHandler";
import { responseHeaders } from "@/lib/responseHeaders";
import { getContributors } from "@/routes/discord/contributors";
import { index } from "@/routes/index";
import { index } from "@/routes";
import { getGenerator } from "@/routes/oc-generators/getGenerator";
import { getGenerators } from "@/routes/oc-generators/getGenerators";
import { getSearch, getRecentAssets } from "@/routes/search/search";
Expand All @@ -11,6 +11,7 @@ import { getUserByUsername } from "@/routes/user/getUserByUsername";
import { getUserBySearch } from "@/routes/user/getUsersBySearch";
import { allGames } from "@/routes/games/allGames";
import { getAssetFromId } from "@/routes/asset/getAssetFromId";
import { createNotFoundResponse } from "@/lib/helpers/responses/notFoundResponse";

const router = Router();

Expand All @@ -27,16 +28,7 @@ router
.get("/oc-generator/:gameId", errorHandler(getGenerator))
.get("/discord/contributors", errorHandler(getContributors))
.all("*", (): Response => {
return new Response(
JSON.stringify({
success: false,
status: "error",
error: "404 Not Found",
}),
{
headers: responseHeaders,
}
);
return createNotFoundResponse("Route doesn't exist", responseHeaders);
});

addEventListener("fetch", (event: FetchEvent) => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/helpers/getQueryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getQueryParam(url, param) {
const value = url.searchParams.get(param);
return value ? value.split(",") : [];
}
12 changes: 12 additions & 0 deletions src/lib/helpers/responses/notFoundResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function createNotFoundResponse(errorMessage, responseHeaders) {
const responseBody = {
success: false,
status: "error",
error: "404 Not Found",
message: errorMessage,
};

return new Response(JSON.stringify(responseBody), {
headers: responseHeaders,
});
}
1 change: 1 addition & 0 deletions src/lib/types/game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Game {
name: string;
id: number;
asset_categories?: string[];
}
4 changes: 2 additions & 2 deletions src/lib/types/ocGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Generator {
name: string;
data: string;
uploadedBy: number;
uploadedDate: string;
uploaded_by: number;
uploaded_date: string;
verified: string;
}
1 change: 1 addition & 0 deletions src/middleware/unwantedPrefixes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: deprecate this file as R2 listing buckets will no longer be used
export const unwantedPrefixes: string[] = [
"other/",
"locales/",
Expand Down
28 changes: 7 additions & 21 deletions src/routes/asset/getAssetFromId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { responseHeaders } from "@/lib/responseHeaders";
import type { Asset } from "@/lib/types/asset";
import { getConnection } from "@/lib/planetscale";
import { createNotFoundResponse } from "@/lib/helpers/responses/notFoundResponse";

export const getAssetFromId = async (
request: Request,
Expand All @@ -9,37 +10,22 @@ export const getAssetFromId = async (
const url = new URL(request.url);
const id = url.pathname.split("/")[2];

if (!id || isNaN(parseInt(id))) {
throw new Error("No ID provided");
}
if (!id || isNaN(parseInt(id))) throw new Error("No ID provided");

const cacheKey = new Request(url.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);

if (response) {
return response;
}
if (response) return response;

const db = await getConnection(env);

const row = await db
.execute("SELECT * FROM assets WHERE id = ?", [id])
.then((row) => row.rows[0] as Asset | undefined);

if (!row) {
return new Response(
JSON.stringify({
success: false,
status: "error",
error: "404 Not Found",
}),
{
headers: responseHeaders,
}
);
}
if (!row)
return createNotFoundResponse("Asset ID not found", responseHeaders);

const asset = {
id: row.id,
Expand All @@ -54,7 +40,7 @@ export const getAssetFromId = async (
file_size: row.file_size,
};

const similarAssets = await (
const similarAssets = (
await db
.execute(
"SELECT * FROM assets WHERE game = ? AND asset_category = ? AND id != ? ORDER BY RAND() LIMIT 4",
Expand Down Expand Up @@ -88,7 +74,7 @@ export const getAssetFromId = async (
}
);

response.headers.set("Cache-Control", "s-maxage=28800");
response.headers.set("Cache-Control", "s-maxage=604800");
await cache.put(cacheKey, response.clone());

return response;
Expand Down
21 changes: 4 additions & 17 deletions src/routes/download/downloadFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { responseHeaders } from "@/lib/responseHeaders";
import type { Asset } from "@/lib/types/asset";
import { getConnection } from "@/lib/planetscale";
import { createNotFoundResponse } from "@/lib/helpers/responses/notFoundResponse";

export const downloadFile = async (
request: Request,
Expand All @@ -9,30 +10,16 @@ export const downloadFile = async (
const url = new URL(request.url);
const id = url.pathname.split("/")[2];

if (!id || isNaN(parseInt(id))) {
throw new Error("No ID provided");
}
if (!id || isNaN(parseInt(id))) throw new Error("No ID provided");

const db = await getConnection(env);

const row = await db
.execute("SELECT * FROM assets WHERE id = ?", [id])
.then((row) => row.rows[0] as Asset | undefined);

console.log(row);

if (!row) {
return new Response(
JSON.stringify({
success: false,
status: "error",
error: "404 Not Found",
}),
{
headers: responseHeaders,
}
);
}
if (!row)
return createNotFoundResponse("Asset ID not found", responseHeaders);

const response = await fetch(`https://cdn.wanderer.moe/${row.url}`);
const headers = new Headers(response.headers);
Expand Down
8 changes: 3 additions & 5 deletions src/routes/games/allGames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ export const allGames = async (
env: Env
): Promise<Response> => {
const url = new URL(request.url);

const cacheKey = new Request(url.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);

if (response) {
return response;
}
if (response) return response;

const row: D1Result<Game> = await env.database
.prepare(`SELECT * FROM games`)
Expand Down Expand Up @@ -46,7 +44,7 @@ export const allGames = async (
}
);

response.headers.set("Cache-Control", "s-maxage=600");
response.headers.set("Cache-Control", "s-maxage=1200");
await cache.put(cacheKey, response.clone());

return response;
Expand Down
15 changes: 12 additions & 3 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { responseHeaders } from "@/lib/responseHeaders";

const routes: string[] = [
"https://api.wanderer.moe/search<?query=''&tags=''&after=''>",
"https://api.wanderer.moe/oc-generators",
"https://api.wanderer.moe/oc-generator/{gameId}",
"/search[?query=''&tags=''&game=''&asset='']",
"/user/[username]",
"/user/s/[search]",
"/recent",
"/asset/[assetId]",
"/download/[assetId]",
"/discord/contributors",
"/games",
"/oc-generators",
"/oc-generator/[gameId]",
];

export const index = async (): Promise<Response> => {
Expand All @@ -12,6 +19,8 @@ export const index = async (): Promise<Response> => {
success: true,
status: "ok",
path: "/",
description:
"Please read the API documentation on the site/github repository if you're unsure how to use this API.",
routes,
}),
{
Expand Down
13 changes: 6 additions & 7 deletions src/routes/oc-generators/getGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ export const getGenerator = async (
): Promise<Response> => {
const url = new URL(request.url);
const gameId = url.pathname.split("/")[2];

const cacheKey = new Request(url.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);

if (response) {
return response;
}
if (response) return response;

const row: D1Result<Generator> = await env.database
.prepare(`SELECT * FROM ocGenerators WHERE name = ?`)
.bind(gameId)
Expand All @@ -36,8 +35,8 @@ export const getGenerator = async (
const results = row.results.map((result) => ({
name: result.name,
data: JSON.parse(result.data),
uploadedBy: result.uploadedBy,
uploadedDate: result.uploadedDate,
uploaded_by: result.uploaded_by,
uploaded_date: result.uploaded_date,
verified: result.verified,
}));

Expand All @@ -52,7 +51,7 @@ export const getGenerator = async (
}
);

response.headers.set("Cache-Control", "s-maxage=28800");
response.headers.set("Cache-Control", "s-maxage=604800");
await cache.put(cacheKey, response.clone());

return response;
Expand Down
11 changes: 5 additions & 6 deletions src/routes/oc-generators/getGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ export const getGenerators = async (
env: Env
): Promise<Response> => {
const url = new URL(request.url);

const cacheKey = new Request(url.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);

if (response) {
return response;
}
if (response) return response;

const row: D1Result<Generator> = await env.database
.prepare(`SELECT * FROM ocGenerators`)
.run();

const results = row.results.map((result) => ({
name: result.name,
path: `/oc-generator/${result.name}`,
uploadedBy: result.uploadedBy,
uploadedDate: result.uploadedDate,
uploaded_by: result.uploaded_by,
uploaded_date: result.uploaded_date,
verified: result.verified,
}));

Expand Down
34 changes: 25 additions & 9 deletions src/routes/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ import { responseHeaders } from "@/lib/responseHeaders";
import type { Asset } from "@/lib/types/asset";
import * as queryLib from "@/lib/query";
import { getConnection } from "@/lib/planetscale";
import { getQueryParam } from "@/lib/helpers/getQueryParams";

export const getSearch = async (
request: Request,
env: Env
): Promise<Response> => {
const url = new URL(request.url);
const query = url.searchParams.get("query") || "";
const game = url.searchParams.get("game")?.split(",") || [];
const asset = url.searchParams.get("asset")?.split(",") || [];
const tags = url.searchParams.get("tags")?.split(",") || [];
const paramNames = ["query", "game", "asset", "tags"];

const results = await (
const params = {};
for (const paramName of paramNames) {
params[paramName] = getQueryParam(url, paramName);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { query, game, asset, tags } = params;

const cacheKey = new Request(url.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);

if (response) return response;

const results = (
await queryLib.getSearchResults(query, game, asset, tags, env)
).map((results) => {
return {
Expand All @@ -30,7 +44,7 @@ export const getSearch = async (
};
});

const response = new Response(
response = new Response(
JSON.stringify({
success: true,
status: "ok",
Expand All @@ -46,6 +60,9 @@ export const getSearch = async (
}
);

response.headers.set("Cache-Control", "s-maxage=3600");
await cache.put(cacheKey, response.clone());

return response;
};

Expand All @@ -59,9 +76,7 @@ export const getRecentAssets = async (
const cache = caches.default;
let response = await cache.match(cacheKey);

if (response) {
return response;
}
if (response) return response;

const db = await getConnection(env);

Expand Down Expand Up @@ -104,5 +119,6 @@ export const getRecentAssets = async (

response.headers.set("Cache-Control", "s-maxage=3600");
await cache.put(cacheKey, response.clone());

return response;
};
Loading

0 comments on commit 4ab7133

Please sign in to comment.