feat(server): choose cors origins by NODE_ENV and run docker in production
- allow the vite dev server origins in development and the compose port origins in production; CORS_ORIGINS overrides both - set NODE_ENV=production in the runtime docker image - document both settings in .env.example and the development docs - bump version to 1.19.3
This commit is contained in:
1 parent
a6d468bd2c
commit
5de9272281
11 files changed
+90
-6
No files matched your search
@@ -0,0 +1,23 @@
|
||||
/** Vite dev server (`npm -w client run dev`), calling the API cross-origin. */
|
||||
const DEV_ORIGINS = ["http://localhost:5173", "http://127.0.0.1:5173"];
|
||||
|
||||
/**
|
||||
* The port compose.yaml publishes. The UI is served from the same origin in
|
||||
* production, so these only matter for other tools calling the API from a
|
||||
* page on that origin; keep in sync with compose.yaml.
|
||||
*/
|
||||
const PRODUCTION_ORIGINS = ["http://localhost:13000", "http://127.0.0.1:13000"];
|
||||
|
||||
/**
|
||||
* Origins allowed to call the API cross-origin. `CORS_ORIGINS` (comma
|
||||
* separated) overrides the defaults, which depend on NODE_ENV: the compose
|
||||
* port in production, the Vite dev server otherwise.
|
||||
*/
|
||||
export function corsOrigins(nodeEnv: string, override: string): string[] {
|
||||
const configured = override
|
||||
.split(",")
|
||||
.map((origin) => origin.trim())
|
||||
.filter(Boolean);
|
||||
if (configured.length > 0) return configured;
|
||||
return nodeEnv === "production" ? PRODUCTION_ORIGINS : DEV_ORIGINS;
|
||||
}
|
||||
@@ -17,9 +17,14 @@ export enum TraceMode {
|
||||
}
|
||||
|
||||
export class AppConfig {
|
||||
/** `production` in the Docker image; also selects the default CORS origins. */
|
||||
@IsString()
|
||||
NODE_ENV: string = "development";
|
||||
|
||||
/** Comma-separated origins allowed to call the API cross-origin; empty = default for NODE_ENV. */
|
||||
@IsString()
|
||||
CORS_ORIGINS: string = "";
|
||||
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@Max(65535)
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
||||
import { AppModule } from "./app.module";
|
||||
import { appPackage } from "./common/app-package";
|
||||
import { configureBodyParsers } from "./common/body-parsers";
|
||||
import { corsOrigins } from "./common/cors";
|
||||
import { TraceLogger } from "./common/trace-logger";
|
||||
import { AppConfig } from "./config/app.config";
|
||||
import { HttpExceptionFilter } from "./filters/http-exception.filter";
|
||||
@@ -53,7 +54,7 @@ async function bootstrap() {
|
||||
const nodeEnv = config.get("NODE_ENV");
|
||||
|
||||
app.enableCors({
|
||||
origin: ["http://localhost:5173", "http://127.0.0.1:5173"],
|
||||
origin: corsOrigins(nodeEnv, config.get("CORS_ORIGINS")),
|
||||
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"],
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { corsOrigins } from "../src/common/cors";
|
||||
|
||||
describe("corsOrigins", () => {
|
||||
it("allows the Vite dev server outside production", () => {
|
||||
for (const env of ["development", "test", ""]) {
|
||||
expect(corsOrigins(env, "")).toEqual([
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:5173",
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it("allows the compose port in production", () => {
|
||||
expect(corsOrigins("production", "")).toEqual([
|
||||
"http://localhost:13000",
|
||||
"http://127.0.0.1:13000",
|
||||
]);
|
||||
});
|
||||
|
||||
it("uses CORS_ORIGINS when set, in any environment", () => {
|
||||
const override = " https://liqa.example.com , http://localhost:8080,";
|
||||
for (const env of ["development", "production"]) {
|
||||
expect(corsOrigins(env, override)).toEqual([
|
||||
"https://liqa.example.com",
|
||||
"http://localhost:8080",
|
||||
]);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user