duct

Dave Simmons 2020-03-28T08:55:19.000600Z

Hi All

Dave Simmons 2020-03-28T08:56:40.002Z

Does anyone have an example of a Duct project that allows both secure and insecure routes. I know you have have a route specific middleware which I think would do this but is there a way to group routes together for specific middleware (rather than a route by route basis)?

Dave Simmons 2020-03-28T08:56:46.002200Z

many thanks

kwrooijen 2020-03-28T09:02:25.005Z

If you have the routes v1/secure And v1/insecure You could place the specific Middleware on secure and insecure routes. The shared Middleware could go on v1. Not sure if that's what you mean though

Dave Simmons 2020-03-28T09:11:38.006600Z

Hi @kevin.van.rooijen - would I then be able to define v1/secure/foo v1/secure/boo ? not quite sure how that works in config.edn.

Dave Simmons 2020-03-28T09:16:26.007700Z

I'm new to ataraxy so I'm assuming you can define a route with various middleware and have subroutes "inherit" that middleware?

Dave Simmons 2020-03-30T18:02:28.009600Z

Hi @kevin.van.rooijen - a follow up question if I may. Where or how would I define multiple middleware to call. In the example above I'd like :middleware/global to call middleware-1 and middleware-2. Is that possible in EDN or would I have to define that in my-app-middleware-global ig/init to return multiple middleware functions? (hope this makes sense :-))

kwrooijen 2020-03-30T18:04:00.009800Z

You can replace ^:middleware/global with

^{:middleware/global true
  :middleware/global2 true}

kwrooijen 2020-03-30T18:04:15.010Z

^:some-key is shorthand for ^{:some-key true}

kwrooijen 2020-03-30T18:04:21.010200Z

It being just metadata in Clojure

kwrooijen 2020-03-30T18:05:42.010400Z

Note however that middleware take order based on name. So if order matters, you'll either have to name them in an ordered way, or you have to create your own middleware init-key that combines multiple middlewares in order

Dave Simmons 2020-03-30T18:22:56.010600Z

Ah thanks - especially about the name order.

kwrooijen 2020-03-30T18:23:38.010800Z

iirc it's backwards. so :z/my-middleware will eval before :a/some-middleware

kwrooijen 2020-03-30T18:23:50.011Z

But I might be wrong

Dave Simmons 2020-03-30T18:24:41.011200Z

I quite like the explicitness of

Dave Simmons 2020-03-30T18:25:03.011400Z

:routes {[:get "/foo"] ^:middleware/global ^:middleware/insecure {[:get "/boo"] ^:middleware/secure [:middle.handler.middle/index]}}}

Dave Simmons 2020-03-30T18:25:42.011600Z

I know the example here doesn't make sense but it was just as an illustration.

kwrooijen 2020-03-30T18:26:13.011800Z

Yeah it's an interesting way of tackling this issue

Dave Simmons 2020-03-30T18:28:35.012Z

Do you know if there is a "clojure" or "duct" best practise for this - would it be better to return multiple middleware in the ig/init function definition - my problem with this is that it kind of hides the intent of the EDN.

kwrooijen 2020-03-30T18:29:17.012200Z

This isn't really a duct or clojure thing. It's pure ataraxy

kwrooijen 2020-03-30T18:29:53.012400Z

And I'm not sure how to solve this properly. We use the namespace as a prefix. So :aaa/middleware1 :bbb/middleware2 etc

Dave Simmons 2020-03-30T18:30:09.012600Z

Ah right. I must confess I think I prefer reitit to ataraxy so might have a play with that and duct.

Dave Simmons 2020-03-30T18:31:25.012800Z

All the same thanks very much for your help on this.

kwrooijen 2020-03-30T18:31:35.013Z

There's a duct-reitit implementation, you could take a look at that https://github.com/yannvanhalewyn/duct-reitit

Dave Simmons 2020-03-30T18:32:21.013300Z

fantastic - I was thinking to myself - how the hell am I going to implement reitit in duct. Perfect!

kwrooijen 2020-03-28T09:33:04.008200Z

Something like this I think

Something like this I think,
:duct.router/ataraxy
{:middleware {:middleware/global #ig/ref :my-app.middleware/global
              :middleware/secure #ig/ref :my-app.middleware/secure
              :middleware/insecure #ig/ref :my-app.middleware/insecure}
 :routes
 {"/v1" 
  ^:middleware/global
  {"/secure"
   ^:middleware/secure
   {"/foo" [:my-app.handler/secure-foo]}

   "/insecure"
   ^:middleware/insecure
   {"/boo" [:my-app.handler/insecure-boo]}}}}

kwrooijen 2020-03-28T09:33:28.008400Z

Ah I switched the boo / foo, but you get the idea

kwrooijen 2020-03-28T09:34:03.008600Z

Basically anything under /v1 will use :middleware/global.

kwrooijen 2020-03-28T09:34:22.008800Z

anything under /secure will use :middleware/secure. anything under /insecure will use :middleware/insecure.

Dave Simmons 2020-03-28T09:40:37.009Z

@kevin.van.rooijen - perfect - many thanks for this. Appreciate the help.

kwrooijen 2020-03-28T09:43:36.009200Z

You're very welcome 😄