Question

I'm building a Compojure webapp composed of two Clojure project. The first one is a webapp to visualize some data, the second one is a more complex application which need to use the first one.

I want to be able to run both application at the same time with run-jetty from the second project. This would allow the second webapp to call the URL from the first one to get the visualization.

I'm using the context macro from Compojure, here are the routes for the second app:

(def second-project-main-routes
  [
   (context "/second" request
            (GET "/viewsession" {session :session} (str session))
            (GET "/resetsession" [] (reset-session))
             ... other routes here ...
            (route/resources "/"))
  ])


(def second-all-routes
(concat second-project-main-routes
         first-project-routes)


(def second-app
  (-> (handler/site (apply routes second-all-routes))
      (wrap-base-url)))

;; to comment when building the JAR:
(defonce second-server (run-jetty #'second-app {:join? false :port 8080}))

and here the routes for the first app:

(def first-project-routes
[(context "/first" request
        (GET "/" [] (index-page))
        (route/files "/" {:root (str (System/getProperty "user.dir") "/data/public")})
        (route/resources "/"))])

I have two main.js files. One in firstproject/resources/public/js/app/main.js and one in secondproject/resources/public/js/app/main.js

When I browse the url localhost:8080/first/app/js/main.js I get the main.js of the second project. Why that?

Was it helpful?

Solution

You didn't say how you are combining these and running, so I don't know how your classpath is set up. However, you have the same route/resource configuration on both contexts, so they will both pull content from "public" on your classpath. If both firstproject/resources and secondproject/resources are on the classpath, you are going to have conflicts. In this case, it seems secondproject/resources is seen first, so that's what gets pulled.

Maybe you should change "public" on first and second apps to be "public-first" and "public-second", respectively. Then you use the appropriate resource location to each context without worrying about the classpath. Ex:

(route/resources "/" {:root "public-first"})
(route/resources "/" {:root "public-second"})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top