fosrl.pangolin/server/index.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

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";
2024-09-28 12:56:36 -04:00
import internal from "@server/routers/internal";
import external from "@server/routers/external";
2024-09-27 21:39:03 -04:00
const dev = environment.ENVIRONMENT !== "prod";
const app = next({ dev });
const handle = app.getRequestHandler();
2024-09-29 21:09:35 -04:00
const mainPort = environment.EXTERNAL_PORT;
const internalPort = environment.INTERNAL_PORT;
2024-09-28 11:59:13 -04:00
2024-09-27 21:39:03 -04:00
app.prepare().then(() => {
2024-09-28 12:56:36 -04:00
// Main server
const mainServer = express();
mainServer.use(helmet());
mainServer.use(cors());
2024-09-27 21:39:03 -04:00
2024-09-29 14:37:26 -04:00
const prefix = `/api/v1`;
2024-09-28 12:56:36 -04:00
mainServer.use(prefix, express.json(), external);
2024-09-28 12:42:38 -04:00
// We are using NEXT from here on
2024-09-28 12:56:36 -04:00
mainServer.all("*", (req: Request, res: Response) => {
2024-09-27 21:39:03 -04:00
const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl);
});
2024-09-28 12:56:36 -04:00
mainServer.listen(mainPort, (err?: any) => {
if (err) throw err;
logger.info(`Main server is running on http://localhost:${mainPort}`);
});
// Internal server
const internalServer = express();
internalServer.use(helmet());
internalServer.use(cors());
internalServer.use(prefix, express.json(), internal);
internalServer.listen(internalPort, (err?: any) => {
if (err) throw err;
2024-09-28 13:40:24 -04:00
logger.info(
`Internal server is running on http://localhost:${internalPort}`,
);
2024-09-27 21:39:03 -04:00
});
});
2024-09-28 11:59:13 -04:00
2024-09-28 13:40:24 -04:00
process.on("SIGINT", () => {
2024-09-28 11:59:13 -04:00
process.exit(0);
2024-09-28 13:40:24 -04:00
});