Compojure - 如何从一个函数启动服务器?
Compojure - how to start server from a function?
我正在编写一个玩具应用程序来了解 Compojure,并将其用于数据库支持的 Web 应用程序。
我知道,如果我使用 lein ring uberjar
编译,我可以创建一个在登录时自动启动服务器的 uberjar。现在我想尝试一个多功能 .jar 文件,想法是在启动 jar 时我可以决定是要进行数据库管理还是启动服务器。
在我的 core.clj 中,我通过 defroutes
定义了一些路由,并在 project.clj 在 :ring {:handler ...}
.
下
我现在的问题是:我怎样才能从一个函数启动环形服务器,同时尽可能少地依赖和代码?
This issue 有从 -main
函数启动服务器的示例,但是使用了太多我无法解决的依赖项,一些没有解释的神秘函数,并且几乎肯定会过时两年了。
我在 Compojure 文档和 wiki 中找不到任何提示,也欢迎指向 docs/tuts 的指针。
编辑: 工作版本,来自 schaueho 的回答和戒指教程:
(ns playground.core
(:require [ring.adapter.jetty :refer :all]
[compojure.core :refer :all]
[compojure.route :as route]))
(defroutes app-routes
(GET "/" [query]
(do (println "Server query:" query)
"<p>Hello from compojure and ring</p>"))
(route/resources "/")
(route/not-found "<h1>404 - Page not found</h1>"))
(run-jetty app-routes {:port 8080 :join? false})
出于某种原因,在我重新启动 REPL 之前调用 run-jetty
会给我 ClassNotFoundExceptions 和完全相同的代码。我猜是受污染的命名空间阻止了它的工作。
查看环文档的 Getting started 部分,了解使用 Jetty 的示例(就像 lein ring
一样)。
您可以在 -main
函数中删除对 run-jetty
的调用。 Compojure 路由充当处理程序,因此您可以将它们作为调用 run-jetty
的参数删除(如 (jetty/run-jetty main-routes port)
.
我正在编写一个玩具应用程序来了解 Compojure,并将其用于数据库支持的 Web 应用程序。
我知道,如果我使用 lein ring uberjar
编译,我可以创建一个在登录时自动启动服务器的 uberjar。现在我想尝试一个多功能 .jar 文件,想法是在启动 jar 时我可以决定是要进行数据库管理还是启动服务器。
在我的 core.clj 中,我通过 defroutes
定义了一些路由,并在 project.clj 在 :ring {:handler ...}
.
我现在的问题是:我怎样才能从一个函数启动环形服务器,同时尽可能少地依赖和代码?
This issue 有从 -main
函数启动服务器的示例,但是使用了太多我无法解决的依赖项,一些没有解释的神秘函数,并且几乎肯定会过时两年了。
我在 Compojure 文档和 wiki 中找不到任何提示,也欢迎指向 docs/tuts 的指针。
编辑: 工作版本,来自 schaueho 的回答和戒指教程:
(ns playground.core
(:require [ring.adapter.jetty :refer :all]
[compojure.core :refer :all]
[compojure.route :as route]))
(defroutes app-routes
(GET "/" [query]
(do (println "Server query:" query)
"<p>Hello from compojure and ring</p>"))
(route/resources "/")
(route/not-found "<h1>404 - Page not found</h1>"))
(run-jetty app-routes {:port 8080 :join? false})
出于某种原因,在我重新启动 REPL 之前调用 run-jetty
会给我 ClassNotFoundExceptions 和完全相同的代码。我猜是受污染的命名空间阻止了它的工作。
查看环文档的 Getting started 部分,了解使用 Jetty 的示例(就像 lein ring
一样)。
您可以在 -main
函数中删除对 run-jetty
的调用。 Compojure 路由充当处理程序,因此您可以将它们作为调用 run-jetty
的参数删除(如 (jetty/run-jetty main-routes port)
.