duct-framework / duct

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

How to use multiple handler components for the same jetty-server? #20

Closed johanhaleby closed 9 years ago

johanhaleby commented 9 years ago

I have two handler-components, a & b, and I like to expose both on the same jetty-server (something similar to how you combine two or more handlers in vanilla compojure using (routes some-handler some-other-handler)). Is this possible?

johanhaleby commented 9 years ago

The reason I want to do this is because I'd like to combine one handler that is protected by basic auth and another handler that is not protected by basic auth in the same jetty-server instance

kendagriff commented 9 years ago

@johanhaleby It's pretty straightforward. Here's an incomplete, but hopefully sufficient example:

(defn endpoint-1 [_]
  (-> (GET ...)
      (wrap-stuff)
      (routes)))

(defn endpoint-2 [_]
  (-> (GET ...)
      (wrap-other-stuff)
      (routes)))

(defn new-system [config]
  (-> (c/system-map
        ...
        :endpoint-1 (endpoint-component endpoint-1)
        :endpoint-2 (endpoint-component endpoint-2))
      (c/system-using
        {:http [:app]
         :app [:endpoint-1 :endpoint-2]
         :endpoint-1 []
         :endpoint-2 []})))
weavejester commented 9 years ago

@kendagriff has it right. You can read more about the handler component on the wiki. The handler component is designed to combine the handlers from multiple endpoints into one.

johanhaleby commented 9 years ago

Thanks a lot, exactly what I need! Sorry I didn't see the wiki, the new github layout confuse me. I'm going to close this.