first commit

This commit is contained in:
creations 2024-12-28 09:13:34 -05:00
commit d69d3b7033
Signed by: creations
GPG key ID: 8F553AA4320FC711
21 changed files with 1142 additions and 0 deletions

146
src/routes/[booru]/[id].ts Normal file
View file

@ -0,0 +1,146 @@
import { determineBooru } from "@helpers/char";
import { fetch } from "bun";
import { redis } from "@/database/redis";
const routeDef: RouteDef = {
method: "GET",
accepts: "*/*",
returns: "application/json",
};
async function handler(
request: Request,
server: BunServer,
requestBody: unknown,
query: Query,
params: Params,
): Promise<Response> {
const { booru, id } = params as { booru: string; id: string };
if (!booru || !id) {
return Response.json(
{
success: false,
code: 400,
error: "Missing booru or id",
},
{
status: 400,
},
);
}
const booruConfig: IBooruConfig | null = determineBooru(booru);
if (!booruConfig) {
return Response.json(
{
success: false,
code: 404,
error: "Booru not found",
},
{
status: 404,
},
);
}
if (!booruConfig.enabled) {
return Response.json(
{
success: false,
code: 403,
error: "Booru is disabled",
},
{
status: 403,
},
);
}
const funcString: string | [string, string] = booruConfig.functions.id;
let url: string = `https://${booruConfig.endpoint}/${booruConfig.functions.id}${id}`;
if (Array.isArray(funcString)) {
const [start, end] = funcString;
url = `https://${booruConfig.endpoint}/${start}${id}${end}`;
}
const cacheKey: string = `${booru}:${id}`;
const cacheData: unknown = await redis.getInstance().get("JSON", cacheKey);
if (cacheData) {
return Response.json(
{
success: true,
code: 200,
cache: true,
data: cacheData,
},
{
status: 200,
},
);
}
try {
const response: Response = await fetch(url);
if (!response.ok) {
return Response.json(
{
success: false,
code: response.status || 500,
error: response.statusText || "Could not reach booru",
},
{
status: response.status || 500,
},
);
}
const data: unknown = await response.json();
if (!data) {
return Response.json(
{
success: false,
code: 404,
error: "Post not found",
},
{
status: 404,
},
);
}
// let keyString = Array.isArray(data) ? "posts" : "post";
return Response.json(
{
success: true,
code: 200,
cache: false,
data,
},
{
status: 200,
},
);
} catch {
return Response.json(
{
success: false,
code: 500,
error: "Internal Server Error",
},
{
status: 500,
},
);
}
}
export { handler, routeDef };

15
src/routes/index.ts Normal file
View file

@ -0,0 +1,15 @@
const routeDef: RouteDef = {
method: "GET",
accepts: "*/*",
returns: "text/html",
};
async function handler(): Promise<Response> {
return new Response("Hello, World!", {
headers: {
"content-type": "text/html",
},
});
}
export { handler, routeDef };