2024-08-11 18:41:03 +03:30
|
|
|
const env = require("./env");
|
|
|
|
|
|
|
|
const cookieParser = require("cookie-parser");
|
2024-09-08 14:10:02 +03:30
|
|
|
const passport = require("passport");
|
2024-08-11 18:41:03 +03:30
|
|
|
const express = require("express");
|
|
|
|
const helmet = require("helmet");
|
2025-01-21 09:38:14 +01:00
|
|
|
const path = require("node:path");
|
2024-08-11 18:41:03 +03:30
|
|
|
const hbs = require("hbs");
|
|
|
|
|
2024-08-21 21:22:59 +03:30
|
|
|
const helpers = require("./handlers/helpers.handler");
|
2024-09-14 15:27:50 +03:30
|
|
|
const renders = require("./handlers/renders.handler");
|
2024-09-09 18:43:12 +03:30
|
|
|
const asyncHandler = require("./utils/asyncHandler");
|
|
|
|
const locals = require("./handlers/locals.handler");
|
2024-09-08 14:10:02 +03:30
|
|
|
const links = require("./handlers/links.handler");
|
2024-08-11 18:41:03 +03:30
|
|
|
const routes = require("./routes");
|
|
|
|
const utils = require("./utils");
|
|
|
|
|
2024-11-23 14:44:33 +03:30
|
|
|
|
|
|
|
// run the cron jobs
|
|
|
|
// the app might be running in cluster mode (multiple instances) so run the cron job only on one cluster (the first one)
|
|
|
|
// NODE_APP_INSTANCE variable is added by pm2 automatically, if you're using something else to cluster your app, then make sure to set this variable
|
|
|
|
if (env.NODE_APP_INSTANCE === 0) {
|
|
|
|
require("./cron");
|
|
|
|
}
|
|
|
|
|
|
|
|
// intialize passport authentication library
|
2024-08-11 18:41:03 +03:30
|
|
|
require("./passport");
|
|
|
|
|
2024-09-09 18:43:12 +03:30
|
|
|
// create express app
|
2024-08-11 18:41:03 +03:30
|
|
|
const app = express();
|
|
|
|
|
2025-01-15 15:35:30 +03:30
|
|
|
// this tells the express app that it's running behind a proxy server
|
2024-11-21 16:20:17 +03:30
|
|
|
// and thus it should get the IP address from the proxy server
|
2025-01-15 15:35:30 +03:30
|
|
|
if (env.TRUST_PROXY) {
|
|
|
|
app.set("trust proxy", true);
|
|
|
|
}
|
2024-08-11 18:41:03 +03:30
|
|
|
|
|
|
|
app.use(helmet({ contentSecurityPolicy: false }));
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2025-01-15 11:45:53 +03:30
|
|
|
|
|
|
|
// serve static
|
|
|
|
app.use("/images", express.static("custom/images"));
|
|
|
|
app.use("/css", express.static("custom/css", { extensions: ["css"] }));
|
2024-08-11 18:41:03 +03:30
|
|
|
app.use(express.static("static"));
|
|
|
|
|
2024-09-08 14:10:02 +03:30
|
|
|
app.use(passport.initialize());
|
2024-09-09 18:43:12 +03:30
|
|
|
app.use(locals.isHTML);
|
2024-09-12 14:26:39 +03:30
|
|
|
app.use(locals.config);
|
2024-08-11 18:41:03 +03:30
|
|
|
|
|
|
|
// template engine / serve html
|
2025-01-15 11:45:53 +03:30
|
|
|
|
2024-08-11 18:41:03 +03:30
|
|
|
app.set("view engine", "hbs");
|
2025-01-15 11:45:53 +03:30
|
|
|
app.set("views", [
|
|
|
|
path.join(__dirname, "../custom/views"),
|
|
|
|
path.join(__dirname, "views"),
|
|
|
|
]);
|
2024-08-21 21:22:59 +03:30
|
|
|
utils.registerHandlebarsHelpers();
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2024-09-08 14:10:02 +03:30
|
|
|
// if is custom domain, redirect to the set homepage
|
|
|
|
app.use(asyncHandler(links.redirectCustomDomainHomepage));
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2025-01-01 12:47:04 +03:30
|
|
|
// render html pages
|
|
|
|
app.use("/", routes.render);
|
|
|
|
|
2024-09-12 14:26:39 +03:30
|
|
|
// handle api requests
|
2024-09-08 14:10:02 +03:30
|
|
|
app.use("/api/v2", routes.api);
|
|
|
|
app.use("/api", routes.api);
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2024-09-08 14:10:02 +03:30
|
|
|
// finally, redirect the short link to the target
|
|
|
|
app.get("/:id", asyncHandler(links.redirect));
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2024-09-14 15:27:50 +03:30
|
|
|
// 404 pages that don't exist
|
|
|
|
app.get("*", renders.notFound);
|
|
|
|
|
2024-09-12 14:26:39 +03:30
|
|
|
// handle errors coming from above routes
|
2024-08-11 18:41:03 +03:30
|
|
|
app.use(helpers.error);
|
|
|
|
|
|
|
|
app.listen(env.PORT, () => {
|
|
|
|
console.log(`> Ready on http://localhost:${env.PORT}`);
|
|
|
|
});
|