2024-09-27 21:39:03 -04:00
|
|
|
import express, { Request, Response } from "express";
|
|
|
|
import next from "next";
|
|
|
|
import { parse } from "url";
|
2024-09-27 21:49:52 -04:00
|
|
|
import environment from "@server/environment";
|
|
|
|
import logger from "@server/logger";
|
2024-09-27 21:39:03 -04:00
|
|
|
import helmet from "helmet";
|
|
|
|
import cors from "cors";
|
|
|
|
import unauth from "@server/routers/unauth";
|
2024-09-28 11:59:13 -04:00
|
|
|
import Database from 'better-sqlite3';
|
2024-09-27 21:39:03 -04:00
|
|
|
|
|
|
|
const dev = environment.ENVIRONMENT !== "prod";
|
|
|
|
const app = next({ dev });
|
|
|
|
const handle = app.getRequestHandler();
|
|
|
|
const port = environment.PORT;
|
|
|
|
|
2024-09-28 11:59:13 -04:00
|
|
|
let db: Database.Database;
|
|
|
|
|
2024-09-27 21:39:03 -04:00
|
|
|
app.prepare().then(() => {
|
2024-09-28 11:59:13 -04:00
|
|
|
// Open the SQLite database connection
|
|
|
|
db = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
2024-09-27 21:39:03 -04:00
|
|
|
|
2024-09-28 11:59:13 -04:00
|
|
|
const server = express();
|
2024-09-27 21:39:03 -04:00
|
|
|
server.use(helmet());
|
|
|
|
server.use(cors());
|
|
|
|
|
2024-09-28 11:59:13 -04:00
|
|
|
const prefix = `/api/${environment.API_VERSION}`;
|
|
|
|
|
|
|
|
// Middleware to attach db to req object
|
|
|
|
server.use((req: Request & { db?: Database.Database }, res: Response, next) => {
|
|
|
|
req.db = db;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2024-09-27 21:39:03 -04:00
|
|
|
server.use(prefix, express.json(), unauth);
|
|
|
|
|
|
|
|
server.all("*", (req: Request, res: Response) => {
|
|
|
|
const parsedUrl = parse(req.url!, true);
|
|
|
|
handle(req, res, parsedUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port, (err?: any) => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
logger.info(`Server is running on http://localhost:${port}`);
|
|
|
|
});
|
|
|
|
});
|
2024-09-28 11:59:13 -04:00
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
db.close();
|
|
|
|
process.exit(0);
|
|
|
|
});
|