Skip to content

Routing for Hosts and Subdomains#363

Merged
lovelydinosaur merged 2 commits intomasterfrom
hosts-and-subdomains
Jan 30, 2019
Merged

Routing for Hosts and Subdomains#363
lovelydinosaur merged 2 commits intomasterfrom
hosts-and-subdomains

Conversation

@lovelydinosaur
Copy link
Contributor

@lovelydinosaur lovelydinosaur commented Jan 28, 2019

  • Adds app.host(host, app), which mirrors the existing app.mount(path, app).
  • Adds a Host(...) routing class, which mirrors the existing Mount routing class.

Closes #360

Examples:

Website and API, with separate routing rules:

app = Starlette()
site = Router()  # Use eg. `@site.route()` to configure this.
api = Router()  # Use eg. `@api.route()` to configure this.

app.host('www.example.org', site)
app.host('api.example.org', api)

A custom subdomain:

app = Starlette()

site = Router()  # Use eg. `@site.route()` to configure this.
subdomains = Router()  # Use eg. `@subdomains.route()` to configure this.

app.host('www.example.org', site)
app.host('{subdomain}.example.org', api)

Features:

  • Subdomain components are available in URL reversing, eg. request.url_for("users_list", subdomain=...)
  • Namespaces route naming is supported. eg. app.host('api.example.org', api, name="api') will then require reverse lookups like eg. request.url_for("api:users_list")
  • Matched subdomain parts are available on the existing request.path_params.

Considerations:

  • The name of request.path_params isn't exactly correct anymore. Should we move to request.route_params or something?
  • For testing purposes users will need an easy way to mock up the host for requests, rather than eg. 127.0.0.1. Do we want that available in Starlette or in Uvicorn? Or do we just point folks at how to edit their hosts file?
  • We might like subdomain parameters to be optional. If they're not included then request.url_for should default to current domain. (Assuming it matches the pattern?) Actually probably forget about that - we don't expect it with submounted paths, we shouldn't expect it here.
  • We need to document this.
  • I'm also becoming less convinced with the benefits of the decorator style routing, vs. composing route instances.

@lovelydinosaur
Copy link
Contributor Author

Let's get this in. We can expand the documentation in due course.

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.

Support virtual hosts

1 participant