3
votes

I've built a very simple web app in Clojure (in fact I followed a tutorial). The project.clj file looks like this:

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

The relevant parts of my main webdev/core.clj look like this:

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

....

If I run Lein ring server or Lein ring uberjar on my project I get the following error:

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

If I run Lein run 8000. It works correctly and I can browse to localhost:8000

My Leiningen version is:

Leiningen 2.3.4 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM

Anyone know what's wrong?

Regards, Simon

1

1 Answers

3
votes

That's looking wrong.

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

(webdev.core/-main) means running your main function and delegating the result of the main Function as the handler to the ring-plugin.

Try using something more like this when using lein ring ...

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