2012-02-22 52 views
2

我正在構建由兩個Clojure項目組成的Compojure webapp。第一個是用於可視化一些數據的web應用程序,第二個是需要使用第一個應用程序的更復雜的應用程序。在Compojure中設置路由的上下文路徑

我希望能夠在第二個項目中同時運行兩個應用程序run-jetty。這將允許第二個Web應用程序調用第一個Web應用程序的URL來獲取可視化。

我使用背景從宏觀的Compojure,這裏是第二個應用程序的路徑:

(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})) 

和這裏的第一個應用程序的路徑:

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

我有兩個main.js文件。一個在firstproject /資源/公/ JS /應用/ main.js和secondproject一個 /資源/公/ JS /應用/ main.js

當我瀏覽網址本地主機:8080/first/app/js/main.js我得到第二個項目的main.js。爲什麼?

回答

1

你沒有說你如何組合這些和運行,所以我不知道你的類路徑是如何設置的。但是,您在兩種情況下都具有相同的路由/資源配置,因此它們都會從類路徑中的「公共」內容中提取內容。如果第一個項目/資源和第二個項目/資源都在類路徑上,那麼您將會發生衝突。在這種情況下,看起來第二個項目/資源首先被看到,這就是被拉的東西。

也許你應該將第一和第二個應用程序中的「public」分別改爲「public-first」和「public-second」。然後,您可以在每個上下文中使用適當的資源位置,而不用擔心類路徑。例如:

(route/resources "/" {:root "public-first"}) 
(route/resources "/" {:root "public-second"})