Skip to content

Commit

Permalink
backend: Parametrized CORS origin with SERVER_CORS_ORIGIN env var
Browse files Browse the repository at this point in the history
  • Loading branch information
CSantosM committed Oct 16, 2024
1 parent aca15ee commit 53d2ddc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"build": "tsc",
"start": "node dist/src/server.js",
"dev:start": "cross-env USE_HTTPS=false TESTING_MODE=ENABLED nodemon -I --exec node --experimental-specifier-resolution=node --loader ts-node/esm ./src/server.ts",
"dev:start": "cross-env USE_HTTPS=false SERVER_CORS_ORIGIN=* nodemon -I --exec node --experimental-specifier-resolution=node --loader ts-node/esm ./src/server.ts",
"lint": "eslint src --fix",
"format": "prettier --ignore-path .gitignore --write '**/*.{ts,js,json,md}'",
"pack": "npm pack"
Expand Down
6 changes: 3 additions & 3 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

import dotenv from 'dotenv';

if (process.env.CALL_CONFIG_DIR) {
dotenv.config({ path: process.env.CALL_CONFIG_DIR });
dotenv.config({ path: process.env.CALL_CONFIG_DIR });
} else {
dotenv.config();
dotenv.config();
}

// General server configuration
export const SERVER_PORT = process.env.SERVER_PORT || 6080;
export const SERVER_CORS_ORIGIN = process.env.SERVER_CORS_ORIGIN || '*';
export const CALL_NAME_ID = process.env.CALL_NAME_ID || 'OpenViduCall';
export const CALL_PRIVATE_ACCESS = process.env.CALL_PRIVATE_ACCESS || 'false';
export const CALL_USER = process.env.CALL_USER || 'user';
Expand Down
9 changes: 5 additions & 4 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ import {
CALL_ADMIN_USER,
CALL_AWS_REGION,
CALL_LOG_LEVEL,
CALL_NAME_ID
CALL_NAME_ID,
SERVER_CORS_ORIGIN
} from './config.js';

const createApp = () => {
const app = express();

// Enable CORS support
if (process.env.TESTING_MODE === 'ENABLED') {
console.log('CORS enabled');
app.use(cors({ origin: '*' }));
if (SERVER_CORS_ORIGIN) {
console.log('CORS Origin:', SERVER_CORS_ORIGIN);
app.use(cors({ origin: SERVER_CORS_ORIGIN }));
}

const __filename = fileURLToPath(import.meta.url);
Expand Down

0 comments on commit 53d2ddc

Please sign in to comment.