Routing subdomains to a slice

I’m building a multi-tenant app where users have their own space on a subdomain or possibly a custom domain name. The host option seems to be gone in Hanami 2.x. How can I route subdomains (*.mydomain.com) to a specific slice?

Hi @wout, thanks for using Hanami!

You’re right that we don’t have :host mounting support in 2.x yet. If you’re interested in contributing to Hanami, I’d love to see this come back, and would be very happy to support any work to do so. Here was where the feature introduced originally, if you’re looking for inspiration: Match against host by nicolaracco · Pull Request #104 · hanami/router · GitHub

In the meantime, you’d probably need to pursue alternative strategies. It might mean you have to reorient your routes around sub-paths for the time being.

1 Like

Thanks for the quick reply!

I’m in for contributing, but I’m still getting to terms with Hanami, so I’ll need some pointers.

Is there a plan or idea of how this should work? Should it be the same approach as in version 1.3?

In my use case, I’ll need both subdomains and custom domains to be routed to a specific slice. So I imagine something like:

# the app's main root (fallback)
root to: 'home.show'

# tenant root, if conditions are met
slice :tenant, at: '/', if: ->(request) { ... } do
  root to: 'home.show'
end

The lambda (or callable object) returns a boolean and can contain arbitrary logic. So it can cover any use case ranging from specific subdomains (e.g. api.example.com) to wildcard subdomains, or even completely different domain names.

I’m not sure if this makes sense or is even achievable that way; just thinking out loud.