Skip to content

Commit

Permalink
feat: make CORS headers configurable (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Apr 21, 2024
1 parent c3f6aaa commit efc463f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ prepayminerfee = false
host = "127.0.0.1"
port = 9_001

# Configure CORS headers set by the backend
# "" to disable
cors = "*"

# The backend can expose a metrics endpoint about swap count, volume, etc
# [prometheus]
# host = "127.0.0.1"
Expand Down
2 changes: 2 additions & 0 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type EthereumConfig = RskConfig & {
type ApiConfig = {
host: string;
port: number;
cors?: string | string[];
};

type GrpcConfig = {
Expand Down Expand Up @@ -244,6 +245,7 @@ class Config {
api: {
host: '127.0.0.1',
port: 9001,
cors: '*',
},

grpc: {
Expand Down
13 changes: 12 additions & 1 deletion lib/api/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ class Api {
) {
this.app = express();
this.app.set('trust proxy', 'loopback');
this.app.use(cors());

if (config.cors === undefined || config.cors.length !== 0) {
this.app.use(
cors({
origin: config.cors || '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
}),
);
}

this.app.use(
express.json({
verify(req, _, buf: Buffer, encoding: string) {
Expand Down

0 comments on commit efc463f

Please sign in to comment.