Fork me on GitHub
#duct
<
2018-04-04
>
lambder08:04:19

how should I do something like this correctly:

(def config 
  {
   :a {}
   
   :b {:x (:x (ig/ref :a))
       :y (:y (ig/ref :a))}})
?

lambder08:04:59

e.g. how to use ig/ref in nested form?

weavejester13:04:07

@lambder sorry, can you elaborate? I’m not sure what you’re doing there.

lambder14:04:19

(:x (ig/ref :a) result in nil (as expected but not desired) I'd like to have the value associated with :x from the the map produced by ig/init :a multimethod call.

lambder14:04:32

@weavejester ^

weavejester15:04:12

@lambder I added a thread reply to your other comment. The easiest way to achieve what you want is with an intermediate key that inits to (:x v) or whatever sub-key you want.

val_waeselynck16:04:31

What would be the most out-of-the-box way of creating a duct project that serves both a cljs-equipped site and an API so that the ClojureScript frontend can exchange data with the backend? I gather from this issue (https://github.com/duct-framework/duct/issues/68) that it is not possible via templates options, correct? My current approach would be to pull apart the prep'ed config, making 2 mid-level handlers each with their own middleware stack and routers, and joining them in a high-level cascading router. That would mean replacing my config with some variation on the expansion of :duct.module/web Is there a better way I'm missing?

weavejester17:04:22

@val_waeselynck You can use :duct.module.web/site and then just add in the :duct.middleware.web/format middleware manually.

val_waeselynck12:04:08

@weavejester it works okay, but requires some more efforts to work soothly - as it is, any API client ends up with an HTML response in case of errors, whereas I'd rather convey a data representation of the error. I'll probably also add a custom error middleware in the API branch.

val_waeselynck13:04:47

Also breaks wrt Anti-forgery token.

val_waeselynck14:04:32

I can't seem to get this "merge 2 handlers into 1 top-level handler" to work, because I'm getting an ambiguous-key error on :duct.core/handler. I'd love to know if you think this is possible at all without completely giving up on the web module.

weavejester14:04:55

You can change the handlers associated with error events in Ataraxy to spit out HTML or data respectively, but you’ll need to check the content type manually. (I think Muuntaja also adds a key?)

weavejester14:04:22

The anti-forgery-token middleware requires a header for AJAX requests. X-CSRF-Token, I believe.

weavejester14:04:14

I don’t understand what you mean by “merge 2 handlers into 1 top-level handler”. That’s what routing is for.

weavejester14:04:23

I’m planning on adding better support for mixed site/api apps, so this should become easier in future.

val_waeselynck15:04:34

@weavejester thanks. > I don’t understand what you mean by “merge 2 handlers into 1 top-level handler”. That’s what routing is for. I know, but I would like to have a routing solution that lets me divide incoming requests into 2 branches (site + api) and apply 2 different middleware stacks on these branches.

weavejester20:04:03

You can nest routes arbitrarily. Routing systems produce larger handlers from a collection of routes linked to smaller ones.

weavejester20:04:37

You can create three :duct.core/handler keys, one to hold the root, one to hold the API, and one to hold the Site.

weavejester20:04:56

Then just have give the route handler two routes: one to the site and one to the API.

val_waeselynck13:04:21

@weavejester Well the error I'm getting (`ExceptionInfo Ambiguous key: :duct.core/handler ...`) seems to imply that it's not possible, because :duct.module/web will reject any config featuring several keys that inherit from :duct.core/handler. Here's a 'repro' config: https://gist.github.com/vvvvalvalval/6ece522a6df394917216e2a8cf1e8786#file-repro-config-edn

weavejester15:04:39

You need to override the handler passed to the web server as well. By default, the web server looks for the :duct.core/handler key, which is where the ambiguous key exception is coming from.

weavejester15:04:21

Something like that should work.

val_waeselynck15:04:21

@weavejester thanks, will try that and come back to you

val_waeselynck15:04:15

@weavejester Hmm the problem now is that the whole middleware stack is applied to the root handler as well as to the children, and I don't know how to prevent that.

val_waeselynck15:04:57

I'm starting to think I'm after something that Duct is not designed to provide in its current version - maybe I should just give up on the web module and go down to integrant

weavejester15:04:55

It might be easier to start from either the :duct.module/web module, or manually connect up the pieces without the module.

weavejester15:04:49

However, please add this issue to the Duct template project (I don’t think there’s one there already).

weavejester15:04:24

I am planning on adding a module already to handle this, but an issue would allow people to give feedback on what they want.

val_waeselynck16:04:11

> It might be easier to start from either the :duct.module/web module, or manually connect up the pieces without the module. Yeah I've tried that too, no luck so far 😞 essentially the issue stays the same.