Skip to content

Conversation

@marioestradarosa
Copy link
Contributor

@marioestradarosa marioestradarosa commented Oct 23, 2018

This PR only applies the options object in the application to benefit the app developer in two areas:

  1. For new developers change the application port number with ease
  2. Avoid the step to specify the server url + port number when used by OASGraph (if not used, then I believe it won't hurt? )

This pull request supersedes #1705.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

rest: {
// your application listening port number, you can change this once
// in production
port: 3000,
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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 ?

Copy link
Contributor

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'}],
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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?

"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] ?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 3c46b71 to 70b3fb7 Compare October 23, 2018 04:08
@marioestradarosa
Copy link
Contributor Author

@raymondfeng setServersFromRequest: true works fine. I was querying the wrong schema from my browser. PTAL.

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 70b3fb7 to 8663413 Compare October 23, 2018 04:38
Copy link
Member

@bajtos bajtos left a 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.env deeply 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 test in an environment where process.env.PORT is 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,
Copy link
Member

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

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 8663413 to ecf130e Compare October 23, 2018 15:19
@marioestradarosa
Copy link
Contributor Author

@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/);
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor Author

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 ?

Copy link
Contributor

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.

@marioestradarosa
Copy link
Contributor Author

@raymondfeng I addressed the test evaluation. PTAL

application.main().catch(err => {
const config = {
rest: {
port: process.env.PORT,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+process.env.PORT || 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!, thanks @raymondfeng

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 231d5b9 to 89838aa Compare October 23, 2018 18:06
@marioestradarosa
Copy link
Contributor Author

@bajtos PTAL

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 89838aa to 6a6f6b4 Compare October 25, 2018 02:47
Copy link
Member

@bajtos bajtos left a 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,
Copy link
Member

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.)

Copy link
Member

@bajtos bajtos left a 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(
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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?

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 6a6f6b4 to 9107a26 Compare October 26, 2018 03:59
@marioestradarosa
Copy link
Contributor Author

Please update all example projects to be consistent with the new index.js template

@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.

@raymondfeng
Copy link
Contributor

I still prefer to move it into index.js.ejs. Using lodash to deep merge in application ctor is a bit overwhelming.

@marioestradarosa marioestradarosa force-pushed the add-openapi-server-property branch from 9107a26 to 0f0a8a5 Compare October 26, 2018 05:24
@marioestradarosa
Copy link
Contributor Author

@raymondfeng I agree!, @bajtos are you ok with this PR now ?. If so, then I will change the examples also.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@marioestradarosa marioestradarosa merged commit 2e7d4bb into loopbackio:master Oct 26, 2018
@marioestradarosa marioestradarosa deleted the add-openapi-server-property branch October 26, 2018 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants