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

fix(example-getting-started): use RestApplication #955

Merged
merged 1 commit into from
Feb 5, 2018

Conversation

shimks
Copy link
Contributor

@shimks shimks commented Feb 2, 2018

Checklist

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

Copy link
Contributor

@virkt25 virkt25 left a comment

Choose a reason for hiding this comment

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

I have this in the boot PR but it's a great idea to land this as a separate change. Left a few comments.

import {Application, ApplicationConfig} from '@loopback/core';
import {RestComponent} from '@loopback/rest';
import {ApplicationConfig} from '@loopback/core';
import {RestComponent, RestApplication} from '@loopback/rest';
Copy link
Contributor

Choose a reason for hiding this comment

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

You no longer need RestComponent.

@@ -30,6 +30,7 @@
"dependencies": {
"@loopback/context": "^4.0.0-alpha.29",
"@loopback/core": "^4.0.0-alpha.31",
"@loopback/openapi-spec": "^4.0.0-alpha.23",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being added? (Don't see it being used in the package).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to be used in todo.model.ts, but the compiler didn't complain about the package not being a dependency though, it's weird. It's entirely unrelated to migrating to RestApplication btw.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's because this example still uses top-down approach to define the schema for a model.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's weird indeed that it wasn't complaining about the dependency being missing before. Makes me wonder if something is broken / needs to be investigated with out build process. (Not a blocker for this PR ~ Great catch!)

@bajtos
Copy link
Member

bajtos commented Feb 5, 2018

The changes look good to me. There is more cleanup related to RestApplication - code setting up per-server configuration like server.route, server.sequence, etc. should be moved out of app.start to app constructor now.

See e.g. pages/en/lb4/Routes.md:

const app = new RestApplication();
// ...
(async function start() {
  const server = await app.getServer(RestServer);
  const route = new Route('get', '/', spec, greet);
  server.route(route);
  await app.start();
})();

Proposed update:

const app = new RestApplication();
const route = new Route('get', '/', spec, greet);
app.route(route);
// ...
(async function start() {
  await app.start();
})();

We need to check all places that contain REST-related configuration in app.start(). It's ok for me to make these changes in a follow-up pull request. Small incremental pull requests FTW!

@shimks
Copy link
Contributor Author

shimks commented Feb 5, 2018

@bajtos I'll work on a PR for your proposal at loopback.io. Good proposal by the way!

@shimks shimks merged commit 3829878 into master Feb 5, 2018
@shimks shimks deleted the use-rest-application branch February 5, 2018 16:34
@shimks
Copy link
Contributor Author

shimks commented Feb 5, 2018

@bajtos On second thought, I've noticed that almost all examples that set up rest config have it done in the async start function. Do we want to make this change for sure?

For example, our own CLI template sets up the config inside the start function

async start() {
    const server = await this.getServer(RestServer);
    server.sequence(MySequence);
    const port = await server.get('rest.port');
    console.log(`Server is running at http://127.0.0.1:${port}`);
    console.log(`Try http://127.0.0.1:${port}/ping`);
    return await super.start();
  }

This issue may need more discussion and I'd feel it might be better dealt in a separate issue.

@bajtos
Copy link
Member

bajtos commented Feb 6, 2018

@bajtos On second thought, I've noticed that almost all examples that set up rest config have it done in the async start function. Do we want to make this change for sure?

For example, our own CLI template sets up the config inside the start function

async start() {
    const server = await this.getServer(RestServer);
    server.sequence(MySequence);
    const port = await server.get('rest.port');
    console.log(`Server is running at http://127.0.0.1:${port}`);
    console.log(`Try http://127.0.0.1:${port}/ping`);
    return await super.start();
  }

This issue may need more discussion and I'd feel it might be better dealt in a separate issue.

IIRC, the original code snippets were configuring sequence & routes in the constructor. This setup code was moved to async start function after @kjdelisle implemented support for multiple REST servers. I was never happy about that change to be honest. For example, it means that code inspecting application's OpenAPI spec (think of lb export-api-def) cannot see all routes until the application is started. Now that we have RestApplication, there is no need to use that suboptimal workaround and we can revert back to the original version.

I am ok to leave those changes out of scope of this PR, but I think they are part of #846. Let me leave a comment there too.

@bajtos
Copy link
Member

bajtos commented Feb 6, 2018

See #968

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.

4 participants