Aniwatch API Version 2 (#66)
BREAKING CHANGE: * chore: remove files that are not necessary for api v2 * test: update existing tests to use pkg * feat: organized aniwatch api envs and add more info about them * feat: update tsconfig to include strict noUnsed params * feat(api homepage): revamp api home page * feat: update wani kuni image * feat: add dot img * feat: use hono cors * feat: use hono rate limiter * build: remove unnecessary deps, add ones needed and update description * feat: add hianime routes and their handlers * feat: update vercel deployment file * docs: update logo and scraper docs, add envs section * feat: update main server file * feat: update peronal deployments caution section
This commit is contained in:
+53
-27
@@ -1,59 +1,85 @@
|
||||
import https from "https";
|
||||
import morgan from "morgan";
|
||||
import express from "express";
|
||||
import { resolve } from "path";
|
||||
import { config } from "dotenv";
|
||||
|
||||
import corsConfig from "./config/cors.js";
|
||||
import { ratelimit } from "./config/ratelimit.js";
|
||||
import errorHandler from "./config/errorHandler.js";
|
||||
import notFoundHandler from "./config/notFoundHandler.js";
|
||||
|
||||
import animeRouter from "./routes/index.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 { HiAnimeError } from "aniwatch";
|
||||
|
||||
config();
|
||||
const app: express.Application = express();
|
||||
const PORT: number = Number(process.env.PORT) || 4000;
|
||||
|
||||
app.use(morgan("dev"));
|
||||
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();
|
||||
|
||||
app.use(logger());
|
||||
app.use(corsConfig);
|
||||
|
||||
// CAUTION: For personal deployments, "refrain" from having an env
|
||||
// named "ANIWATCH_API_HOSTNAME". You may face rate limitting
|
||||
// and other issues if you do.
|
||||
const ISNT_PERSONAL_DEPLOYMENT = Boolean(process?.env?.ANIWATCH_API_HOSTNAME);
|
||||
// or other issues if you do.
|
||||
const ISNT_PERSONAL_DEPLOYMENT = Boolean(ANIWATCH_API_HOSTNAME);
|
||||
if (ISNT_PERSONAL_DEPLOYMENT) {
|
||||
app.use(ratelimit);
|
||||
}
|
||||
|
||||
app.use(express.static(resolve("public")));
|
||||
app.get("/health", (_, res) => res.sendStatus(200));
|
||||
app.use("/anime", animeRouter);
|
||||
app.use("/", serveStatic({ root: "public" }));
|
||||
app.get("/health", (c) => c.text("OK", { status: 200 }));
|
||||
|
||||
app.use(notFoundHandler);
|
||||
app.use(errorHandler);
|
||||
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 });
|
||||
});
|
||||
|
||||
// NOTE: this env is "required" for vercel deployments
|
||||
if (!Boolean(process?.env?.IS_VERCEL_DEPLOYMENT)) {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`⚔️ api @ http://localhost:${PORT}`);
|
||||
});
|
||||
if (!Boolean(process?.env?.ANIWATCH_API_VERCEL_DEPLOYMENT)) {
|
||||
serve({
|
||||
port: PORT,
|
||||
fetch: app.fetch,
|
||||
}).addListener("listening", () =>
|
||||
console.info(
|
||||
"\x1b[1;36m" + `aniwatch-api at http://localhost:${PORT}` + "\x1b[0m"
|
||||
)
|
||||
);
|
||||
|
||||
// NOTE: remove the `if` block below for personal deployments
|
||||
if (ISNT_PERSONAL_DEPLOYMENT) {
|
||||
const interval = 9 * 60 * 1000; // 9mins
|
||||
|
||||
// don't sleep
|
||||
const intervalTime = 9 * 60 * 1000; // 9mins
|
||||
setInterval(() => {
|
||||
console.log("HEALTHCHECK ;)", new Date().toLocaleString());
|
||||
console.log("aniwatch-api HEALTH_CHECK at", new Date().toISOString());
|
||||
https
|
||||
.get(
|
||||
new URL("/health", `https://${process.env.ANIWATCH_API_HOSTNAME}`)
|
||||
.href
|
||||
)
|
||||
.get(`https://${ANIWATCH_API_HOSTNAME}/health`)
|
||||
.on("error", (err) => {
|
||||
console.error(err.message);
|
||||
});
|
||||
}, intervalTime);
|
||||
}, interval);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user