Lein ring uberjar

3

我建立了一个非常简单的Clojure Web应用程序(实际上是按照教程操作)。项目.clj文件如下所示:

(defproject webdev "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
                :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [ring "1.2.2"]
                 [hiccup "1.0.5"]
                 [compojure "1.1.6"]
                 [org.clojure/java.jdbc "0.3.3"]
                 [postgresql/postgresql "9.1-901.jdbc4"]]

  :plugins [[lein-ring "0.8.10"]]

  :ring {:handler (webdev.core/-main)
         :port 8000}

  :uberjar-name "webdev.jar"

  :main webdev.core

  :aot [webdev.core]

  :profiles {:dev
                 {:main webdev.core/-dev-main}})

我主要的webdev/core.clj文件中相关的部分如下:

(defroutes routes
  (GET "/about" [] about)
  (ANY "/request" [] handle-dump)

  (GET "/items" [] handle-index-items)
  (POST "/items" [] handle-create-item)
  (DELETE "/items/:item-id" [] handle-delete-item)
  (PUT "/items/:item-id" [] handle-update-item)

  (not-found "Page not found."))


(defn wrap-db [hndlr]
  (fn [req]
    (hndlr (assoc req :webdev/db db))))

(defn wrap-server-header [hndlr]
  (fn [req]
    (hndlr (assoc req :webdev/db db))))

(defn wrap-server-response [hndlr]
  (fn [req]
    (let [response (hndlr req)]
      (assoc-in response [:headers "Server:"] "my-server"))))


(def sim-methods {"PUT" :put
                  "DELETE" :delete})

(defn wrap-simulated-methods [hndlr]
  (fn [req]
    (if-let [method (and (= :post (:request-method req))
                         (sim-methods (get-in req [:params "_method"])))]
      (hndlr (assoc req :request-method method))
      (hndlr req))))

(def app
  (wrap-file-info
   (wrap-resource
    (wrap-server-response
     (wrap-db
      (wrap-params
       (wrap-simulated-methods routes)))) "static")))

(defn -main [& [port]]
  (items/create-table db)
  (jetty/run-jetty #'app                     
        {:port (if port (Integer/parseInt port) 
                   (Integer/parseInt (System/getenv "PORT")))}))

(defn -dev-main [port]
  (items/create-table db)
  (jetty/run-jetty (wrap-reload #'app)       {:port (Integer. port)}))

....

如果我在我的项目上运行Lein ring serverLein ring uberjar,会出现以下错误:

java.lang.ClassCastException: clojure.lang.PersistentList 
cannot be cast to clojure.lang.Named

如果我运行Lein run 8000,它会正常工作,并且我可以浏览到localhost:8000。
我的Leiningen版本是:
Leiningen 2.3.4 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM

有人知道出了什么问题吗?

谢谢, Simon

1个回答

3

看起来有些不对劲。

 :ring {:handler (webdev.core/-main)
         :port 8000}

(webdev.core/-main)表示运行您的主函数并将主函数的结果委派为ring-plugin的处理程序。

在使用lein ring时,请尝试使用类似以下内容的东西...

 :ring {:handler webdev.core/app
         :port 8000}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接