Skip to content

Commit

Permalink
feat(cache): add middleware to insert CACHE_CONFIG variable in requ…
Browse files Browse the repository at this point in the history
…est context
  • Loading branch information
ghoshRitesh12 committed Dec 7, 2024
1 parent 5cd99fc commit da83048
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import { ratelimit } from "./config/ratelimit.js";

import { hianimeRouter } from "./routes/hianime.js";

import { serveStatic } from "@hono/node-server/serve-static";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { logger } from "hono/logger";
import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";

import { HiAnimeError } from "aniwatch";
import { AniwatchAPICache } from "./config/cache.js";
import type { AniwatchAPIVariables } from "./config/variables.js";

config();

const BASE_PATH = "/api/v2" as const;
const PORT: number = Number(process.env.ANIWATCH_API_PORT) || 4000;
const ANIWATCH_API_HOSTNAME = process.env?.ANIWATCH_API_HOSTNAME;

const app = new Hono();
const app = new Hono<{ Variables: AniwatchAPIVariables }>();

app.use(logger());
app.use(corsConfig);
Expand All @@ -35,6 +37,20 @@ if (ISNT_PERSONAL_DEPLOYMENT) {
app.use("/", serveStatic({ root: "public" }));
app.get("/health", (c) => c.text("OK", { status: 200 }));

app.use(async (c, next) => {
const { pathname, search } = new URL(c.req.url);

c.set("CACHE_CONFIG", {
key: `${pathname.slice(BASE_PATH.length) + search}`,
duration: Number(
c.req.header(AniwatchAPICache.CACHE_EXPIRY_HEADER_NAME) ||
AniwatchAPICache.DEFAULT_CACHE_EXPIRY_SECONDS
),
});

await next();
});

app.basePath(BASE_PATH).route("/hianime", hianimeRouter);
app
.basePath(BASE_PATH)
Expand All @@ -57,7 +73,7 @@ app.onError((err, c) => {
});

// NOTE: this env is "required" for vercel deployments
if (!Boolean(process?.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) {
if (!Boolean(process.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) {
serve({
port: PORT,
fetch: app.fetch,
Expand Down

0 comments on commit da83048

Please sign in to comment.