duct

richiardiandrea 2021-04-13T00:51:10.012100Z

Hi folks, I am still using an old version of duct and I have a question. Say one of my components is referencing a composite key that is defined in one of the :include but I want to override with a new implementation of init-key How I would go about that? One thing that I cannot do is to modify the original files, so it will have to go in a separate .edn file...

richiardiandrea 2021-04-13T15:14:40.014500Z

Yeah I am on a version that does not have profiles...

2021-04-22T07:15:31.025900Z

You can pretty trivially rebuild profiles how you want them using duct/merge-configs. Assuming your version of duct has it. Just be sure to use the same set of readers.

2021-04-22T07:15:58.026100Z

I wouldn’t use merge, as you’ll probably want the meta-merge semantics.

👍 2
richiardiandrea 2021-04-22T15:24:50.026500Z

Thank you sounds good

richiardiandrea 2021-04-13T00:51:57.012200Z

I was thinking of doing something like

(merge (io/resource "config.edn")
       (io/resource "overrides.edn"))

richiardiandrea 2021-04-13T00:54:19.012400Z

forgot to mention that this is not as dev profile but it will be read by a completely separate "production" app

walterl 2021-04-13T01:28:21.012600Z

We pass multiple profiles to (duct/prep-config (read-config) profiles), so that later profiles overwrite keys in earlier ones. E.g. we pass [:duct.profile/dev :duct.profile/local] for dev, so that anything in local.edn overrides the defaults in dev.edn.

scaturr 2021-04-13T14:43:48.014100Z

Does anyone have a pro-tip for getting the raw request body in a duct application? I'm not able to do (slurp (:body request)) as it returns an empty string - I am assuming because it has been consumed by an earlier middleware

walterl 2021-04-13T15:05:14.014300Z

Add an earlier middleware that copies the request body to a separate key 🤷

scaturr 2021-04-13T15:15:03.014700Z

How does one tinker with the middleware order in duct? Especially those that are included by default?

walterl 2021-04-13T17:34:19.014900Z

:duct.server.http/jetty
  {:port    #duct/env ["HTTP_SERVER_PORT" Int :or 5000]
   :handler #ig/ref :<http://my-app.app/handler|my-app.app/handler>
   :logger  #ig/ref :duct.logger/timbre}

; ...

  :<http://my-app.app/handler|my-app.app/handler>
  {:router     #ig/ref :duct.router/cascading
   :middleware [#ig/ref :my-app.middleware/sentry
                #ig/ref :my-app.middleware/bind-db
                #ig/ref :my-app.middleware.session/bind-session
                #ig/ref :duct.middleware.web/not-found
                #ig/ref :duct.middleware.web/webjars
                #ig/ref :duct.middleware.web/format
                #ig/ref :duct.middleware.web/defaults
                #ig/ref :duct.middleware.web/log-requests
                #ig/ref :duct.middleware.web/log-errors
                #ig/ref :my-app.middleware/error-middleware]}

walterl 2021-04-13T17:34:47.015100Z

I.e. overwrite your Jetty handler's :middleware

scaturr 2021-04-13T20:36:47.015300Z

Thank you so much. This got me to the correct solution. I may be on a newer/older version - but it was enough to reorder only the relevant pieces:

:duct.handler/root
  {:middleware [#ig/ref :web.middleware/cors
                #ig/ref :duct.middleware.web/not-found
                #ig/ref :duct.middleware.web/format
                #ig/ref :duct.middleware.web/defaults
                #ig/ref :web.middleware/raw-body]}

🎉 1
scaturr 2021-04-13T20:37:22.015500Z

(instead of replacing the entire vector)

scaturr 2021-04-13T20:37:46.015700Z

prep-config shows all the defaults are still present, just reordered 🙂