Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run Jetty Example with Ring in Clojure

I am following along with this example on creating a simple web service in Clojure using ring and jetty.

I have this in my project.clj:

(defproject ws-example "0.0.1"
  :description "REST datastore interface."
  :dependencies
    [[org.clojure/clojure "1.5.1"]
     [ring/ring-jetty-adapter "0.2.5"]
     [ring-json-params "0.1.0"]
     [compojure "0.4.0"]
     [clj-json "0.5.3"]]
   :dev-dependencies
     [[lein-run "1.0.0-SNAPSHOT"]])

This in script/run.clj

(use 'ring.adapter.jetty)
(require '[ws-example.web :as web])

(run-jetty #'web/app {:port 8080})

And this in src/ws_example/web.clj

(ns ws-example.web
  (:use compojure.core)
  (:use ring.middleware.json-params)
  (:require [clj-json.core :as json]))

(defn json-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/json"}
   :body (json/generate-string data)})

(defroutes handler
  (GET "/" []
    (json-response {"hello" "world"}))

  (PUT "/" [name]
    (json-response {"hello" name})))

(def app
  (-> handler
    wrap-json-params))

However, when I execute:

lein run script/run.clj

I get this error:

No :main namespace specified in project.clj.

Why am I getting this and how do I fix it?

like image 689
David Williams Avatar asked Apr 05 '13 16:04

David Williams


1 Answers

You're getting this error because the purpose of lein run (according to lein help run) is to "Run the project's -main function." You don't have a -main function in your ws-example.web namespace, nor do you have a :main specified in your project.clj file, which is what lein run is complaining about.

To fix this, you have a few options. You could move the run-jetty code to a new -main function of the ws-example.web function and then say lein run -m ws-example.web. Or you could do that and also add a line :main ws-example.web to project.clj and then just say lein run. Or you could try using the lein exec plugin to execute a file, rather than a namespace.

For more info, check out the Leiningen Tutorial.

like image 105
Jeff Terrell Ph.D. Avatar answered Sep 18 '22 10:09

Jeff Terrell Ph.D.