How to Run Jetty Example with Ring in Clojure
Asked Answered
P

3

6

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?

Perceval answered 5/4, 2013 at 16:26 Comment(2)
The tutorial you linked to uses Leiningen 1.x - you should probably be using lein2.Mandate
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 ClojurePerceval
C
3

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.

Coombs answered 6/3, 2014 at 14:53 Comment(0)
P
2

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)
Pastoralize answered 5/4, 2013 at 16:34 Comment(1)
thanks, do you have a suggestion about the somehwhere? the run-jetty stuff is located in a script called run.clj.Perceval
M
0

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
Mandate answered 5/4, 2013 at 17:27 Comment(1)
This is using lein2. Commands to lein run are slightly different in 1.x.Mandate

© 2022 - 2024 — McMap. All rights reserved.