feat: integrate use of ebvalidated envs

This commit is contained in:
Ritesh Ghosh
2025-05-11 13:07:52 +05:30
parent 2054785565
commit e098e7cfcb
+21 -24
View File
@@ -1,35 +1,32 @@
import { config } from "dotenv";
import { AniwatchAPICache } from "../config/cache.js";
import type { MiddlewareHandler } from "hono"; import type { MiddlewareHandler } from "hono";
import { env } from "../config/env.js";
config(); import { AniwatchAPICache } from "../config/cache.js";
// Define middleware to add Cache-Control header // Define middleware to add Cache-Control header
export const cacheControlMiddleware: MiddlewareHandler = async (c, next) => { export const cacheControl: MiddlewareHandler = async (c, next) => {
const sMaxAge = process.env.ANIWATCH_API_S_MAXAGE || "60"; const sMaxAge = env.ANIWATCH_API_S_MAXAGE;
const staleWhileRevalidate = const staleWhileRevalidate = env.ANIWATCH_API_STALE_WHILE_REVALIDATE;
process.env.ANIWATCH_API_STALE_WHILE_REVALIDATE || "30";
c.header( c.header(
"Cache-Control", "Cache-Control",
`s-maxage=${sMaxAge}, stale-while-revalidate=${staleWhileRevalidate}` `s-maxage=${sMaxAge}, stale-while-revalidate=${staleWhileRevalidate}`
); );
await next(); await next();
}; };
export function cacheConfigSetter(keySliceIndex: number): MiddlewareHandler { export function cacheConfigSetter(keySliceIndex: number): MiddlewareHandler {
return async (c, next) => { return async (c, next) => {
const { pathname, search } = new URL(c.req.url); const { pathname, search } = new URL(c.req.url);
c.set("CACHE_CONFIG", { c.set("CACHE_CONFIG", {
key: `${pathname.slice(keySliceIndex) + search}`, key: `${pathname.slice(keySliceIndex) + search}`,
duration: Number( duration: Number(
c.req.header(AniwatchAPICache.CACHE_EXPIRY_HEADER_NAME) || c.req.header(AniwatchAPICache.CACHE_EXPIRY_HEADER_NAME) ||
AniwatchAPICache.DEFAULT_CACHE_EXPIRY_SECONDS AniwatchAPICache.DEFAULT_CACHE_EXPIRY_SECONDS
), ),
}); });
await next(); await next();
}; };
} }