Fork me on GitHub
#reitit
<
2021-03-10
>
Jen W12:03:44

Hi, how would I go about adding a custom Last-Modified header to all responses at router level? I’m assuming this should be done at a response coercion level but I’m not sure how to wire it up.

bartuka13:03:27

hi, I have some name-based routing in a project and I would like to add more methods to the same route path. Something like this:

["/add"
  {:name :my-route/list
   :get {:handler ...}}

 {:name :my-route/create
  :post {:handler ..}}]
This snippet does not work, what is the proper way to achieve this? thanks!

dharrigan13:03:12

Hi...like this

dharrigan13:03:16

["" {:get {:handler (search app-config)
               :parameters {:query specs/search}
               :swagger {:produces [poi-search-api-version]}}
         :post {:handler (create app-config)
                :parameters {:body specs/create}
                :swagger {:produces [poi-create-api-version]}}}]

bartuka13:03:13

but I would like to keep the :name key.. On testing I've been using match by name on other endpoints

dharrigan13:03:51

Don't know then

juhoteperi13:03:06

IIRC :name matches just the route, not the method

juhoteperi13:03:51

["" {:name :my-route
         :get {:handler (search app-config)
               :parameters {:query specs/search}
               :swagger {:produces [poi-search-api-version]}}
         :post {:handler (create app-config)
                :parameters {:body specs/create}
                :swagger {:produces [poi-create-api-version]}}}]

(get (match-by-name routes :my-route) ...)
(post (match-by-name routes :my-route) ...)

jmckitrick14:03:54

So I see that `reitit` supports `malli` (of course), and `reitit` supports `swagger`, and `malli` supports `swagger`. But... can I use `malli` on my `reitit` routes and produce `swagger` docs in one fell swoop?

jmckitrick15:03:32

Well, my current codebase will not let me switch to Malli at the moment.

jmckitrick15:03:59

So my question now is about the 'or' specs, such as (s/or :string spec/string? :int spec/number?)

jmckitrick15:03:36

How can I make swagger show the string as the preferred type? The order of the specs does not seem to matter... 'int' appears to take precedence.

ikitommi16:03:15

spec doesn't support metadata, but you could wrap you spec into spec-tools.core/schema , something like (st/schema (s/or ...) {:swagger/type "string"}). Not at computer, so the syntax might be wrong.

ikitommi16:03:14

it's basically a no-op wrapper with support for metadata (among other add-ons)

jmckitrick16:03:36

So that wouldn't require moving away from our current 'data spec' use on endpoints? I'll be adding more spec to other endpoints, but I would like to stay consistent.

ikitommi16:03:31

well, that's one of the root causes for developing malli: data-specs (or plumatic) syntax is good for simple things, but gets messy when one needs to add things like meta-data. You can mix data-specs and specs, but it's two systems instead of one.

ikitommi16:03:03

if there are ideas how to extend data-specs to support metadata without tears, happy to add that.

jmckitrick16:03:16

I'm afraid tears will be part of my job, lol.

jmckitrick16:03:16

I'm looking for the 'schema' function to add metadata, and I'm not seeing it...

jmckitrick16:03:27

Going to browse the ns and keep looking...

ikitommi16:03:43

oh, st/spec

jmckitrick16:03:12

Those look like all predicates....

sfertman16:03:18

Hi 👋! Is there a way to gradually migrate a giant api server built with compojure to reitit routing? I'd like to showcase a small working example within my organization without having to rewrite the world. Thanks 🙏

ikitommi16:03:34

Maybe:

(def compojure-app 
  (c/GET "/old" [] handler)

(def reitit-app
  (rr/ring-handler
    (rr/router
      ["/new" {:get handler}])))

(def app
  (rr/routes compojure-app reitit-app))

ikitommi16:03:53

... then move stuff one by one. I recommend to run a perf test before and after (full migration)

sfertman16:03:46

Oh! Really cool. Thanks; I will try that!

Jen W16:03:23

Hello again, I wonder if anyone’s got any ideas for the response headers Q? (sorry to double post, I’ve just not had any luck looking through the codebase or docs for this) https://clojurians.slack.com/archives/C7YF1SBT3/p1615378184014600

ikitommi17:03:41

@jen you should mount a middleware to all routes for that. It's all ring: all request/response handling is done either in the mw (reusable) or in the handler.

🙇 3
Jen W17:03:27

thanks I’ll go hunt in ring docs rather than reitit then for wiring up examples. Thanks!