don't inject db into req

This commit is contained in:
Milo Schwartz 2024-09-28 13:40:24 -04:00
parent 06ee87ac2c
commit 497b8a223f
No known key found for this signature in database
4 changed files with 11 additions and 30 deletions

View file

@ -1,7 +1,7 @@
ENVIRONMENT=dev
LOG_LEVEL=debug
SAVE_LOGS=
SAVE_LOGS=false
PORT=3000
INTERNAL_PORT=3001
CONFIG_PATH=./config
API_VERSION=v1
API_VERSION=v1

View file

@ -67,4 +67,4 @@ export type Site = InferSelectModel<typeof sites>;
export type Resource = InferSelectModel<typeof resources>;
export type ExitNode = InferSelectModel<typeof exitNodes>;
export type Route = InferSelectModel<typeof routes>;
export type Target = InferSelectModel<typeof targets>;
export type Target = InferSelectModel<typeof targets>;

View file

@ -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;

View file

@ -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);
});
});