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

Update http route registration in migration guide and examples #48518

Merged
merged 7 commits into from
Oct 22, 2019

Conversation

rudolf
Copy link
Contributor

@rudolf rudolf commented Oct 17, 2019

Summary

Update http route registration in migration guide and migration examples. Make it clearer that a shim exposing server.route isn't NP compatible, but can be a helpful way to break down the migration into steps.

[skip-ci]

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@rudolf rudolf requested a review from a team as a code owner October 17, 2019 12:43
@rudolf rudolf added v7.6.0 v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc labels Oct 17, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@rudolf rudolf mentioned this pull request Oct 17, 2019
8 tasks
elasticsearch: ElasticsearchPlugin // note: Elasticsearch is in Core in NP, rather than a plugin
http: {
route: Legacy.Server['route'] // note: NP uses `http.createRouter()`
Copy link
Member

Choose a reason for hiding this comment

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

Is there any intention to provide core.http.route as a method in the NP, that works like this? If not, then I think showing this as a "shim" is confusing because I interpret "shim" as "massage the old way so that it looks and works like the new way".

Copy link
Member

@jasonrhodes jasonrhodes Oct 17, 2019

Choose a reason for hiding this comment

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

In other words if in NP core.http exists, but core.http.route isn't going to exist, then this shim breaks your future code in a hard to notice way.

If we need an example of how to keep using legacy router in the shim (do we need this?) I would say maybe shimming it as core.legacyHttp.route is a clearer way of indicating what's going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

core.http.route will never exist.

If we need an example of how to keep using legacy router in the shim (do we need this?) I would say maybe shimming it as core.legacyHttp.route is a clearer way of indicating what's going on.

I think the intention is to use types to minimize the legacy API surface that's depended on and make these dependencies explicit. So instead of just say "I depend on Legacy.Server" you narrow it down to just the hapi router or just a dependency on another plugin exposed via the server object.

I agree that this introduces confusion in this case and like your idea of making it more explicit. The shimming instructions were written before most of the API's were available in CoreSetup. Now that the real coreSetup has many useful API's another approach might be to inject three variables into setup:

import { CoreSetup } from 'src/core/server';

export interface LegacySetup {
  server: {
    route: Legacy.Server['route']
  }
}

export class Plugin {
  public setup(core: CoreSetup, plugins: {}, legacy: LegacySetup) {
    legacy.route({...})
  }

This gives you the added safety that you're relying on Core's CoreSetup so you don't have to wonder if your shimmed MyPluginCoreSetup is accurately shimming core.

Copy link
Member

Choose a reason for hiding this comment

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

I like that idea a lot to be honest.

Copy link
Contributor

@joshdover joshdover Oct 17, 2019

Choose a reason for hiding this comment

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

Another option is to use the __legacy namespace that some plugins have been doing:

import { CoreSetup } from 'src/core/server';

export interface DemoPluginCoreSetup extends CoreSetup {
  __legacy: {
    server: {
      route: Legacy.Server['route'];
    };
  };
}

export class Plugin {
  public setup(core: CoreSetup, plugins: {}) {
    core.__legacy.server.route({...})
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I like what you have here and would be happy to see it merge.

core.http.route will never exist.

Is that correct? I opened #44174 about core.http.route and @restrry replied "should be done as a part of #44620" so I thought it was coming eventually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have broad consensus that registering routes with a configuration object could be a nicer API but I don't think we have concrete designs for how it will be exposed. I imagined that we might still have a core.http.createRouter(path: string) method like today, but that the Router instance returned from this would allow you to do router.add({...}) or router.register({...})

However, this conversation kinda changed my mind on the our approach to shimming plugins. We used to recommend constructing a CoreSetup that looks as close as possible to what CoreSetup will look like in the New Platform. This has caused a lot of confusion and speculation which I realise now doesn't add much value.

As @jasonrhodes pointed out:

then this shim breaks your future code in a hard to notice way.

Instead of making it look the same, I think the goal should rather be to have legacy be as different and noticeable as possible, and make the exact legacy API surface explicit by picking only the legacy methods that you depend on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Exposing a function on a router instance, vs core.http is also what I was expecting. Thanks for the explanation & confirmation. Sorry for the noise.

@elastic elastic deleted a comment from elasticmachine Oct 17, 2019
- [3. New Platform shim using New Platform router](#3-new-platform-shim-using-new-platform-router)
- [4. New Platform plugin](#4-new-platform-plugin)
- [Accessing Services](#accessing-services)
- [Chrome](#chrome)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the TOC

> property instead of introducing a third parameter to your plugins lifecycle
> function. The important thing is that you reduce the legacy API surface that
> you depend on to a minimum by only picking and injecting the methods you
> require and that you clearly differentiate legacy dependencies in a namespace.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@rudolf rudolf removed the v7.6.0 label Oct 22, 2019
@rudolf rudolf merged commit 5563ee9 into elastic:master Oct 22, 2019
@rudolf rudolf deleted the http-router-migration-docs branch May 16, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants