-
Notifications
You must be signed in to change notification settings - Fork 60
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: Added CORS to config #302
Changes from all commits
3d7c705
1179ef3
7d2ec1a
b620de1
ff3f6f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ export default async function initialize<T: Application>(app: T, { | |
path, | ||
port, | ||
logging, | ||
database | ||
database, | ||
server: serverConfig | ||
}: Application$opts) { | ||
const routes = loader(path, 'routes'); | ||
const models = loader(path, 'models'); | ||
|
@@ -103,7 +104,8 @@ export default async function initialize<T: Application>(app: T, { | |
|
||
const server = new Server({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be beneficial to spread the const server = new Server({
...serverOpts,
router,
logger
}); |
||
router, | ||
logger | ||
logger, | ||
...serverConfig | ||
}); | ||
|
||
if (!LUX_CONSOLE) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,11 @@ export function createDefaultConfig(): Config { | |
const isProdENV = NODE_ENV === 'production'; | ||
|
||
return { | ||
server: { | ||
cors: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For style purposes let's leave this property on a single line: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought let's make the boolean cors: {
enabled: false
} |
||
enabled: false | ||
} | ||
}, | ||
logging: { | ||
level: isProdENV ? 'INFO' : 'DEBUG', | ||
format: isProdENV ? 'json' : 'text', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// @flow | ||
import type { Logger$config } from '../logger'; | ||
import type { Server$config } from '../server'; | ||
|
||
export type Config = { | ||
logging: Logger$config; | ||
server: Server$config; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,18 @@ | |
import type Logger from '../logger'; | ||
import type Router from '../router'; | ||
|
||
export type Server$opts = { | ||
export type Server$cors = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good pattern you've got going on here. I think we should keep the actual export type Server$cors = {
origin?: string;
enabled: boolean;
headers?: Array<string>;
methods?: Array<string>;
};
export type Server$config = {
cors: Server$cors;
};
export type Server$opts = Server$config & {
logger: Logger;
router: Router;
}; P.S we should always put semicolons after a |
||
enabled: boolean; | ||
origin?: string; | ||
headers?: Array<string>; | ||
methods?: Array<string>; | ||
}; | ||
|
||
export type Server$config = { | ||
cors: Server$cors; | ||
}; | ||
|
||
export type Server$opts = Server$config & { | ||
logger: Logger; | ||
router: Router; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @flow | ||
import type { Response } from '../index'; | ||
import type { Server$cors } from '../interfaces'; | ||
|
||
export default function setCORSHeaders(res: Response, { | ||
origin, | ||
methods, | ||
headers, | ||
enabled | ||
}: Server$cors) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
if (origin) { | ||
res.setHeader('Access-Control-Allow-Origin', origin); | ||
} | ||
|
||
if (methods) { | ||
res.setHeader('Access-Control-Allow-Methods', methods.join()); | ||
} | ||
|
||
if (headers) { | ||
res.setHeader('Access-Control-Allow-Headers', headers.join()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it might be useful to show the CORS config in an example project, so I've left this in here for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea!