diff options
| author | Ritesh Ghosh <[email protected]> | 2024-12-24 17:10:14 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2024-12-24 17:10:14 +0530 |
| commit | da6699ba224451e0d1a1d2cac569d314ea9c29b3 (patch) | |
| tree | 273a22fcae1255930f161e11846a4111876769ac /src | |
| parent | da19fb7e869c7a9f4007b8bf1439c08e5fe684f7 (diff) | |
| download | aniwatch-api-da6699ba224451e0d1a1d2cac569d314ea9c29b3.tar.xz aniwatch-api-da6699ba224451e0d1a1d2cac569d314ea9c29b3.zip | |
feat: use refactored error handlers and cache middlewares
Diffstat (limited to 'src')
| -rw-r--r-- | src/server.ts | 41 |
1 files changed, 9 insertions, 32 deletions
diff --git a/src/server.ts b/src/server.ts index 85d3fbc..acc064a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,17 +4,19 @@ import { config } from "dotenv"; import corsConfig from "./config/cors.js"; import { ratelimit } from "./config/ratelimit.js"; +import { + cacheConfigSetter, + cacheControlMiddleware, +} from "./middleware/cache.js"; import { hianimeRouter } from "./routes/hianime.js"; -import cacheControlMiddleware from "./config/cacheControlMiddleware.js"; 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 pkgJson from "../package.json" with { type: "json" }; +import { errorHandler, notFoundHandler } from "./config/errorHandler.js"; import type { AniwatchAPIVariables } from "./config/variables.js"; config(); @@ -38,47 +40,22 @@ if (ISNT_PERSONAL_DEPLOYMENT) { } app.use("/", serveStatic({ root: "public" })); -app.get("/health", (c) => c.text("OK", { status: 200 })); +app.get("/health", (c) => c.text("daijoubu", { status: 200 })); app.get("/v", async (c) => c.text( `v${"version" in pkgJson && pkgJson?.version ? pkgJson.version : "-1"}` ) ); -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.use(cacheConfigSetter(BASE_PATH.length)); app.basePath(BASE_PATH).route("/hianime", hianimeRouter); app .basePath(BASE_PATH) .get("/anicrush", (c) => c.text("Anicrush could be implemented in future.")); -app.notFound((c) => - c.json({ status: 404, message: "Resource Not Found" }, 404) -); - -app.onError((err, c) => { - console.error(err); - const res = { status: 500, message: "Internal Server Error" }; - - if (err instanceof HiAnimeError) { - res.status = err.status; - res.message = err.message; - } - - return c.json(res, { status: res.status }); -}); +app.notFound(notFoundHandler); +app.onError(errorHandler); // NOTE: this env is "required" for vercel deployments if (!Boolean(process.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) { |
