Fork me on GitHub
#biff
<
2022-10-01
>
Jacob O'Bryant02:10:42

Next week: planning to replace the task script with bb tasks , and a handful of other small-ish improvements (e.g. submit-tx is going to become a bit fancier). After that I'll start on a medium-sized create-your-own-forum-thing tutorial for the docs, the project which my latest clj together funding is for.

🦾 1
Jesse17:10:33

Does anyone know if it's possible to match routes with different domains, instead of just paths? I have a subdomain that I would like to serve a couple of specific routes from my main Biff server, I did not find anything for this in Reitit yet

Jacob O'Bryant17:10:40

If nothing else, you could always make two separate reitit routers/handlers and then put some middleware/another handler on top that picks which one to use based on the domain. e.g. in your main clj file, replace this bit:

(def routes [["" {:middleware [anti-forgery/wrap-anti-forgery
                               biff/wrap-anti-forgery-websockets
                               biff/wrap-render-rum]}
              (keep :routes features)]
             (keep :api-routes features)])

(def handler (-> (biff/reitit-handler {:routes routes})
                 (biff/wrap-inner-defaults {})))
With this:
(def routes [["" {:middleware [anti-forgery/wrap-anti-forgery
                               biff/wrap-anti-forgery-websockets
                               biff/wrap-render-rum]}
              (keep :routes features)]
             (keep :api-routes features)])

(def custom-domain-routes [["" {:middleware [anti-forgery/wrap-anti-forgery
                                             biff/wrap-anti-forgery-websockets
                                             biff/wrap-render-rum]}
                            (keep :custom-domain-routes features)]
                           (keep :custom-domain-api-routes features)])

(defn dispatch-on-domain [handler1 handler2]
  (fn [request]
    ((if ...
       handler1
       handler2)
     request)))

(def handler (-> (dispatch-on-domain (biff/reitit-handler {:routes routes})
                                     (biff/reitit-handler {:routes custom-domain-routes}))
                 (biff/wrap-inner-defaults {})))
and then put all the subdomain routes under :custom-domain-routes and/or :custom-domain-api-routes in your feature maps (maybe rename those to be less verbose).

Jacob O'Bryant18:10:32

alternatively maybe you could add middleware to rewrite the path, e.g. if you get a request to /foo/ on the subdomain, have the middleware change it to /subdomain/foo/ .

Jacob O'Bryant18:10:42

(you'll also need to make sure the letsencrypt cert works for the subdomain if you haven't already--should be pretty easy though, I think just re-run certbot --nginx on the server)

1
Jesse19:10:11

That works, thanks!

👌 1