How to run a Jetty example with a ring in Clojure

I follow along with this example to create a simple web service in Clojure with a bell and a jetty.

I have this in my .clj project:

(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 is in script / run.clj

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

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

And this is 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 to fix it?

+5
source share
3 answers

You get this error because the goal lein run(in accordance with lein help run) is to "Run the main function of the project." You do not have a function -mainin your namespace ws-example.web, and you do not have what is :mainindicated in your file project.cljabout which it complains lein run.

, . run-jetty -main ws-example.web, lein run -m ws-example.web. , :main ws-example.web project.clj, lein run. lein exec , .

Leiningen Tutorial.

+3

(run-jetty) -main -, project.clj

:main ws-example.core)
+2

lein help run:

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

, script.clj - , :

lein run -m script
0

All Articles