clojureringcompojurehttp-kit

Registering multiple handlers while running server


Is there any way to register multiple handlers while running an http-kit server:

(defroutes rest-main-app
  (GET "/" "Welcome"))

(defroutes rest-events-app
  (GET "/events" "Event API"))

(defn -main []
  (run-server rest-main-app {:port 5000}))

How can I pass both routes to the run-server e.g both rest-main-app and rest-events-app ?


Solution

  • You can use compojure's routes function. You can also pass several handlers to defroutes, an example is provided below:

    (defroutes get-routes
      (GET "/events" [] "Event API")
      (GET "/" [] "Welcome"))
    
    (defroutes post-routes
      (POST "/events" [] "Post Event API"))
    
    (def all-routes 
      (routes
       get-routes
       post-routes))
    
    (defn -main []
      (run-server all-routes {:port 5000}))