File size: 1,756 Bytes
11acfd9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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";
config();
const app: express.Application = express();
const PORT: number = Number(process.env.PORT) || 4000;
app.use(morgan("dev"));
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);
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(notFoundHandler);
app.use(errorHandler);
// NOTE: this env is "required" for vercel deployments
if (!Boolean(process?.env?.IS_VERCEL_DEPLOYMENT)) {
app.listen(PORT, () => {
console.log(`⚔️ api @ http://localhost:${PORT}`);
});
// NOTE: remove the `if` block below for personal deployments
if (ISNT_PERSONAL_DEPLOYMENT) {
// don't sleep
const intervalTime = 9 * 60 * 1000; // 9mins
setInterval(() => {
console.log("HEALTHCHECK ;)", new Date().toLocaleString());
https
.get(
new URL("/health", `https://${process.env.ANIWATCH_API_HOSTNAME}`)
.href
)
.on("error", (err) => {
console.error(err.message);
});
}, intervalTime);
}
}
export default app;
|