6
votes

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?

3
The tutorial you linked to uses Leiningen 1.x - you should probably be using lein2.Alex
It would be great if I could find a tutorial that just worked since I'm learning. Any suggestions? I want to create a web service in ClojureDavid Williams

3 Answers

3
votes

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.

2
votes

You have to put that (run-jetty) stuff into a -main somewhere and then add it to the project.clj like

:main ws-example.core)
0
votes

From lein help run:

USAGE: lein run -m NAMESPACE[/MAIN_FUNCTION] [ARGS...]
Calls the main function in the specified namespace.

So, you would need to put your script.clj somewhere on the project source path and then call it as:

lein run -m script