load-views when running multiple noir servers

183 views Asked by At

I'm experimenting with using noir to start three servers (each to handle a different aspect of the application). I am trying to do this so that I can run all three servers within one application while developing and easily decouple the project into three different applications for deployment.

It is no problem to use noir.server/start and noir.server/stop to run the jetty servers I need.

What I'm trying to figure out is some way to call load-views (or something like that) with a different set views for each server so that URI conflicts are handled by the correct defpage.

1

There are 1 answers

0
Roth Michaels On BEST ANSWER

I found a solution that works by clearing noir.core/noir-routes, noir.core/route-funcs, noir.core/pre-routes, and noir.core/post-routes. I have an example below of a namespace that starts three servers all with their own views.

(ns my-three.server
  (:require
   [noir.core :as noir] 
   [noir.server :as server]))

(def ^:dynamic *servers* (atom {}))

(defn clear-routes []
  (do
    (reset! noir/noir-routes {})
    (reset! noir/route-funcs {})
    (reset! noir/pre-routes (sorted-map))
    (reset! noir/post-routes [])
    nil))

(defn start-server [port mode]
  (if-let [s (@*servers* port)]
    (println "Server already running on port" port)
    (swap! *servers* assoc port (server/start port {:mode mode}))))

(defn stop-server [port]
  (let [s (@*servers* port)]
    (server/stop s)
    (swap! *servers* dissoc port)))

(defn stop-all []
  (doall (map stop-server (keys @*servers*))))


(defn -main [& m]
  (let [mode (if (nil? (first m)) :prod (first m))]
    (server/load-views "src/my_three/views/web/" )
    (start-server 8080 mode)
    (clear-routes)
    (server/load-views "src/my_three/views/mobile_downstream/")
    (start-server 8081 mode)
    (clear-routes)
    (server/load-views "src/my_three/views/mobile_upstream/")
    (start-server 8082 mode)))