mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-19 08:48:34 +02:00
Add two servers
This commit is contained in:
parent
db3ce357df
commit
f8ed090a83
5 changed files with 60 additions and 25 deletions
7
.env-example
Normal file
7
.env-example
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
ENVIRONMENT=dev
|
||||||
|
LOG_LEVEL=debug
|
||||||
|
SAVE_LOGS=
|
||||||
|
PORT=3000
|
||||||
|
INTERNAL_PORT=3001
|
||||||
|
CONFIG_PATH=./config
|
||||||
|
API_VERSION=v1
|
|
@ -6,6 +6,7 @@ const environmentSchema = z.object({
|
||||||
LOG_LEVEL: z.string(),
|
LOG_LEVEL: z.string(),
|
||||||
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
||||||
PORT: z.string(),
|
PORT: z.string(),
|
||||||
|
INTERNAL_PORT: z.string(),
|
||||||
CONFIG_PATH: z.string(),
|
CONFIG_PATH: z.string(),
|
||||||
API_VERSION: z.string(),
|
API_VERSION: z.string(),
|
||||||
});
|
});
|
||||||
|
@ -15,6 +16,7 @@ const environment = {
|
||||||
LOG_LEVEL: (process.env.LOG_LEVEL as string) || "debug",
|
LOG_LEVEL: (process.env.LOG_LEVEL as string) || "debug",
|
||||||
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",
|
||||||
|
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,
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,15 +5,16 @@ import environment from "@server/environment";
|
||||||
import logger from "@server/logger";
|
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 internal from "@server/routers/internal";
|
||||||
|
import external from "@server/routers/external";
|
||||||
import Database from 'better-sqlite3';
|
import Database from 'better-sqlite3';
|
||||||
import { drizzle } from 'drizzle-orm/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 port = environment.PORT;
|
const mainPort = environment.PORT;
|
||||||
|
const internalPort = environment.INTERNAL_PORT;
|
||||||
let db: Database.Database;
|
let db: Database.Database;
|
||||||
|
|
||||||
app.prepare().then(() => {
|
app.prepare().then(() => {
|
||||||
|
@ -21,36 +22,47 @@ app.prepare().then(() => {
|
||||||
const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
|
||||||
const db = drizzle(sqlite);
|
const db = drizzle(sqlite);
|
||||||
|
|
||||||
|
// Main server
|
||||||
const server = express();
|
const mainServer = express();
|
||||||
server.use(helmet());
|
mainServer.use(helmet());
|
||||||
server.use(cors());
|
mainServer.use(cors());
|
||||||
|
|
||||||
// Run migrations (if you're using Drizzle's migration system)
|
|
||||||
// migrate(db, { migrationsFolder: './drizzle' });
|
|
||||||
|
|
||||||
// Middleware to attach the database to the request
|
// Middleware to attach the database to the request
|
||||||
server.use((req, res, next) => {
|
mainServer.use((req, res, next) => {
|
||||||
(req as any).db = db;
|
(req as any).db = db;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
const prefix = `/api/${environment.API_VERSION}`;
|
const prefix = `/api/${environment.API_VERSION}`;
|
||||||
|
mainServer.use(prefix, express.json(), external);
|
||||||
server.use(prefix, express.json(), unauth);
|
|
||||||
|
|
||||||
|
|
||||||
// We are using NEXT from here on
|
// We are using NEXT from here on
|
||||||
server.all("*", (req: Request, res: Response) => {
|
mainServer.all("*", (req: Request, res: Response) => {
|
||||||
const parsedUrl = parse(req.url!, true);
|
const parsedUrl = parse(req.url!, true);
|
||||||
handle(req, res, parsedUrl);
|
handle(req, res, parsedUrl);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(port, (err?: any) => {
|
mainServer.listen(mainPort, (err?: any) => {
|
||||||
if (err) {
|
if (err) throw err;
|
||||||
throw err;
|
logger.info(`Main server is running on http://localhost:${mainPort}`);
|
||||||
}
|
});
|
||||||
logger.info(`Server is running on http://localhost:${port}`);
|
|
||||||
|
// Internal server
|
||||||
|
const internalServer = express();
|
||||||
|
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}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,6 @@ unauth.get("/", (_, res) => {
|
||||||
res.status(200).json({ message: "Healthy" });
|
res.status(200).json({ message: "Healthy" });
|
||||||
});
|
});
|
||||||
|
|
||||||
unauth.use("/badger", badger);
|
|
||||||
unauth.use("/gerbil", badger);
|
|
||||||
unauth.use("/newt", badger);
|
unauth.use("/newt", badger);
|
||||||
unauth.use("/pangolin", badger);
|
unauth.use("/pangolin", badger);
|
||||||
|
|
16
server/routers/internal.ts
Normal file
16
server/routers/internal.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { Router } from "express";
|
||||||
|
import badger from "./badger/badger";
|
||||||
|
import gerbil from "./gerbil/gerbil";
|
||||||
|
import newt from "./newt/newt";
|
||||||
|
import pangolin from "./pangolin/pangolin";
|
||||||
|
|
||||||
|
const unauth = Router();
|
||||||
|
|
||||||
|
unauth.get("/", (_, res) => {
|
||||||
|
res.status(200).json({ message: "Healthy" });
|
||||||
|
});
|
||||||
|
|
||||||
|
unauth.use("/badger", badger);
|
||||||
|
unauth.use("/gerbil", badger);
|
||||||
|
|
||||||
|
export default unauth;
|
Loading…
Add table
Add a link
Reference in a new issue