Fork me on GitHub
#duct
<
2017-12-22
>
flowthing08:12:55

So I have a pretty basic question… I have :duct.module.web/site {} in my config.edn, but I want to define many handlers that return JSON. I assume there's some way to declare that a handler should return JSON in config.edn without me having to do JSON conversion in the handler explicitly, but I can't seem to figure out how to do that.

snichme09:12:32

I’ve had the same issue, not sure how to do it, but know that @weavejester has mentioned that he is looking at a site + api web module. I’m waiting for that

flowthing09:12:08

OK, good to know. Thanks for the info.

rickmoynihan12:12:24

@flowthing: Not sure there’s support for that just yet… You can do something like this:

rickmoynihan12:12:28

but the config is going to get unmanageable without writing a module to help make the pain go away

rickmoynihan12:12:19

but there could already be a way to do it that I don’t know about

flowthing12:12:00

@rickmoynihan: Thanks! I'll give that a shot when I get the chance.

rickmoynihan12:12:50

You might want a module that takes a list of handlers and wraps them with the given middleware… there might be something in duct.web to do this already though

rickmoynihan12:12:41

though if you have two sets of middlewares i.e. if you want both :site and :api I think that might not be possible… they’ll probably stomp each other.

flowthing12:12:49

Yeah, was just about to say.

flowthing12:12:18

If I enable the :api middleware, then routes that return e.g. JS or CSS break.

rickmoynihan12:12:24

yeah I think the problem is they use the same :middleware key

rickmoynihan12:12:46

I think a module that let you wrap your own middlewares over specific handlers would be a good thing.

rickmoynihan12:12:53

i.e. a module that would work on something like this:

flowthing12:12:11

Something like that would certainly fit the bill for me.

flowthing21:12:43

FWIW, adding these bits into my config.edn seemed to do the trick:

:duct.core/handler                {:middleware [#ig/ref :duct.middleware.web/not-found
                                                 #ig/ref :duct.middleware.web/format
                                                 #ig/ref :duct.middleware.web/defaults]}

 :duct.middleware.web/format       {}