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-28 12:42:38 -04:00
|
|
|
import { drizzle } from 'drizzle-orm/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
|
2024-09-28 12:42:38 -04:00
|
|
|
const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
|
|
|
const db = drizzle(sqlite);
|
|
|
|
|
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 12:42:38 -04:00
|
|
|
// Run migrations (if you're using Drizzle's migration system)
|
|
|
|
// migrate(db, { migrationsFolder: './drizzle' });
|
|
|
|
|
|
|
|
// Middleware to attach the database to the request
|
|
|
|
server.use((req, res, next) => {
|
|
|
|
(req as any).db = db;
|
|
|
|
next();
|
2024-09-28 11:59:13 -04:00
|
|
|
});
|
2024-09-28 12:42:38 -04:00
|
|
|
|
|
|
|
const prefix = `/api/${environment.API_VERSION}`;
|
2024-09-28 11:59:13 -04:00
|
|
|
|
2024-09-27 21:39:03 -04:00
|
|
|
server.use(prefix, express.json(), unauth);
|
|
|
|
|
2024-09-28 12:42:38 -04:00
|
|
|
|
|
|
|
// We are using NEXT from here on
|
2024-09-27 21:39:03 -04:00
|
|
|
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);
|
|
|
|
});
|