mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-16 17:05:04 +02:00
don't inject db into req
This commit is contained in:
parent
06ee87ac2c
commit
497b8a223f
4 changed files with 11 additions and 30 deletions
|
@ -1,6 +1,6 @@
|
|||
ENVIRONMENT=dev
|
||||
LOG_LEVEL=debug
|
||||
SAVE_LOGS=
|
||||
SAVE_LOGS=false
|
||||
PORT=3000
|
||||
INTERNAL_PORT=3001
|
||||
CONFIG_PATH=./config
|
||||
|
|
|
@ -18,14 +18,14 @@ const environment = {
|
|||
PORT: (process.env.PORT as string) || "3000",
|
||||
INTERNAL_PORT: (process.env.INTERNAL_PORT as string) || "3001",
|
||||
CONFIG_PATH: process.env.CONFIG_PATH as string,
|
||||
API_VERSION: process.env.API_VERSION as string,
|
||||
API_VERSION: (process.env.API_VERSION as string) || "v1",
|
||||
};
|
||||
|
||||
const parsedConfig = environmentSchema.safeParse(environment);
|
||||
|
||||
if (!parsedConfig.success) {
|
||||
const errors = fromError(parsedConfig.error);
|
||||
throw new Error(`Invalid environment configuration: ${errors}`);
|
||||
const errors = fromError(parsedConfig.error);
|
||||
throw new Error(`Invalid environment configuration: ${errors}`);
|
||||
}
|
||||
|
||||
export default parsedConfig.data;
|
||||
|
|
|
@ -7,33 +7,19 @@ import helmet from "helmet";
|
|||
import cors from "cors";
|
||||
import internal from "@server/routers/internal";
|
||||
import external from "@server/routers/external";
|
||||
import Database from 'better-sqlite3';
|
||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||
|
||||
const dev = environment.ENVIRONMENT !== "prod";
|
||||
const app = next({ dev });
|
||||
const handle = app.getRequestHandler();
|
||||
const mainPort = environment.PORT;
|
||||
const internalPort = environment.INTERNAL_PORT;
|
||||
let db: Database.Database;
|
||||
|
||||
app.prepare().then(() => {
|
||||
// Open the SQLite database connection
|
||||
// const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
||||
const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`);
|
||||
const db = drizzle(sqlite);
|
||||
|
||||
// Main server
|
||||
const mainServer = express();
|
||||
mainServer.use(helmet());
|
||||
mainServer.use(cors());
|
||||
|
||||
// Middleware to attach the database to the request
|
||||
mainServer.use((req, res, next) => {
|
||||
(req as any).db = db;
|
||||
next();
|
||||
});
|
||||
|
||||
const prefix = `/api/${environment.API_VERSION}`;
|
||||
mainServer.use(prefix, express.json(), external);
|
||||
|
||||
|
@ -53,21 +39,16 @@ app.prepare().then(() => {
|
|||
internalServer.use(helmet());
|
||||
internalServer.use(cors());
|
||||
|
||||
// Middleware to attach the database to the request
|
||||
internalServer.use((req, res, next) => {
|
||||
(req as any).db = db;
|
||||
next();
|
||||
});
|
||||
|
||||
internalServer.use(prefix, express.json(), internal);
|
||||
|
||||
internalServer.listen(internalPort, (err?: any) => {
|
||||
if (err) throw err;
|
||||
logger.info(`Internal server is running on http://localhost:${internalPort}`);
|
||||
logger.info(
|
||||
`Internal server is running on http://localhost:${internalPort}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
db.close();
|
||||
process.on("SIGINT", () => {
|
||||
process.exit(0);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue