2013-04-05 76 views
5

我正在关注使用ring和jetty在Clojure中创建简单Web服务的this example如何在Clojure中使用Ring运行Jetty示例

我有这个在我的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"]]) 

这在脚本/ run.clj

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

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

这将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)) 

但是,当我执行时:

lein run script/run.clj 

我得到这个错误:

No :main namespace specified in project.clj. 

为什么会出现这个?如何解决?

+0

您链接的教程使用Leiningen 1.x - 您应该使用lein2。 – Alex 2013-04-05 17:29:44

+0

如果我能找到一个自从学习以来就能工作的教程,那将是非常棒的。有什么建议么?我想在Clojure – 2013-04-05 17:33:38

回答

2

你必须把那(run-jetty)东西成-main的地方,然后把它添加到project.clj

:main ws-example.core) 
+0

中创建一个Web服务谢谢,你有关于某处的建议吗? run-jetty的东西位于一个名为run.clj的脚本中。 – 2013-04-05 17:22:39

0

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

这是使用lein2。在lex run中的命令在1.x中略有不同。 – Alex 2013-04-05 17:31:16

3

由于lein run(根据lein help run)的目的是“运行项目的主函数”,所以出现此错误。您的ws-example.web命名空间中没有-main函数,您的project.clj文件中也没有指定:main,这是lein run所抱怨的内容。

要解决这个问题,你有几个选择。您可以将run-jetty代码移至ws-example.web函数的新-main函数,然后再说lein run -m ws-example.web。或者你可以这样做,也可以添加一行:main ws-example.webproject.clj,然后再说lein run。或者您可以尝试使用lein exec plugin来执行文件,而不是命名空间。

欲了解更多信息,请查看Leiningen Tutorial