feat: integrate custom logger and envalidated envs

This commit is contained in:
Ritesh Ghosh
2025-05-11 13:08:12 +05:30
parent e098e7cfcb
commit 83076b672b
+28 -35
View File
@@ -1,40 +1,36 @@
import https from "https"; import https from "https";
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 { Hono } from "hono"; import { Hono } from "hono";
import { logger } from "hono/logger";
import { serve } from "@hono/node-server"; import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import pkgJson from "../package.json" with { type: "json" }; import { env } from "./config/env.js";
import { logger } from "./config/logger.js";
import { corsConfig } from "./config/cors.js";
import { ratelimit } from "./config/ratelimit.js";
import { errorHandler, notFoundHandler } from "./config/errorHandler.js"; import { errorHandler, notFoundHandler } from "./config/errorHandler.js";
import type { AniwatchAPIVariables } from "./config/variables.js"; import type { AniwatchAPIVariables } from "./config/variables.js";
config(); import { hianimeRouter } from "./routes/hianime.js";
import { logging } from "./middleware/logging.js";
import { cacheConfigSetter, cacheControl } from "./middleware/cache.js";
import pkgJson from "../package.json" with { type: "json" };
//
const BASE_PATH = "/api/v2" as const; 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<{ Variables: AniwatchAPIVariables }>(); const app = new Hono<{ Variables: AniwatchAPIVariables }>();
app.use(logger()); app.use(logging);
app.use(corsConfig); app.use(corsConfig);
app.use(cacheControlMiddleware); app.use(cacheControl);
//
// CAUTION: For personal deployments, "refrain" from having an env // CAUTION: For personal deployments, "refrain" from having an env
// named "ANIWATCH_API_HOSTNAME". You may face rate limitting // named "ANIWATCH_API_HOSTNAME". You may face rate limitting
// or other issues if you do. // or other issues if you do.
const ISNT_PERSONAL_DEPLOYMENT = Boolean(ANIWATCH_API_HOSTNAME); const ISNT_PERSONAL_DEPLOYMENT = Boolean(env.ANIWATCH_API_HOSTNAME);
if (ISNT_PERSONAL_DEPLOYMENT) { if (ISNT_PERSONAL_DEPLOYMENT) {
app.use(ratelimit); app.use(ratelimit);
} }
@@ -43,7 +39,8 @@ app.use("/", serveStatic({ root: "public" }));
app.get("/health", (c) => c.text("daijoubu", { status: 200 })); app.get("/health", (c) => c.text("daijoubu", { status: 200 }));
app.get("/v", async (c) => app.get("/v", async (c) =>
c.text( c.text(
`v${"version" in pkgJson && pkgJson?.version ? pkgJson.version : "-1"}` `aniwatch-api: v${"version" in pkgJson && pkgJson?.version ? pkgJson.version : "-1"}\n` +
`aniwatch-package: v${"dependencies" in pkgJson && pkgJson?.dependencies?.aniwatch ? pkgJson?.dependencies?.aniwatch : "-1"}`
) )
); );
@@ -57,18 +54,17 @@ app.basePath(BASE_PATH).get("/anicrush", (c) =>
app.notFound(notFoundHandler); app.notFound(notFoundHandler);
app.onError(errorHandler); app.onError(errorHandler);
//
// NOTE: this env is "required" for vercel deployments // NOTE: this env is "required" for vercel deployments
if (!Boolean(process.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) { if (!env.ANIWATCH_API_VERCEL_DEPLOYMENT) {
serve({ serve({
port: PORT, port: env.ANIWATCH_API_PORT,
fetch: app.fetch, fetch: app.fetch,
}).addListener("listening", () => }).addListener("listening", () => {
console.info( logger.info(
"\x1b[1;36m" + `aniwatch-api RUNNING at http://localhost:${env.ANIWATCH_API_PORT}`
`aniwatch-api at http://localhost:${PORT}` + );
"\x1b[0m" });
)
);
// NOTE: remove the `if` block below for personal deployments // NOTE: remove the `if` block below for personal deployments
if (ISNT_PERSONAL_DEPLOYMENT) { if (ISNT_PERSONAL_DEPLOYMENT) {
@@ -76,15 +72,12 @@ if (!Boolean(process.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) {
// don't sleep // don't sleep
setInterval(() => { setInterval(() => {
console.log( logger.info(
"aniwatch-api HEALTH_CHECK at", `aniwatch-api HEALTH_CHECK at ${new Date().toISOString()}`
new Date().toISOString()
); );
https https
.get(`https://${ANIWATCH_API_HOSTNAME}/health`) .get(`https://${env.ANIWATCH_API_HOSTNAME}/health`)
.on("error", (err) => { .on("error", (err) => logger.error(err.message.trim()));
console.error(err.message);
});
}, interval); }, interval);
} }
} }