I suppose this is a strange question to the huge majority of programmers that work daily with Java. I don't. I know Java-the-language, because I worked on Java projects, but not Java-the-world. I never made a web app from scratch in Java. If I have to do it with Python, Ruby, I know where to go (Django or Rails), but if I want to make a web application in Clojure, not because I'm forced to live in a Java world, but because I like the language and I want to give it a try, what libraries and frameworks should I use?
当前回答
Reframe和om。接下来可能就是你要找的了。
其他回答
Compojure is no longer a complete framework for developing web applications. Since the 0.4 release, compojure has been broken off into several projects. Ring provides the foundation by abstracting away the HTTP request and response process. Ring will parse the incoming request and generate a map containing all of the parts of the request such as uri, server-name and request-method. The application will then handle the request and based on the request generate a response. A response is represented as a map containing the following keys: status, headers, and body. So a simple application would look like:
(def app [req]
(if (= "/home" (:uri req))
{:status 200
:body "<h3>Welcome Home</h3>"}
{:status 200
:body "<a href='/home'>Go Home!</a>"}))
One other part of Ring is the concept of middle-ware. This is code that sits between the handler and the incoming request and/or the outgoing response. Some built in middle-ware include sessions and stacktrace. The session middle-ware will add a :session key to the request map that contains all of the session info for the user making the request. If the :session key is present in the response map, it will be stored for the next request made by the current user. While the stack trace middle-ware will capture any exceptions that occur while processing the request and generate a stack trace that is sent back as the response if any exceptions do occur.
直接使用Ring可能会很乏味,所以Compojure是建立在Ring之上的,将细节抽象出来。应用程序现在可以用路由来表达,所以你可以有这样的东西:
(defroutes my-routes
(GET "/" [] "<h1>Hello all!</h1>")
(GET "/user/:id" [id] (str "<h1>Hello " id "</h1>")))
Compojure仍然在使用请求/响应映射,所以你可以在需要的时候访问它们:
(defroutes my-routes
(GET "*" {uri :uri}
{:staus 200 :body (str "The uri of the current page is: " uri)}))
在这种情况下,{uri:uri}部分访问请求映射中的:uri键,并将uri设置为该值。
最后一个组件是Hiccup,它使生成html更容易。各种html标记被表示为向量,第一个元素表示标记名,其余元素是标记体。"<h2>A header</h2>"变为[:h2 "A header "]。标记的属性位于可选映射中。"<a href='/login'>登录页面</a>"变为[:a {:href "/login"} "登录页面"]。下面是一个使用模板生成html的小示例。
(defn layout [title & body]
(html
[:head [:title title]]
[:body [:h1.header title] body]))
(defn say-hello [name]
(layout "Welcome Page" [:h3 (str "Hello " name)]))
(defn hiccup-routes
(GET "/user/:name" [name] (say-hello name)))
这里是compojure作者目前正在编写的一些文档的草稿的链接,您可能会发现这些文档很有用:compojure Doc
我已经成功地在生产环境中使用Liberator一段时间了。如果你只是想要一个基本的框架,例如,如果你正在构建一个RESTful web服务或类似的东西,它是一个很好的框架。它本质上是ring和compojure的包装器,并在验证传入请求时提供决策图。与其他更笨重的web框架相比,它的速度也非常快。如果你想快速开始,然后慢慢建立,那么解放者是一个很好的选择。
还有“Noir”(http://www.webnoir.org/),这是一个新的Clojure web框架(太新了,文档还没有)。来自Django/Rails,我喜欢简单、直接的语法,它非常精简。
我现在常用的网络图书馆是yada。
如果您刚刚开始,介绍性的服务器是Compojure。我认为它是Clojure世界中web服务器的apache(在这种情况下,yada/aleph将是nginx)。你可以用鲁米努斯做模板。它有很多变体,比如compojure-api。
我尝试了欧台座,并对它感到满意。我不敢说已经掌握了它,但是它的语法很好,感觉很有凝聚力,而且看起来确实有很好的性能。它也得到了Cognitect (Rich Hickey工作的Clojure/Datomic公司)的支持。
我发现Aleph呈现了一个有趣的抽象,内置的反压力似乎很有趣。我还没有玩过它,但它绝对在我的清单上。
在尝试了不同的网络服务器之后,以下是我的优缺点列表:
简短的回答:看看Luminus来快速开始,也许会随着你的需求发展而转向其他东西(也许Yada)。
Compojure
优点(1): 简单,大量的模板/示例(例如,发光) 缺点(2): 不是高性能的(每个请求一个线程),期望性能略好于rails 不简单,中间件模型有不便之处
基座
优点(3): 拦截器模型,将拦截器添加到路由子集的愉快语法 高性能路由器 支持json/transit/multipart表单透明开箱即用,不需要任何要求。非常酷! 缺点(4): 没有websocket支持(目前),返回核心。异步通道会很好 如果把它放在Stuart Sierra的组件中,重载有点慢(我认为你应该使用重载拦截器) 没有异步拦截器的测试工具 需要接受(?)
阿莱
优点 (3):
性能 反压力 Websocket/SSE支持返回流形流
缺点(1):
低水平,做自己的风格(即。它只是给了你一种方法让你的处理者做点什么。没有路由器,什么都没有)。并不是真正的缺点,只是要注意。
Yada
优点 (3):
建在阿莱夫的基础上 内容协商的 时髦的集成 bidi很好(尽管我更喜欢基座路由器语法)
缺点(1):
文档(虽然不像nginx-clojure那样糟糕,但正在迅速改进)。
HttpKit
优点 (2):
用Clojure编写!(和Java…) 性能看起来不错(请参阅600K并发连接的文章)
缺点(2):
不支持CORS 错误吗?而且,最近的提交也不多
Nginx-Clojure
注意:我没有使用它,主要是因为缺少文档。它看起来很有趣,而且性能非常好。
优点(2):
Nginx (performance, offload ssl, restart workers…) 这个模型是否允许零停机更新?那太棒了!
缺点(1):
文档(改善)。此外,我不想在nginx配置文件中嵌入字符串,如果这是唯一的方法。 第一次部署可能会有点复杂(?)
Immutant
注:我没有玩过它。
优点:
集成(缓存、消息传递、调度、wildfly部署)
缺点:
没有HTTP客户端
卡塔昆巴
注意:我还没有使用它,尽管文档看起来很棒。我接下来可能会试试。有一些聊天项目的例子看起来很有趣,它们对协议的大量使用起初让我这个Clojure开发新手望而却步。
优点(6):
文档!像所有的funcool项目一样,这个文档读起来很舒服。 类底座路由语法 应该是高性能的(在鼠包之上) 反压力 Websockets, sse, cors, security, ssl… 特色挖掘:邮政
缺点(2):
不完全确定ct/routes语法是多么令人愉快,以及关于放弃环规范(据说是异步的故事,但我认为基座的家伙固定) 不知道如何整合招摇过市等。 当我尝试它时,我不能让它立即工作
注意:如果原始性能是最重要的,Clojure web服务器的基准测试是可用的。
你也可以看看下面这些框架(摘自clojure/projects):
级联 召唤
关于Stack Overflow还有一个相关的问题:成熟的Clojure web框架?