mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-31 06:59:33 +02:00
Add db to express server
This commit is contained in:
parent
b7bc797a99
commit
c498312ed3
2 changed files with 22 additions and 4 deletions
|
@ -7,6 +7,7 @@ const environmentSchema = z.object({
|
||||||
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
||||||
PORT: z.string(),
|
PORT: z.string(),
|
||||||
CONFIG_PATH: z.string(),
|
CONFIG_PATH: z.string(),
|
||||||
|
API_VERSION: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const environment = {
|
const environment = {
|
||||||
|
@ -15,6 +16,7 @@ const environment = {
|
||||||
SAVE_LOGS: (process.env.SAVE_LOGS as string) || "false",
|
SAVE_LOGS: (process.env.SAVE_LOGS as string) || "false",
|
||||||
PORT: (process.env.PORT as string) || "3000",
|
PORT: (process.env.PORT as string) || "3000",
|
||||||
CONFIG_PATH: process.env.CONFIG_PATH as string,
|
CONFIG_PATH: process.env.CONFIG_PATH as string,
|
||||||
|
API_VERSION: process.env.API_VERSION as string,
|
||||||
};
|
};
|
||||||
|
|
||||||
const parsedConfig = environmentSchema.safeParse(environment);
|
const parsedConfig = environmentSchema.safeParse(environment);
|
||||||
|
|
|
@ -6,20 +6,31 @@ import logger from "@server/logger";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import unauth from "@server/routers/unauth";
|
import unauth from "@server/routers/unauth";
|
||||||
|
import Database from 'better-sqlite3';
|
||||||
|
|
||||||
const dev = environment.ENVIRONMENT !== "prod";
|
const dev = environment.ENVIRONMENT !== "prod";
|
||||||
const app = next({ dev });
|
const app = next({ dev });
|
||||||
const handle = app.getRequestHandler();
|
const handle = app.getRequestHandler();
|
||||||
|
|
||||||
const port = environment.PORT;
|
const port = environment.PORT;
|
||||||
|
|
||||||
app.prepare().then(() => {
|
let db: Database.Database;
|
||||||
const server = express();
|
|
||||||
|
|
||||||
|
app.prepare().then(() => {
|
||||||
|
// Open the SQLite database connection
|
||||||
|
db = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
||||||
|
|
||||||
|
const server = express();
|
||||||
server.use(helmet());
|
server.use(helmet());
|
||||||
server.use(cors());
|
server.use(cors());
|
||||||
|
|
||||||
const prefix = `/api`;
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
server.use(prefix, express.json(), unauth);
|
server.use(prefix, express.json(), unauth);
|
||||||
|
|
||||||
server.all("*", (req: Request, res: Response) => {
|
server.all("*", (req: Request, res: Response) => {
|
||||||
|
@ -34,3 +45,8 @@ app.prepare().then(() => {
|
||||||
logger.info(`Server is running on http://localhost:${port}`);
|
logger.info(`Server is running on http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.on('SIGINT', () => {
|
||||||
|
db.close();
|
||||||
|
process.exit(0);
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue