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";
|
2024-07-18 21:35:17 +03:00
|
|
|
import sessionParser from "./routes/session_parser.js";
|
|
|
|
import utils from "./services/utils.js";
|
2024-07-18 23:24:36 +03:00
|
|
|
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";
|
2024-07-19 00:47:09 +03:00
|
|
|
import sql_init from "./services/sql_init.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");
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2024-07-19 00:18:35 +03:00
|
|
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2024-07-19 00:47:09 +03:00
|
|
|
// Initialize DB
|
|
|
|
sql_init.initializeDb();
|
|
|
|
|
2025-03-08 22:03:45 +00:00
|
|
|
// Initialize embedding providers
|
|
|
|
const { initializeEmbeddings } = await import("./services/llm/embeddings/init.js");
|
|
|
|
await initializeEmbeddings();
|
|
|
|
|
2025-03-11 23:26:47 +00:00
|
|
|
// Initialize the index service for LLM functionality
|
|
|
|
const { default: indexService } = await import("./services/llm/index_service.js");
|
|
|
|
await indexService.initialize().catch(e => console.error("Failed to initialize index service:", e));
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
// view engine setup
|
2025-01-09 18:07:02 +02:00
|
|
|
app.set("views", path.join(scriptDir, "views"));
|
|
|
|
app.set("view engine", "ejs");
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2024-09-08 18:12:16 +03:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.locals.t = t;
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2025-01-22 18:57:06 +01:00
|
|
|
if (!utils.isElectron) {
|
2022-08-22 19:29:58 +02:00
|
|
|
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 }));
|
2017-10-14 23:31:44 -04:00
|
|
|
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")));
|
2017-11-30 23:50:42 -05:00
|
|
|
app.use(sessionParser);
|
2024-08-05 18:47:57 +02:00
|
|
|
app.use(favicon(`${scriptDir}/../images/app-icons/icon.ico`));
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2024-12-19 18:16:46 +02:00
|
|
|
await assets.register(app);
|
2024-07-18 23:24:36 +03:00
|
|
|
routes.register(app);
|
|
|
|
custom.register(app);
|
|
|
|
error_handlers.register(app);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-10-28 12:23:11 -04:00
|
|
|
// triggers sync timer
|
2024-07-24 20:33:35 +03:00
|
|
|
await import("./services/sync.js");
|
2017-10-21 21:10:33 -04:00
|
|
|
|
2017-10-28 12:23:11 -04:00
|
|
|
// triggers backup timer
|
2025-01-09 18:07:02 +02:00
|
|
|
await import("./services/backup.js");
|
2017-10-28 12:23:11 -04:00
|
|
|
|
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");
|
2018-03-02 20:56:58 -05:00
|
|
|
|
2024-07-19 00:34:37 +03:00
|
|
|
startScheduledCleanup();
|
|
|
|
|
2025-01-22 18:57:06 +01:00
|
|
|
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
|
|
|
|
2024-07-18 21:50:12 +03:00
|
|
|
export default app;
|