duct-framework / duct

Server-side application framework for Clojure
MIT License
1.13k stars 51 forks source link

Offer possibility to mix Site-style and API-style handlers #75

Closed vvvvalvalval closed 5 years ago

vvvvalvalval commented 6 years ago

Use case: developing a website which serves several ClojureScript-rich pages, which communicate to web services via AJAX.

=> this induces the need for a configuration which exposes both site-style (à la :duct.module.web/site) and API-style (à la :duct.module.web/api) routes.

In particular, site handlers and API handlers require different middleware stacks, e.g a site requires an HTML-based "Not found" / "stacktrace" response, whereas an API will want to encode that in a data format like Transit or JSON; likewise, an API will want to perform content-type negotiation, whereas a site usually only serves HTML.

The Duct template provides no defaults for this (at the time of writing, lein new duct my-website +site +api is not supported, although it does not throw an error), and I'm not even sure it's possible to achieve via duct.module.web, which seems to affect config globally - making it impossible for 2 strategies to cohabitate. For instance, even creating 3 :duct.core/handler-derived keys (one API, one site, and one root) as in this Gist will not work as is, because the whole middleware stack gets added in 3 all places.

weavejester commented 5 years ago

This is fixed in the latest alpha release.