Skip to content
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

Add remoting options to server/config.json #164

Merged
merged 1 commit into from
Dec 1, 2014

Conversation

raymondfeng
Copy link
Member

server.use(restApiRoot, server.loopback.rest());
var remotingOptions = server.get('remoting') || {};
var restOptions = remotingOptions.rest || {};
server.use(restApiRoot, server.loopback.rest(restOptions));
Copy link
Member

Choose a reason for hiding this comment

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

The general approach is to keep the templates as small as possible and implement most features in loopack. That way we can roll bugfixes without having to ask the users to upgrade the generated code.

AFAIK, loopback.rest has access to the app object it is mounted on, thus it can read app.get('remoting') itself, there is no need to pass it down from here.

Copy link
Member Author

Choose a reason for hiding this comment

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

How to access the app instance from loopback.rest? I understand the app instance is available from req.app.

Copy link
Member

Choose a reason for hiding this comment

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

See lib/middleware/rest.js#L27, it is already accessing the app.

Copy link
Member

Choose a reason for hiding this comment

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

The trick is that you have the application available only at request-handling time, not at the time you are building the middleware/handler function.

@bajtos bajtos self-assigned this Oct 22, 2014
{ name: 'port', value: 3000 }
{ name: 'port', value: 3000 },
{ name: 'remoting', value: {
rest: {context: {enableHttpContext: false}, normalizeHttpPath: false},
Copy link
Member

Choose a reason for hiding this comment

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

The comments in strongloop/loopback#608 indicate that normalizeHttpPath is read from remoting.normalizeHttpPath, not remoting.rest.normalizeHttpPath.

Please write an automated test to verify we got this right. Something along the lines:

  1. Assert that remoting.context.normalizeHttpPath is defined as false in the scaffolded app. That way the test fails if we change the default config in the future (e.g. move the key to a different place).
  2. Edit server/config.json and set normalizeHttpPath to true.
  3. Setup anything else needed to verify that http paths are normalized.
  4. Start the server, verify that http paths are normalized now.

Ideally, there should be one test for each of the config options. To be realistic, I think it should be ok to test only enableHttpContext and normalizeHttpPath.

Copy link
Member

Choose a reason for hiding this comment

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

Also the default config is missing enableContext option as mentioned in strongloop/loopback#337

@bajtos
Copy link
Member

bajtos commented Oct 22, 2014

I feel the proposed config option structure is not optimal.

{
  remoting: {
    rest: {context: {enableHttpContext: false}, normalizeHttpPath: false},
    json: {strict: false, limit: '100kb'},
    urlencoded: {extended: true, limit: '100kb'},
    cors: {origin: true, credentials: true}
  }
}

Assuming the most common usage is to enable context middleware either for all transports (REST, JSON-RPC, WebSocket) or none of them, it makes more sense to have the context options on the top level.

{
  remoting: {
    context: { enable: true, someOtherContextOptionSharedByAllTransports: 'value' },
    rest: { enableHttpContext: false, normalizeHttpPath: false},
    json: {strict: false, limit: '100kb'},
    urlencoded: {extended: true, limit: '100kb'},
    cors: {origin: true, credentials: true}
  }
}

I am not entirely sure where does enableHttpContext belongs to, whether it should be remoting.context.enableHttpContext, remoting.rest.enableHttpContext or remoting.rest.context.enableHttpContext as originally proposed.

The goal of this exercise is to approach this problem from the POV of the future user and come up with a solution that will be easiest to understand and use, instead of using the current implementation details to drive the user interface.

@bajtos bajtos assigned raymondfeng and unassigned bajtos Oct 22, 2014
@raymondfeng
Copy link
Member Author

Does normalizeHttpPath apply to REST only?

@bajtos
Copy link
Member

bajtos commented Oct 22, 2014

Does normalizeHttpPath apply to REST only?

Yes, AFAIK. I assume JSON-RPC and WebSocket uses method names (e.g. User.create).

@bajtos
Copy link
Member

bajtos commented Oct 30, 2014

See also strongloop/loopback#708.

@raymondfeng raymondfeng force-pushed the feature/add-remoting-options branch from 169b34d to 6abf5b1 Compare October 31, 2014 22:58
@raymondfeng
Copy link
Member Author

@bajtos I updated the remoting options to be:

{
      context: {enableHttpContext: false},
      rest: {normalizeHttpPath: false},
      json: {strict: false, limit: '100kb'},
      urlencoded: {extended: true, limit: '100kb'},
      cors: {origin: true, credentials: true}
}

The changes to boot/rest-api.js is also reverted.

@bajtos
Copy link
Member

bajtos commented Nov 5, 2014

Is it worth adding errorHandler section too? See https://github.com/strongloop/strong-remoting/pull/114/files and strongloop/loopback#688.

rest: {normalizeHttpPath: false},
json: {strict: false, limit: '100kb'},
urlencoded: {extended: true, limit: '100kb'},
cors: {origin: true, credentials: true}
Copy link
Member

Choose a reason for hiding this comment

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

Please fix the coding style - add space inside curly brackets.

@raymondfeng raymondfeng force-pushed the feature/add-remoting-options branch from 6abf5b1 to 09494ad Compare November 30, 2014 23:08
@raymondfeng
Copy link
Member Author

Fixed the style and added errorHandler.

enableHttpContext: false
},
rest: {
normalizeHttpPath: false
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to add xml: false too?

Copy link
Member

Choose a reason for hiding this comment

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

@bajtos
Copy link
Member

bajtos commented Dec 1, 2014

It would be nice to add rest.xml option too, but it can be done in a new PR too.

I did not verify that the values in template match LoopBack's defaults.

LGTM.

@raymondfeng raymondfeng force-pushed the feature/add-remoting-options branch from 09494ad to d0a87c8 Compare December 1, 2014 16:35
@raymondfeng
Copy link
Member Author

Added rest.xml and rest.supportedTypes.

raymondfeng added a commit that referenced this pull request Dec 1, 2014
Add remoting options to server/config.json
@raymondfeng raymondfeng merged commit 81bee6c into master Dec 1, 2014
@raymondfeng raymondfeng deleted the feature/add-remoting-options branch December 1, 2014 17:29
rest: {
normalizeHttpPath: false,
xml: false,
supportedTypes: ['json', 'application/javascript', 'text/javascript']
Copy link
Member

Choose a reason for hiding this comment

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

This is not great. It means that to enable xml, you have to edit both xml and supportedTypes now.

Before this PR, it was enough to add xml:true and developers did not need to worry about content types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should I remove supportedTypes: ['json', 'application/javascript', 'text/javascript']?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that will be the best.

Here are some alternatives:

  • Change the value of supportedTypes to list all types including xml types. I find that a bit confusing, plus it is difficult to upgrade. You have to upgrade your config file if you want to use a new JSON-based type like application/vnd.api+json.
  • Keep the current value, tell people to delete supportedTypes together with changing xml:true. However, the problem with upgrades is still present.

Copy link
Member Author

Choose a reason for hiding this comment

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

See b682a70

Copy link
Member

Choose a reason for hiding this comment

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

👍 thanks

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.

2 participants