Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make cors origins configurable through environment variables #186

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"build": "nx run-many --targets=build --all",
"build:frontend": "nx build frontend",
"build:backend": "nx build backend",
"build:shared": "nx build shared",
"start:backend": "nx start backend",
"watch": "nx run-many --targets=build:watch --all",
"frontend:dev": "nx start frontend",
Expand Down
13 changes: 8 additions & 5 deletions packages/backend/src/setupServer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import express from "express";
import http from "http";
import { Server, Socket } from "socket.io";
import cors from "cors";
import cors, { CorsOptions } from "cors";

import { ClientToServerEvents, ServerToClientEvents } from "@shared/socket";
import { ConnectionStore } from "./store/ConnectionStore";
import { logger } from "@shared/logger";
import { configuration } from "@shared/configuration";

export function setupServer() {
const app = express();
const server = http.createServer(app);

const connectionStore = new ConnectionStore();

const corsOptions: CorsOptions = {
origin: configuration.corsOrigins,
};

const io = new Server<ClientToServerEvents, ServerToClientEvents>(server, {
cors: {
origin: "*", // TODO: add proper CORS config
},
cors: corsOptions,
allowEIO3: true,
});

app.use(cors());
app.use(cors(corsOptions));

app.get("/:namespace/rooms/:roomId", (req, res) => {
const { roomId, namespace } = req.params;
Expand Down
13 changes: 11 additions & 2 deletions packages/shared/configuration/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApplicationConfiguration } from "./types";
import { ApplicationConfiguration, CorsOrigins } from "./types";
import { RetroAppUrl } from "./RetroAppUrl";

export const configuration = getConfiguration();

function getConfiguration(): ApplicationConfiguration {
const backendUrl = new RetroAppUrl({
protocol: process.env.REACT_APP_BACKEND_PROTOCOL ?? "http",
Expand All @@ -21,7 +23,14 @@ function getConfiguration(): ApplicationConfiguration {
retro: {
maxVoteCount: Number(process.env.REACT_APP_RETRO_MAX_VOTE_COUNT) ?? 3,
},
corsOrigins: parseCorsOrigins(process.env.CORS_ORIGIN) ?? "*",
};
}

export const configuration = getConfiguration();
function parseCorsOrigins(list?: string): CorsOrigins | undefined {
if (!list) return undefined;

const origins = list.split(",").map((origin) => origin.trim());
if (origins.length <= 1) return list;
return origins;
}
3 changes: 3 additions & 0 deletions packages/shared/configuration/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { RetroAppUrl } from "./RetroAppUrl";

export type CorsOrigins = string | string[];

export interface ApplicationConfiguration {
logLevel: string;
backendUrl: RetroAppUrl;
retro: RetroConfiguration;
signalingServerUrl: RetroAppUrl;
corsOrigins: CorsOrigins;
}

export interface RetroConfiguration {
Expand Down
Loading