-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor(cli): use options to specify port and OpenAPI server url #1900
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
refactor(cli): use options to specify port and OpenAPI server url #1900
Conversation
| rest: { | ||
| // your application listening port number, you can change this once | ||
| // in production | ||
| port: 3000, |
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.
Does it make sense now to have port: +process.env.PORT || 3000 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.
Yes, you are right, I forgot about the process.env.PORT support
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.
is the plus symbol +process.env.PORT intentional ?
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.
Yes. It converts the value to number.
| openApiSpec: { | ||
| // useful when used with OASGraph to locate your application when | ||
| // resolving GraphQL schemas | ||
| servers: [{url: 'http://localhost:3000'}], |
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.
Is setServersFromRequest: true better as we don't always know the port in advance?
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 have just placed setServersFromRequest: true and removed the servers: and it doesn't work, it can't find the REST api resolver. Also, I just removed the port number and it doesn't work. Only works whenever the full path like servers: [{url: 'http://localhost:3000'}] is specified.
thoughts?
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.
Please elaborate on setServersFromRequest: true
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.
If you have setServersFromRequest: true, what's servers from the generated openapi spec?
Please note that browsers may cache the response. I had to use private browsing sometimes to work around it.
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.
If you have
setServersFromRequest: true, what'sserversfrom the generated openapi spec?
"servers": [
{
"url": "http://[::1]:3010"
}
]
However, the node_modules/oasgraph/cli.js http://localhost:3010/openapi.json still doesn't work. I guess the problem is with OASGraph not liking the format [::1] ?
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.
Neither node_modules/.bin/oasgraph http://localhost:3000/openapi.json or probably the problem is the express-graphql since this is the one connecting the resolver from the GraphQL schema.
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.
http://[::1]:3010 uses ipv6 address.
3c46b71 to
70b3fb7
Compare
|
@raymondfeng |
70b3fb7 to
8663413
Compare
bajtos
left a comment
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.
Please see the discussion in #1705 on why it's a bad idea to apply process.env.PORT inside the Application class.
Accessing
process.envdeeply from our code is an anti-pattern I'd like to avoid.
- For example, how are we going to test this in a safe way?
- If we start
npm testin an environment whereprocess.env.PORTis accidentally configured (e.g. a local dev machine), tests checking the default port number would start failing.I am proposing a different solution instead: Implement a new Booter that will parse commonly used PORT and HOST variables from the environment and apply them to REST server config accordingly.
As a simpler alternative, I proposed to apply environment-specific configuration in the top-level index.js file - I think that's the easiest way how to get this functionality implemented.
if (require.main === module) {
// Run the application
const config = {
rest: {
port: process.env.PORT,
host: process.env.HOST
},
};
application.main(config).catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}| rest: { | ||
| // your application listening port number, you can change this once | ||
| // in production | ||
| port: process.env.PORT || 3000, |
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.
would you mind adding a host configuration too?
host: process.env.HOST || undefined
8663413 to
ecf130e
Compare
|
@bajtos, @raymondfeng PTAL |
| assert.fileContent('src/application.ts', /constructor\(/); | ||
| assert.fileContent('src/application.ts', /this.projectRoot = __dirname/); | ||
| assert.fileContent('src/application.ts', /openApiSpec: {/); | ||
| assert.fileContent('src/application.ts', /port: process.env.PORT || 3000/); |
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.
Is this still true after moving process.env.PORT to index.js?
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.
You are right!, but it didn't give any error, it should have returned false, what would be happening?
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.
do you think the || is being evaluated ?
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.
Yeah, you regular expression might have wildcard matches.
ecf130e to
231d5b9
Compare
|
@raymondfeng I addressed the test evaluation. PTAL |
| application.main().catch(err => { | ||
| const config = { | ||
| rest: { | ||
| port: process.env.PORT, |
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.
+process.env.PORT || 0?
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.
fixed!, thanks @raymondfeng
231d5b9 to
89838aa
Compare
|
@bajtos PTAL |
89838aa to
6a6f6b4
Compare
bajtos
left a comment
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.
LGTM.
| port: process.env.PORT || 3000, | ||
| openApiSpec: { | ||
| // useful when used with OASGraph to locate your application | ||
| setServersFromRequest: true, |
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 think it makes sense to keep this part inside the Application class constructor 👍
Would it be difficult to modify the tests we are scaffolding by lb4 app and include a new check to verify that setServersFromRequest is enabled?
(OTOH, I still thinks it's a bug of oasgraph that they don't support relative server addresses.)
bajtos
left a comment
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.
Actually, I have to withdraw my previous approval. Please update all example projects to be consistent with the new index.js template. No further review is required for that.
| export class <%= project.applicationName %> extends BootMixin(RestApplication) { | ||
| <% } -%> | ||
| constructor(options: ApplicationConfig = {}) { | ||
| options = Object.assign( |
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.
Could you please verify that Object.assign is preserving deep values provided by options? I believe it will replace rest object as a whole, i.e. changing {rest: {port: 123, host: 'name'}} provided by the top-level index.js into {rest: {openApiSpec: {setServersFromRequest: true}}}. A possible solution is to use a deep-merge algorithm, there are many implementations available on npmjs. deepmerge seems like a popular one that's still actively maintained. lodash provides deep-merge or deep-assign API too.
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.
Verified, it replaces it. I will take a look at deepmerge and lodash 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.
Why not move the config to index.js.ejs?
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.
That could also be a good solution @raymondfeng , simplifies everything for this context. @bajtos thoughts?
6a6f6b4 to
9107a26
Compare
@bajtos / @raymondfeng could you check the new application.ts ?, and I also placed a default value for host on index.js Now it works with lodash merge method and is preserving the port and host passed thru the index.js file. After that, I will update the examples too. |
|
I still prefer to move it into index.js.ejs. Using lodash to deep merge in application ctor is a bit overwhelming. |
9107a26 to
0f0a8a5
Compare
|
@raymondfeng I agree!, @bajtos are you ok with this PR now ?. If so, then I will change the examples also. |
bajtos
left a comment
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.
👍
This PR only applies the options object in the application to benefit the app developer in two areas:
This pull request supersedes #1705.
Checklist
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated