Skip to content

Commit

Permalink
Merge pull request #13 from windingtree/feat/cors
Browse files Browse the repository at this point in the history
feat: add allow localhost in cors
  • Loading branch information
valera-rozuvan committed Aug 9, 2022
2 parents a327ecf + f2cbb0b commit cb57072
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Expand Up @@ -13,3 +13,4 @@ CLIENT_JWT={jwt}
SIMARD_JWT={jwt}
SIMARD_ORG_ID={id}
SUMARD_URL={url}
ALLOW_LOCALHOST_UI=false
4 changes: 4 additions & 0 deletions src/config.ts
Expand Up @@ -37,7 +37,11 @@ export const mongoDBUrl = String(process.env.MONGODB_URL);
export const DBName = String(process.env.DB_NAME);
export const derbySoftProxyUrl = String(process.env.DERBYSOFT_PROXY_URL);
export const clientJwt = String(process.env.CLIENT_JWT);
export const clientUrl = String(process.env.CLIENT_URL);
export const simardJwt = String(process.env.SIMARD_JWT);
export const simardOrgId = String(process.env.SIMARD_ORG_ID);
export const simardUrl = String(process.env.SUMARD_URL);
export const defaultRadius = 2000; //in meters
export const allowLocalhostUI = Boolean(
process.env.ALLOW_LOCALHOST_UI === 'true'
);
10 changes: 8 additions & 2 deletions src/services/ServerService.ts
Expand Up @@ -7,7 +7,7 @@ import morgan from 'morgan';
import router from '../router/index';
import { Express } from 'express-serve-static-core';
import errorMiddleware from '../middlewares/ErrorMiddleware';
import { debugEnabled } from '../config';
import { allowLocalhostUI, clientUrl, debugEnabled } from '../config';
import responseTime from 'response-time';
import { MetricsService } from './MetricsService';
import * as openApiValidator from 'express-openapi-validator';
Expand All @@ -33,8 +33,14 @@ export default class ServerService {
this.app.use(cookieParser());

// CORS
let origins;
if (allowLocalhostUI) {
origins = [clientUrl, 'http://localhost:3000'];
} else {
origins = clientUrl;
}
const corsOptions = {
origin: process.env.CLIENT_URL, // @todo Add handling of origins array
origin: origins,
optionsSuccessStatus: 200,
methods: 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
allowedHeaders:
Expand Down

0 comments on commit cb57072

Please sign in to comment.