mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-30 14:39:29 +02:00
env context and refactor api support different ports
This commit is contained in:
parent
f6efdfcd93
commit
acab2ff3a7
35 changed files with 287 additions and 135 deletions
|
@ -3,54 +3,71 @@ import cors from "cors";
|
|||
import cookieParser from "cookie-parser";
|
||||
import config from "@server/config";
|
||||
import logger from "@server/logger";
|
||||
import { errorHandlerMiddleware, notFoundMiddleware, rateLimitMiddleware } from "@server/middlewares";
|
||||
import {
|
||||
errorHandlerMiddleware,
|
||||
notFoundMiddleware,
|
||||
rateLimitMiddleware,
|
||||
} from "@server/middlewares";
|
||||
import { authenticated, unauthenticated } from "@server/routers/external";
|
||||
import { router as wsRouter, handleWSUpgrade } from "@server/routers/ws";
|
||||
import { logIncomingMiddleware } from "./middlewares/logIncoming";
|
||||
import helmet from "helmet";
|
||||
|
||||
const dev = process.env.ENVIRONMENT !== "prod";
|
||||
const externalPort = config.server.external_port;
|
||||
|
||||
export function createApiServer() {
|
||||
const apiServer = express();
|
||||
|
||||
// Middleware setup
|
||||
apiServer.set("trust proxy", 1);
|
||||
apiServer.use(cors());
|
||||
apiServer.use(cookieParser());
|
||||
apiServer.use(express.json());
|
||||
|
||||
if (!dev) {
|
||||
apiServer.use(
|
||||
rateLimitMiddleware({
|
||||
windowMin: config.rate_limit.window_minutes,
|
||||
max: config.rate_limit.max_requests,
|
||||
type: "IP_ONLY",
|
||||
})
|
||||
);
|
||||
}
|
||||
const apiServer = express();
|
||||
|
||||
// API routes
|
||||
const prefix = `/api/v1`;
|
||||
apiServer.use(logIncomingMiddleware);
|
||||
apiServer.use(prefix, unauthenticated);
|
||||
apiServer.use(prefix, authenticated);
|
||||
|
||||
// WebSocket routes
|
||||
apiServer.use(prefix, wsRouter);
|
||||
|
||||
// Error handling
|
||||
apiServer.use(notFoundMiddleware);
|
||||
apiServer.use(errorHandlerMiddleware);
|
||||
// Middleware setup
|
||||
apiServer.set("trust proxy", 1);
|
||||
if (dev) {
|
||||
apiServer.use(
|
||||
cors({
|
||||
origin: `http://localhost:${config.server.next_port}`,
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
apiServer.use(cors());
|
||||
apiServer.use(helmet());
|
||||
}
|
||||
apiServer.use(cookieParser());
|
||||
apiServer.use(express.json());
|
||||
|
||||
// Create HTTP server
|
||||
const httpServer = apiServer.listen(externalPort, (err?: any) => {
|
||||
if (err) throw err;
|
||||
logger.info(`API server is running on http://localhost:${externalPort}`);
|
||||
});
|
||||
if (!dev) {
|
||||
apiServer.use(
|
||||
rateLimitMiddleware({
|
||||
windowMin: config.rate_limit.window_minutes,
|
||||
max: config.rate_limit.max_requests,
|
||||
type: "IP_ONLY",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Handle WebSocket upgrades
|
||||
handleWSUpgrade(httpServer);
|
||||
// API routes
|
||||
const prefix = `/api/v1`;
|
||||
apiServer.use(logIncomingMiddleware);
|
||||
apiServer.use(prefix, unauthenticated);
|
||||
apiServer.use(prefix, authenticated);
|
||||
|
||||
return httpServer;
|
||||
// WebSocket routes
|
||||
apiServer.use(prefix, wsRouter);
|
||||
|
||||
// Error handling
|
||||
apiServer.use(notFoundMiddleware);
|
||||
apiServer.use(errorHandlerMiddleware);
|
||||
|
||||
// Create HTTP server
|
||||
const httpServer = apiServer.listen(externalPort, (err?: any) => {
|
||||
if (err) throw err;
|
||||
logger.info(
|
||||
`API server is running on http://localhost:${externalPort}`,
|
||||
);
|
||||
});
|
||||
|
||||
// Handle WebSocket upgrades
|
||||
handleWSUpgrade(httpServer);
|
||||
|
||||
return httpServer;
|
||||
}
|
||||
|
|
|
@ -124,6 +124,7 @@ if (!parsedConfig.success) {
|
|||
throw new Error(`Invalid configuration file: ${errors}`);
|
||||
}
|
||||
|
||||
process.env.NEXT_PORT = parsedConfig.data.server.next_port.toString();
|
||||
process.env.SERVER_EXTERNAL_PORT =
|
||||
parsedConfig.data.server.external_port.toString();
|
||||
process.env.SERVER_INTERNAL_PORT =
|
||||
|
|
|
@ -5,31 +5,31 @@ import { createInternalServer } from "./internalServer";
|
|||
import { User, UserOrg } from "./db/schema";
|
||||
|
||||
async function startServers() {
|
||||
await ensureActions();
|
||||
|
||||
// Start all servers
|
||||
const apiServer = createApiServer();
|
||||
const nextServer = await createNextServer();
|
||||
const internalServer = createInternalServer();
|
||||
await ensureActions();
|
||||
|
||||
return {
|
||||
apiServer,
|
||||
nextServer,
|
||||
internalServer
|
||||
};
|
||||
// Start all servers
|
||||
const apiServer = createApiServer();
|
||||
const nextServer = await createNextServer();
|
||||
const internalServer = createInternalServer();
|
||||
|
||||
return {
|
||||
apiServer,
|
||||
nextServer,
|
||||
internalServer,
|
||||
};
|
||||
}
|
||||
|
||||
// Types
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: User;
|
||||
userOrg?: UserOrg;
|
||||
userOrgRoleId?: number;
|
||||
userOrgId?: string;
|
||||
userOrgIds?: string[];
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: User;
|
||||
userOrg?: UserOrg;
|
||||
userOrgRoleId?: number;
|
||||
userOrgId?: string;
|
||||
userOrgIds?: string[];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
startServers().catch(console.error);
|
||||
|
|
|
@ -26,4 +26,4 @@ export async function createNextServer() {
|
|||
});
|
||||
|
||||
return nextServer;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue