Closed zengxinhui closed 1 month ago
I end up pulling the query-fn from system
directly:
(defn get-query-fn [] (:db.sql/query-fn integrant.repl.state/system))
That approach is fine for dev, but the proper way to do it for prod is to provide it to the api component in system.edn
:reitit.routes/api
{:base-path "/api"
:env #ig/ref :system/env
:query-fn #ig/ref :db.sql/query-fn}
and then grab it from the opts passed to api-routes
, e.g:
["/:id" {:get {:parameters {:path [:map [:id integer?]]}
:responses {200 {:body gifs/Gif}}
:handler (partial my-handler (:query-fn opts))}}]
Thank you. I give guestbook.html another read and just realize that the guestbook
example was updated.
Is there plan to update pages
route as well?
It should work the same way. The routes for the pages already take opts
as seen in the guestbook tutorial:
(defn page-routes [opts]
[["/" {:get (partial home opts)}]
["/save-message" {:post (partial guestbook/save-message! opts)}]])
So, you'd just have to update system.edn
to provide the query-fn
for :reitit.routes/pages
and it will be available for the handlers.
I meant that
(defn route-data [opts]
**(merge
opts**
...
(defmethod ig/init-key :reitit.routes/pages [_ {:keys [base-path] :or {base-path ""} :as opts}] (layout/init-selmer! opts) [base-path (route-data opts) (page-routes opts)])
2. https://kit-clj.github.io/docs/integrant.html#accessing_components
(defmethod ig/init-key :reitit.routes/pages [_ {:keys [base-path ] :or {base-path ""} :as opts}] (layout/init-selmer!) [base-path (route-data opts) (page-routes opts)])
Oh I see, just updated that to match. 👍
After this commit https://github.com/kit-clj/kit/commit/c2c85f82784c943559cfb0011d5e455ab6ddcf2b,
opts
is essentially ignored asapi-routes
does nothing about it.And
pages
route here https://github.com/kit-clj/modules/blob/master/html/assets/src/pages.clj is still using the same code before the commit for api.clj. Also documented here: https://kit-clj.github.io/docs/integrant.html#accessing_componentsQuestion: what's the recommended way to access query-fy in api route?