Fork me on GitHub
#reitit
<
2018-07-24
>
zarkone08:07:55

@ikitommi hi! tried to follow this guide: https://metosin.github.io/reitit/advanced/composing_routers.html, however, can't get how to build handler with such dynamic routing:

["/*" {:handler (fn [request]
                             ;; (def tmp-request request)
                             (let [handler (-> @external-router
                                               (r/match-by-path (:uri request))
                                               :result
                                               
                                               )]

                               (handler request)))

                  :swagger {:id ::external-api
                   :tags ["Available endpoints"]}
                  :name ::endpoints}

            ]
this kind of worked, but it seems like middleware was not applied...

ikitommi09:07:54

@zarkone using reitit-ring? With it, the middleware is applied by the ring-handler function. So, you should re-create such in the endpoint and pass is the request.

ikitommi09:07:27

I think the guides could have a "nesting routers" part too, for both core & ring.

ikitommi09:07:53

You are doing nesting, right?

zarkone09:07:11

thanks, just got to ring-handler point also: external-router is reitit-router, not ring: (reset! external-router (reitit.core/router (generate-external-router)))

zarkone09:07:42

yes, this routes are nested, will check out this section

zarkone09:07:25

as I understand, I should pass ring-router to ring-handler, will now try to do that

zarkone09:07:58

> could have ah, no section yet 🙂

zarkone09:07:45

thanks anyway, will try it out now!

zarkone10:07:04

so, we've got it to work, thanks 🙂 however, we've found that for nested routes you have to add coersions explicitly:

(defstate routes
  :start (fn []
           (reset! external-router (ring/router (generate-external-router) {:data {:middleware [ring.coercion/coerce-exceptions-middleware
                         ring.coercion/coerce-request-middleware
                         ring.coercion/coerce-response-middleware]
                                                                                   }}))
           ["/*" {:handler (fn [request]
                             (def tmp-request request)
                             (let [request tmp-request ;;for debug here
                                   handler (-> @external-router
                                               (ring/ring-handler)
                                               )]

                               handler
                               (handler request)
                               )
                             )

                  :swagger {:id ::external-api
                            :tags ["Available endpoints"]}
                  :name ::endpoints}

            ]
           )
  :stop nil)

hope this makes sense!