metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.4k stars 252 forks source link

Is it possible to do multi host with reitit-routes? #660

Closed matheusfrancisco closed 5 months ago

matheusfrancisco commented 5 months ago

I did found on the documentation anything about do a multiple host routes. I would like to do a set of routes to when I hit api.xpto.com it will redirect to be available some set of routes, and when I hit api2.xpto.com it will be available another set of routes.

Currently, I have an application running in production with pedestal routes, so I was switching to reitit, but I miss this feature, if is something you guys would like to have or any suggestion I'm willing to open a pull request or something.

(ns myapp.service
  (:require [io.pedestal.http.route :as route]))

(def application-routes
  (route/expand-routes
    #{{:host "example.com" :scheme :https}
      ["/user"          :get user-search-form]
      ["/user/:user-id" :get view-user        :constraints {:user-id #"[0-9]+"}]
      ,,,
      }))
ikitommi commented 5 months ago

There is nothing built-in, but you can build this easily on top of reitit. e.g.

  1. create a middleware that reads :host and :schema keys from route data. see parameter coercion middleware as example, e.g. either needs to needs to be set for the middleware to mount into to the route
  2. mount the middleware to top-level
  3. attach data to route tree, at any level, e.g.
[["" {:host "example.com"} ...]
 ["/all-hosts"
  ["/trade" {:host "trade.com"} ...]
  ["/api"
   ["/admin" {:host "admin.com" :scheme :https} ...]
   ["/public" {:host "public.com"} ...]]]]
matheusfrancisco commented 5 months ago

it might not be a use case for an example here in reitit but I will put my repo here https://github.com/matheusfrancisco/multihost-reitit-pedestal with the interceptor that work for simple case, than I will do a complex case and migrate the moclojer https://github.com/moclojer/moclojer for reitit thanks for your support @ikitommi if you want you can close this issue