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,7 +1,7 @@
|
||||||
ENVIRONMENT=dev
|
ENVIRONMENT=dev
|
||||||
LOG_LEVEL=debug
|
LOG_LEVEL=debug
|
||||||
SAVE_LOGS=
|
SAVE_LOGS=false
|
||||||
PORT=3000
|
PORT=3000
|
||||||
INTERNAL_PORT=3001
|
INTERNAL_PORT=3001
|
||||||
CONFIG_PATH=./config
|
CONFIG_PATH=./config
|
||||||
API_VERSION=v1
|
API_VERSION=v1
|
||||||
|
|
|
@ -67,4 +67,4 @@ export type Site = InferSelectModel<typeof sites>;
|
||||||
export type Resource = InferSelectModel<typeof resources>;
|
export type Resource = InferSelectModel<typeof resources>;
|
||||||
export type ExitNode = InferSelectModel<typeof exitNodes>;
|
export type ExitNode = InferSelectModel<typeof exitNodes>;
|
||||||
export type Route = InferSelectModel<typeof routes>;
|
export type Route = InferSelectModel<typeof routes>;
|
||||||
export type Target = InferSelectModel<typeof targets>;
|
export type Target = InferSelectModel<typeof targets>;
|
||||||
|
|
|
@ -18,14 +18,14 @@ const environment = {
|
||||||
PORT: (process.env.PORT as string) || "3000",
|
PORT: (process.env.PORT as string) || "3000",
|
||||||
INTERNAL_PORT: (process.env.INTERNAL_PORT as string) || "3001",
|
INTERNAL_PORT: (process.env.INTERNAL_PORT as string) || "3001",
|
||||||
CONFIG_PATH: process.env.CONFIG_PATH as string,
|
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);
|
const parsedConfig = environmentSchema.safeParse(environment);
|
||||||
|
|
||||||
if (!parsedConfig.success) {
|
if (!parsedConfig.success) {
|
||||||
const errors = fromError(parsedConfig.error);
|
const errors = fromError(parsedConfig.error);
|
||||||
throw new Error(`Invalid environment configuration: ${errors}`);
|
throw new Error(`Invalid environment configuration: ${errors}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default parsedConfig.data;
|
export default parsedConfig.data;
|
||||||
|
|
|
@ -7,33 +7,19 @@ import helmet from "helmet";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import internal from "@server/routers/internal";
|
import internal from "@server/routers/internal";
|
||||||
import external from "@server/routers/external";
|
import external from "@server/routers/external";
|
||||||
import Database from 'better-sqlite3';
|
|
||||||
import { drizzle } from 'drizzle-orm/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 mainPort = environment.PORT;
|
const mainPort = environment.PORT;
|
||||||
const internalPort = environment.INTERNAL_PORT;
|
const internalPort = environment.INTERNAL_PORT;
|
||||||
let db: Database.Database;
|
|
||||||
|
|
||||||
app.prepare().then(() => {
|
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
|
// Main server
|
||||||
const mainServer = express();
|
const mainServer = express();
|
||||||
mainServer.use(helmet());
|
mainServer.use(helmet());
|
||||||
mainServer.use(cors());
|
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}`;
|
const prefix = `/api/${environment.API_VERSION}`;
|
||||||
mainServer.use(prefix, express.json(), external);
|
mainServer.use(prefix, express.json(), external);
|
||||||
|
|
||||||
|
@ -53,21 +39,16 @@ app.prepare().then(() => {
|
||||||
internalServer.use(helmet());
|
internalServer.use(helmet());
|
||||||
internalServer.use(cors());
|
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.use(prefix, express.json(), internal);
|
||||||
|
|
||||||
internalServer.listen(internalPort, (err?: any) => {
|
internalServer.listen(internalPort, (err?: any) => {
|
||||||
if (err) throw err;
|
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', () => {
|
process.on("SIGINT", () => {
|
||||||
db.close();
|
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue