Notes/src/app.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-07-18 21:55:26 +03:00
import express from "express";
import path from "path";
import favicon from "serve-favicon";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import compression from "compression";
2024-07-19 00:18:35 +03:00
import { fileURLToPath } from "url";
import { dirname } from "path";
import sessionParser from "./routes/session_parser.js";
2025-03-26 00:50:37 +01:00
import checkAuthState from "./routes/auth_check.js";
import utils from "./services/utils.js";
import assets from "./routes/assets.js";
import routes from "./routes/routes.js";
import custom from "./routes/custom.js";
import error_handlers from "./routes/error_handlers.js";
2024-07-19 00:34:37 +03:00
import { startScheduledCleanup } from "./services/erase.js";
import sql_init from "./services/sql_init.js";
import totp from "./services/totp.js";
2024-09-07 10:21:41 -07:00
import oidc from "express-openid-connect";
import openID from "./services/open_id.js";
2024-09-08 18:12:16 +03:00
import { t } from "i18next";
2023-05-07 15:23:46 +02:00
2025-01-09 18:07:02 +02:00
await import("./services/handlers.js");
await import("./becca/becca_loader.js");
const app = express();
2024-07-19 00:18:35 +03:00
const scriptDir = dirname(fileURLToPath(import.meta.url));
2024-09-07 11:41:54 -07:00
// Initialize DB
sql_init.initializeDb();
// view engine setup
2025-01-09 18:07:02 +02:00
app.set("views", path.join(scriptDir, "views"));
app.set("view engine", "ejs");
2024-09-08 18:12:16 +03:00
app.use((req, res, next) => {
res.locals.t = t;
return next();
});
if (!utils.isElectron) {
app.use(compression()); // HTTP compression
}
2022-08-20 17:30:35 -04:00
2025-01-09 18:07:02 +02:00
app.use(
helmet({
hidePoweredBy: false, // errors out in electron
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false
})
);
app.use(express.text({ limit: "500mb" }));
app.use(express.json({ limit: "500mb" }));
app.use(express.raw({ limit: "500mb" }));
2024-04-03 20:47:41 +03:00
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
2025-01-09 18:07:02 +02:00
app.use(express.static(path.join(scriptDir, "public/root")));
app.use(`/manifest.webmanifest`, express.static(path.join(scriptDir, "public/manifest.webmanifest")));
app.use(`/robots.txt`, express.static(path.join(scriptDir, "public/robots.txt")));
app.use(`/icon.png`, express.static(path.join(scriptDir, "public/icon.png")));
app.use(sessionParser);
2025-03-26 00:50:37 +01:00
app.use(checkAuthState);
2024-08-05 18:47:57 +02:00
app.use(favicon(`${scriptDir}/../images/app-icons/icon.ico`));
2025-03-25 23:53:49 +01:00
// Check if TOTP is enabled and validate TOTP secret is set
totp.isTotpEnabled();
if (openID.checkOpenIDRequirements())
2024-09-07 10:21:41 -07:00
app.use(oidc.auth(openID.generateOAuthConfig()));
await assets.register(app);
routes.register(app);
custom.register(app);
error_handlers.register(app);
// triggers sync timer
await import("./services/sync.js");
2017-10-21 21:10:33 -04:00
// triggers backup timer
2025-01-09 18:07:02 +02:00
await import("./services/backup.js");
2017-12-14 22:16:26 -05:00
// trigger consistency checks timer
2025-01-09 18:07:02 +02:00
await import("./services/consistency_checks.js");
2017-12-14 22:16:26 -05:00
2025-01-09 18:07:02 +02:00
await import("./services/scheduler.js");
2024-07-19 00:34:37 +03:00
startScheduledCleanup();
if (utils.isElectron) {
2025-01-09 18:07:02 +02:00
(await import("@electron/remote/main/index.js")).initialize();
2021-11-18 21:35:23 +01:00
}
2021-11-16 22:43:08 +01:00
export default app;