你如何在Clojure中创建一个Web应用程序?

时间:2023-01-02 22:01:02

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?

我想这对于每天使用Java工作的绝大多数程序员来说都是一个奇怪的问题。我不。我知道Java-the-language,因为我从事过Java项目,但不是Java-the-world。我从未在Java中从头开始创建Web应用程序。如果我必须使用Python,Ruby,我知道去哪里(Django或Rails),但如果我想在Clojure中创建一个Web应用程序,不是因为我*生活在Java世界中,而是因为我喜欢这种语言,我想尝试一下,我应该使用哪些库和框架?

16 个解决方案

#1


101  

By far the best Clojure web framework I have yet encountered is Compojure: http://github.com/weavejester/compojure/tree/master

到目前为止,我遇到的最好的Clojure Web框架是Compojure:http://github.com/weavejester/compojure/tree/master

It's small but powerful, and has beautifully elegant syntax. (It uses Jetty under the hood, but it hides the Servlet API from you unless you want it, which won't be often). Go look at the README at that URL, then download a snapshot and start playing.

它体积小但功能强大,语法优美。 (它在引擎盖下使用Jetty,但除非你需要它,否则它会隐藏你的Servlet API,这不常见)。去查看该URL的README,然后下载快照并开始播放。

#2


178  

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:

Compojure不再是开发Web应用程序的完整框架。自0.4发布以来,compojure已被分解为几个项目。 Ring通过抽象出HTTP请求和响应过程来提供基础。 Ring将解析传入的请求并生成包含请求的所有部分的映射,例如uri,server-name和request-method。然后,应用程序将处理该请求,并根据请求生成响应。响应表示为包含以下键的映射:status,headers和body。所以一个简单的应用程序看起来像:

(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的另一部分是中间件的概念。这是处理程序与传入请求和/或传出响应之间的代码。一些内置的中间件包括会话和堆栈跟踪。会话中间件将向请求映射添加:会话密钥,其中包含发出请求的用户的所有会话信息。如果:会话密钥存在于响应映射中,则它将被存储以用于当前用户发出的下一个请求。堆栈跟踪中间件将捕获处理请求时发生的任何异常,并生成堆栈跟踪,如果发生任何异常,则将其作为响应发回。

Working directly with Ring can be tedious, so Compojure is built on top of Ring abstracting away the details. The application can now be expressed in terms of routing so you can have something like this:

直接使用Ring可能会很乏味,因此Compojure构建在Ring之上,抽象出细节。现在可以用路由表示应用程序,这样你就可以得到类似的东西:

(defroutes my-routes
  (GET "/" [] "<h1>Hello all!</h1>")
  (GET "/user/:id" [id] (str "<h1>Hello " id "</h1>")))

Compojure is still working with the request/response maps so you can always access them if needed:

Compojure仍然使用请求/响应映射,因此您可以随时访问它们:

(defroutes my-routes
  (GET "*" {uri :uri} 
           {:staus 200 :body (str "The uri of the current page is: " uri)}))

In this case the {uri :uri} part accesses the :uri key in the request map and sets uri to that value.

在这种情况下,{uri:uri}部分访问请求映射中的:uri键并将uri设置为该值。

The last component is Hiccup which makes generating the html easier. The various html tags are represented as vectors with the first element representing the tag name and the rest being the body of the tag. "<h2>A header</h2>" becomes [:h2 "A Header"]. The attributes of a tag are in an optional map. "<a href='/login'>Log In Page</a>" becomes [:a {:href "/login"} "Log In Page"]. Here is a small example using a template to generate the html.

最后一个组件是Hiccup,它使生成html更容易。各种html标记表示为向量,第一个元素表示标记名称,其余元素是标记的主体。 “

标题 ”变为[:h2“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)))

Here is a link to a rough draft of some documentation currently being written by the author of compojure that you might find helpful: Compojure Doc

以下链接指向当前由compojure作者撰写的一些文档的草稿,您可能会发现这些文档很有帮助:Compojure Doc

#3


48  

There's also "Noir" (http://www.webnoir.org/), which is a new Clojure web framework (so new the docs aren't there yet). Coming from Django/Rails, I dig the simple, straightforward syntax and it's pretty lean.

还有“Noir”(http://www.webnoir.org/),这是一个新的Clojure Web框架(所以新的文档还没有)。来自Django / Rails,我挖掘了简单,直接的语法,它非常精简。

#4


24  

Consider the Luminus web framework. I have no affiliation but have heard good things from friends I respect.

考虑Luminus Web框架。我没有任何从属关系,但我听到了我尊重的朋友的好话。

#5


20  

My current go-to web library is now yada.

我现在的网络图书馆现在是yada。

If you are just starting out, the introductory server is Compojure. I see it as the apache of web servers in the Clojure world (in which case yada/aleph would be nginx). You could use Luminus as a template. There are variants of it, like compojure-api.

如果您刚刚开始,介绍服务器是Compojure。我认为它是Clojure世界中Web服务器的apache(在这种情况下,yada / aleph将是nginx)。你可以使用Luminus作为模板。它有变种,比如compojure-api。

I tried ou Pedestal and was globally satisfied with it. I don't claim to master it, but it has a pleasant syntax, feels very cohesive, and looks like it does have great performance. It is also backed by Cognitect (the Clojure/Datomic company where Rich Hickey works).

我试过ou Pedestal并对它感到满意。我并没有声称掌握它,但它具有令人愉快的语法,感觉非常有凝聚力,并且看起来确实具有很好的性能。它也得到了Cognitect(Rich Hickey工作的Clojure / Datomic公司)的支持。

I found Aleph to present an interesting abstraction, and the built-in backpressure seems interesting. I have yet to play with it, but it's definitely on my list.

我发现Aleph呈现了一个有趣的抽象,内置的背压似乎很有趣。我还没有玩它,但它肯定在我的名单上。

After playing a bit with various web servers, here is my quick Pro/Cons list :

在使用各种Web服务器播放之后,这是我的快速Pro / Cons列表:

Short answer : have a look at Luminus to get started quickly, maybe move on to something else as your needs evolve (Yada maybe).

简短的回答:看看Luminus快速入门,或许随着你的需求的发展转向别的东西(Yada也许)。

Compojure

  • Pros (1):

    • easy, lots of templates/examples (ex. Luminous)
    • 简单,很多模板/示例(例如Luminous)

  • 优点(1):简单,很多模板/示例(例如Luminous)

  • Cons (2):

    • Not performant (a thread per request), expect performances slightly better than rails
    • 性能不高(每个请求一个线程),期望性能略好于rails

    • Not simple, the middleware model has inconvenients
    • 不简单,中间件模型有不便之处

  • 缺点(2):性能不高(每个请求一个线程),期望性能稍好于rails不简单,中间件模型有不便之处

Pedestal

  • Pros (3):

    • interceptor model, pleasant syntax to add interceptors to a subset of routes
    • 拦截器模型,将拦截器添加到路径子集的令人愉快的语法

    • performant router
    • supports json/transit/multipart forms transparently out of the box, without asking anything. Very cool !
    • 支持json / transit / multipart表单,开箱即用,无需任何询问。很酷 !

  • 优点(3):拦截器模型,令人愉快的语法,将拦截器添加到路由的子集中,性能路由器支持json / transit / multipart形式,开箱即用,无需任何询问。很酷 !

  • Cons (4):

    • no websocket support (yet), returning core.async channels would be nice
    • 没有websocket支持(还),返回core.async频道会很好

    • a bit slow to reload if putting it in a Stuart Sierra's component (I think you are supposed to use the reload interceptor)
    • 将它放入Stuart Sierra的组件中时重新加载有点慢(我认为你应该使用重载拦截器)

    • no testing facility for async interceptors
    • 没有异步拦截器的测试设施

    • requires buy-in (?)
    • 需要买入(?)

  • 缺点(4):没有websocket支持(还),如果把它放在Stuart Sierra的组件中(我认为你应该使用重载拦截器)没有测试设备用于异步,返回core.async通道会很慢重新加载拦截器需要买入(?)

Aleph

Pro (3):

  • Performant
  • backpressure
  • Websocket/SSE support when returning a manifold stream
  • 返回流形流时支持Websocket / SSE

Cons (1):

  • Low level, do it yourself style (ie. it just gives you a way to make your handlers do something. No router, no nothing). Not really a cons, just be aware of it.
  • 低级别,自己动手(即它只是让你有办法让你的处理程序做一些事情。没有路由器,没有任何东西)。不是一个缺点,只是意识到它。

Yada

Pro (3):

  • built on Aleph
  • 建立在Aleph上

  • content negociation
  • swagger integration
  • bidi is quite ok (though I like pedestal router syntax better)
  • bidi相当不错(虽然我更喜欢基座路由器语法更好)

Cons (1):

  • documentation (although not as bad as nginx-clojure, quickly improving).
  • 文档(虽然没有nginx-clojure那么糟糕,但很快就会改进)。

HttpKit

Pro (2):

  • Written in Clojure ! (and Java...)
  • 写在Clojure! (和Java ...)

  • performance looks good (see the 600K concurrent connections post)
  • 性能看起来不错(参见600K并发连接帖)

Cons (2):

  • No CORS support
  • 没有CORS支持

  • Bugs ? Also, not a lot of recent commits
  • 臭虫?此外,最近提交的不是很多

Nginx-Clojure

Note : I haven't played with it, mainly because of the lack of documentation. It looks interesting though, and very performant.

注意:我没有玩过它,主要是因为缺少文档。它虽然看起来很有趣,但性能非常好。

Pros (2):

  • Nginx (performant, offload ssl, restart workers...)
  • Nginx(高性能,卸载ssl,重启工作者......)

  • Could this model allow zero-downtime updates ? That would be so awesome !
  • 该模型是否允许零停机更新?那真是太棒了!

Cons (1):

  • Documentation (improving). Also, I don't want to program in strings embedded in an nginx config file if that is the only way to do it.
  • 文件(改进)。另外,我不想在嵌入在nginx配置文件中的字符串中编程,如果这是唯一的方法。

  • Probably complicates a bit the first deployment (?)
  • 可能会使第一次部署(?)复杂化

Immutant

Note : I haven't played with it.

注意:我没玩过它。

Pros :

  • integrated (caching, messaging, scheduling, wildfly deploy)
  • 集成(缓存,消息传递,调度,野生动物部署)

Cons :

  • no http client
  • 没有http客户端

Catacumba

Note : I haven't played with it, although the documentation looks excellent. I am probably going to try it next. There are example chat projects that look interesting, their heavy use of protocols put me off at first as a novice Clojure dev.

注意:尽管文档看起来很棒,但我还没有玩过它。我可能会接下来尝试一下。有一些示例聊天项目看起来很有趣,他们大量使用协议使我最初成为新手Clojure dev。

Pros (6):

  • documentation ! Like all funcool projects, the doc is very pleasant to read.
  • 文档!像所有funcool项目一样,该文档阅读起来非常愉快。

  • pedestal-like routing syntax
  • 类似基座的路由语法

  • should be performant (on top of Ratpack)
  • 应该是高性能的(在Ratpack之上)

  • backpressure
  • websockets, sse, cors, security, ssl...
  • websockets,sse,cors,security,ssl ...

  • unique features to dig : postal
  • 独特的挖掘功能:邮政

Cons (2):

  • Not completely sure about how pleasant the ct/routes syntax is, and about ditching the Ring spec (supposedly for the async story, but I thought the pedestal guys fixed that)
  • 不完全确定ct / routes语法是多么令人愉快,以及放弃Ring规范(据说是异步故事,但我认为基座的人修复了这个)

  • Not sure how one would integrate swagger etc.
  • 不确定如何整合招摇等。

  • when I tried it, I was not able to make it work straight away
  • 当我尝试它时,我无法立即使用它

Note : a benchmark of Clojure web servers is available, if raw performance is all that matters.

注意:如果原始性能非常重要,可以使用Clojure Web服务器的基准测试。

#6


14  

These days Pedestal is a framework worth a look. It's a server-side framework that builds on top of Ring, but also frees the incoming request from the initial thread by being able to pause and resume that particular request (otherwise a slow request actually block that serverthread). Maybe sort of like a JavaBean.

这些天Pedestal是一个值得一看的框架。它是一个构建在Ring之上的服务器端框架,但也可以通过暂停和恢复该特定请求来释放来自初始线程的传入请求(否则慢速请求实际阻止该serverthread)。也许有点像JavaBean。

Other cool frameworks are hoplon.io and David Nolen's Om (based on React)

其他很酷的框架是hoplon.io和David Nolen的Om(基于React)

#7


11  

Webjure, a web programming framework for Clojure.

Webjure,Clojure的Web编程框架。

Features: Dispatch servlet calls Clojure functions. Dynamic HTML generation. SQL query interface (through JDBC).

功能:Dispatch servlet调用Clojure函数。动态HTML生成。 SQL查询接口(通过JDBC)。

This answer is meant as a placeholder for Webjure information.

这个答案是作为Webjure信息的占位符。

#8


8  

Compojure's what I used to build a tiny blogging application. It's modeled on Sinatra, which is a minimal, light-weight web framework for Ruby. I mostly just used the routing, which is just like Sinatra's. It looks like:

Compojure是我用来构建一个小型博客应用程序的东西。它以Sinatra为蓝本,这是一个用于Ruby的最小,轻量级Web框架。我大多只使用路由,这就像Sinatra的。看起来像:

(GET "/post/:id/:slug"
  (some-function-that-returns-html :id :slug))

There's no ORM or templating library, but it does have functions that turn vectors into HTML.

没有ORM或模板库,但它确实具有将向量转换为HTML的功能。

#9


5  

You can also have look at these frameworks (taken from disclojure/projects):

您还可以查看这些框架(取自disclojure / projects):

There is also one more related question on Stack Overflow: Mature Clojure web frameworks?

Stack Overflow上还有一个相关的问题:成熟的Clojure Web框架?

#10


3  

Disclaimer: I am the author.

免责声明:我是作者。

I put together a leiningen template which combines luminusweb and chestnut templates. So you get something that you can build clojure code with and clojurescript code for front and backend.
Additionally it provides user management plus some simple CRUD generation and some more small nice to haves: https://github.com/sveri/closp

我整理了一个结合了luminusweb和栗子模板的leiningen模板。因此,您可以使用前端和后端的clojurescript代码构建clojure代码。此外,它提供了用户管理以及一些简单的CRUD生成和一些更小的好处:https://github.com/sveri/closp

#11


3  

I'll throw in my two cents for Duct, also from @weavejester, the maintainer of Compojure and Ring.

我将为Duct投入两美分,也来自@weavejester,他是Compojure和Ring的维护者。

At it's core, it brings Component and the Ring router under one roof. Reasons why I use Duct:

在它的核心,它将Component和Ring路由器集中在一个屋檐下。我使用Duct的原因:

  • Excellent philosophical foundation: it encourages you to build your app as a series of small components, and it strikes a nice balance between holding few opinions while providing sane defaults.
  • 优秀的哲学基础:它鼓励您将应用程序构建为一系列小组件,并且在提供合理的默认值的同时保持少量意见之间取得了很好的平衡。

  • Stable path: I speak for myself, but over the years I've felt that the Clojure community has presented one less-than-credible web framework after another. A couple simply felt too experimental (my experience with Om and client-side Pedestal) for "getting things done" (not that they won't prove superior down the road). On the other hand, I feel like @weavejester has brought the same stability and measured progress to Duct that he did to Compojure and Ring, which have been superbly born out in the community.
  • 稳定的道路:我为自己说话,但多年来我一直认为Clojure社区已经提出了一个又一个不可靠的网络框架。一对夫妇只是感觉太过实验性(我对Om和客户端Pedestal的经验)“完成任务”(并不是说它们不会在未来发展得更好)。另一方面,我觉得@weavejester带来了与Duct相同的稳定性和测量进度,他对Compojure和Ring做了很多,这些都是在社区中出生的。

  • It's super lightweight, and out of the way of my components.
  • 它超级轻巧,不受我的组件影响。

Major features:

  • Organizes routes by "endpoints", small components that can you can think of as mini web servers (or, small cross sections of your HTTP routes).
  • 通过“端点”组织路由,这些小组件可以被视为迷你Web服务器(或HTTP路由的小横截面)。

  • Out-of-the-box support for the Reloaded Workflow.
  • 重新加载的工作流程的开箱即用支持。

  • Perfect integration with Ring and Compojure.
  • 与Ring和Compojure完美集成。

  • Development and production configurations (something I've found conspicuously missing elsewhere).
  • 开发和生产配置(我发现其他地方明显缺少的)。

  • Good documentation with examples.
  • 好的文档和示例。

Note: It goes without saying, but for the benefit of web development newcomers, like most Clojurey things Duct requires a solid grasp of Clojure the language. I also recommend reading about Component first.

注意:不言而喻,但为了网络开发新手的利益,像大多数Clojurey一样,Duct需要扎实掌握Clojure语言。我还建议先阅读有关Component的内容。

On another personal note, I've been using Duct in several production applications for over a year now and am extremely happy with it.

另一方面,我已经在几个生产应用程序中使用Duct超过一年了,我对它非常满意。

#12


2  

you can also try Clojure on Coils, http://github.com/zubairq/coils - disclaimer: I am the author

你也可以尝试Clojure on Coils,http://github.com/zubairq/coils-免责声明:我是作者

#13


2  

Another interesting webserver is Http-kit. It has good performance and is ring compliant, and has support for WebSockets as well. It is made mostly in clojure, and lacks some of the strange things in Jetty/Tomcat.

另一个有趣的网络服务器是Http-kit。它具有良好的性能并且符合环形,并且还支持WebSockets。它主要是在clojure中制作的,缺少Jetty / Tomcat中的一些奇怪的东西。

It's easy to tinker with.

修补很容易。

#14


2  

Reframe and om.next probably what you are looking for.

Reframe和om.next可能就是你要找的东西。

#15


1  

Arachne is a newcomer web framework. Quoting the site's description:

Arachne是一个新手网络框架。引用网站的描述:

Arachne is a full, highly modular web development framework for Clojure. It emphasizes ease, simplicity, and a solid, scalable design.

Arachne是Clojure的完整,高度模块化的Web开发框架。它强调简单,简洁,可靠,可扩展的设计。

It has a kickstarter campaign claiming to offer a "getting started" experience similar to Rails. It is developed by a Cognitect.

它有一个kickstarter活动声称提供类似于Rails的“入门”体验。它由Cognitect开发。

Here is a good discussion about it with the author of Luminus (yogthos).

以下是Luminus(yogthos)作者对它的一个很好的讨论。

#16


1  

I've been using Liberator successfully in production for a while now. It's a great framework if you want just the bare bones e.g. if you're building a RESTful web service or something similar. It's essentially a wrapper for ring and compojure and provides a decision graph when validating incoming requests. It's also extremely fast compared to other more bulky web frameworks. If you want to start somewhere fast and slowly build out then Liberator is a great choice.

我已经在生产中成功使用了Liberator一段时间了。如果你只想要裸骨,这是一个很棒的框架,例如如果您正在构建RESTful Web服务或类似的东西。它本质上是ring和compojure的包装器,并在验证传入请求时提供决策图。与其他更庞大的Web框架相比,它也非常快。如果你想快速开始并慢慢建立起来,那么Liberator是一个很好的选择。

#1


101  

By far the best Clojure web framework I have yet encountered is Compojure: http://github.com/weavejester/compojure/tree/master

到目前为止,我遇到的最好的Clojure Web框架是Compojure:http://github.com/weavejester/compojure/tree/master

It's small but powerful, and has beautifully elegant syntax. (It uses Jetty under the hood, but it hides the Servlet API from you unless you want it, which won't be often). Go look at the README at that URL, then download a snapshot and start playing.

它体积小但功能强大,语法优美。 (它在引擎盖下使用Jetty,但除非你需要它,否则它会隐藏你的Servlet API,这不常见)。去查看该URL的README,然后下载快照并开始播放。

#2


178  

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:

Compojure不再是开发Web应用程序的完整框架。自0.4发布以来,compojure已被分解为几个项目。 Ring通过抽象出HTTP请求和响应过程来提供基础。 Ring将解析传入的请求并生成包含请求的所有部分的映射,例如uri,server-name和request-method。然后,应用程序将处理该请求,并根据请求生成响应。响应表示为包含以下键的映射:status,headers和body。所以一个简单的应用程序看起来像:

(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的另一部分是中间件的概念。这是处理程序与传入请求和/或传出响应之间的代码。一些内置的中间件包括会话和堆栈跟踪。会话中间件将向请求映射添加:会话密钥,其中包含发出请求的用户的所有会话信息。如果:会话密钥存在于响应映射中,则它将被存储以用于当前用户发出的下一个请求。堆栈跟踪中间件将捕获处理请求时发生的任何异常,并生成堆栈跟踪,如果发生任何异常,则将其作为响应发回。

Working directly with Ring can be tedious, so Compojure is built on top of Ring abstracting away the details. The application can now be expressed in terms of routing so you can have something like this:

直接使用Ring可能会很乏味,因此Compojure构建在Ring之上,抽象出细节。现在可以用路由表示应用程序,这样你就可以得到类似的东西:

(defroutes my-routes
  (GET "/" [] "<h1>Hello all!</h1>")
  (GET "/user/:id" [id] (str "<h1>Hello " id "</h1>")))

Compojure is still working with the request/response maps so you can always access them if needed:

Compojure仍然使用请求/响应映射,因此您可以随时访问它们:

(defroutes my-routes
  (GET "*" {uri :uri} 
           {:staus 200 :body (str "The uri of the current page is: " uri)}))

In this case the {uri :uri} part accesses the :uri key in the request map and sets uri to that value.

在这种情况下,{uri:uri}部分访问请求映射中的:uri键并将uri设置为该值。

The last component is Hiccup which makes generating the html easier. The various html tags are represented as vectors with the first element representing the tag name and the rest being the body of the tag. "<h2>A header</h2>" becomes [:h2 "A Header"]. The attributes of a tag are in an optional map. "<a href='/login'>Log In Page</a>" becomes [:a {:href "/login"} "Log In Page"]. Here is a small example using a template to generate the html.

最后一个组件是Hiccup,它使生成html更容易。各种html标记表示为向量,第一个元素表示标记名称,其余元素是标记的主体。 “

标题 ”变为[:h2“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)))

Here is a link to a rough draft of some documentation currently being written by the author of compojure that you might find helpful: Compojure Doc

以下链接指向当前由compojure作者撰写的一些文档的草稿,您可能会发现这些文档很有帮助:Compojure Doc

#3


48  

There's also "Noir" (http://www.webnoir.org/), which is a new Clojure web framework (so new the docs aren't there yet). Coming from Django/Rails, I dig the simple, straightforward syntax and it's pretty lean.

还有“Noir”(http://www.webnoir.org/),这是一个新的Clojure Web框架(所以新的文档还没有)。来自Django / Rails,我挖掘了简单,直接的语法,它非常精简。

#4


24  

Consider the Luminus web framework. I have no affiliation but have heard good things from friends I respect.

考虑Luminus Web框架。我没有任何从属关系,但我听到了我尊重的朋友的好话。

#5


20  

My current go-to web library is now yada.

我现在的网络图书馆现在是yada。

If you are just starting out, the introductory server is Compojure. I see it as the apache of web servers in the Clojure world (in which case yada/aleph would be nginx). You could use Luminus as a template. There are variants of it, like compojure-api.

如果您刚刚开始,介绍服务器是Compojure。我认为它是Clojure世界中Web服务器的apache(在这种情况下,yada / aleph将是nginx)。你可以使用Luminus作为模板。它有变种,比如compojure-api。

I tried ou Pedestal and was globally satisfied with it. I don't claim to master it, but it has a pleasant syntax, feels very cohesive, and looks like it does have great performance. It is also backed by Cognitect (the Clojure/Datomic company where Rich Hickey works).

我试过ou Pedestal并对它感到满意。我并没有声称掌握它,但它具有令人愉快的语法,感觉非常有凝聚力,并且看起来确实具有很好的性能。它也得到了Cognitect(Rich Hickey工作的Clojure / Datomic公司)的支持。

I found Aleph to present an interesting abstraction, and the built-in backpressure seems interesting. I have yet to play with it, but it's definitely on my list.

我发现Aleph呈现了一个有趣的抽象,内置的背压似乎很有趣。我还没有玩它,但它肯定在我的名单上。

After playing a bit with various web servers, here is my quick Pro/Cons list :

在使用各种Web服务器播放之后,这是我的快速Pro / Cons列表:

Short answer : have a look at Luminus to get started quickly, maybe move on to something else as your needs evolve (Yada maybe).

简短的回答:看看Luminus快速入门,或许随着你的需求的发展转向别的东西(Yada也许)。

Compojure

  • Pros (1):

    • easy, lots of templates/examples (ex. Luminous)
    • 简单,很多模板/示例(例如Luminous)

  • 优点(1):简单,很多模板/示例(例如Luminous)

  • Cons (2):

    • Not performant (a thread per request), expect performances slightly better than rails
    • 性能不高(每个请求一个线程),期望性能略好于rails

    • Not simple, the middleware model has inconvenients
    • 不简单,中间件模型有不便之处

  • 缺点(2):性能不高(每个请求一个线程),期望性能稍好于rails不简单,中间件模型有不便之处

Pedestal

  • Pros (3):

    • interceptor model, pleasant syntax to add interceptors to a subset of routes
    • 拦截器模型,将拦截器添加到路径子集的令人愉快的语法

    • performant router
    • supports json/transit/multipart forms transparently out of the box, without asking anything. Very cool !
    • 支持json / transit / multipart表单,开箱即用,无需任何询问。很酷 !

  • 优点(3):拦截器模型,令人愉快的语法,将拦截器添加到路由的子集中,性能路由器支持json / transit / multipart形式,开箱即用,无需任何询问。很酷 !

  • Cons (4):

    • no websocket support (yet), returning core.async channels would be nice
    • 没有websocket支持(还),返回core.async频道会很好

    • a bit slow to reload if putting it in a Stuart Sierra's component (I think you are supposed to use the reload interceptor)
    • 将它放入Stuart Sierra的组件中时重新加载有点慢(我认为你应该使用重载拦截器)

    • no testing facility for async interceptors
    • 没有异步拦截器的测试设施

    • requires buy-in (?)
    • 需要买入(?)

  • 缺点(4):没有websocket支持(还),如果把它放在Stuart Sierra的组件中(我认为你应该使用重载拦截器)没有测试设备用于异步,返回core.async通道会很慢重新加载拦截器需要买入(?)

Aleph

Pro (3):

  • Performant
  • backpressure
  • Websocket/SSE support when returning a manifold stream
  • 返回流形流时支持Websocket / SSE

Cons (1):

  • Low level, do it yourself style (ie. it just gives you a way to make your handlers do something. No router, no nothing). Not really a cons, just be aware of it.
  • 低级别,自己动手(即它只是让你有办法让你的处理程序做一些事情。没有路由器,没有任何东西)。不是一个缺点,只是意识到它。

Yada

Pro (3):

  • built on Aleph
  • 建立在Aleph上

  • content negociation
  • swagger integration
  • bidi is quite ok (though I like pedestal router syntax better)
  • bidi相当不错(虽然我更喜欢基座路由器语法更好)

Cons (1):

  • documentation (although not as bad as nginx-clojure, quickly improving).
  • 文档(虽然没有nginx-clojure那么糟糕,但很快就会改进)。

HttpKit

Pro (2):

  • Written in Clojure ! (and Java...)
  • 写在Clojure! (和Java ...)

  • performance looks good (see the 600K concurrent connections post)
  • 性能看起来不错(参见600K并发连接帖)

Cons (2):

  • No CORS support
  • 没有CORS支持

  • Bugs ? Also, not a lot of recent commits
  • 臭虫?此外,最近提交的不是很多

Nginx-Clojure

Note : I haven't played with it, mainly because of the lack of documentation. It looks interesting though, and very performant.

注意:我没有玩过它,主要是因为缺少文档。它虽然看起来很有趣,但性能非常好。

Pros (2):

  • Nginx (performant, offload ssl, restart workers...)
  • Nginx(高性能,卸载ssl,重启工作者......)

  • Could this model allow zero-downtime updates ? That would be so awesome !
  • 该模型是否允许零停机更新?那真是太棒了!

Cons (1):

  • Documentation (improving). Also, I don't want to program in strings embedded in an nginx config file if that is the only way to do it.
  • 文件(改进)。另外,我不想在嵌入在nginx配置文件中的字符串中编程,如果这是唯一的方法。

  • Probably complicates a bit the first deployment (?)
  • 可能会使第一次部署(?)复杂化

Immutant

Note : I haven't played with it.

注意:我没玩过它。

Pros :

  • integrated (caching, messaging, scheduling, wildfly deploy)
  • 集成(缓存,消息传递,调度,野生动物部署)

Cons :

  • no http client
  • 没有http客户端

Catacumba

Note : I haven't played with it, although the documentation looks excellent. I am probably going to try it next. There are example chat projects that look interesting, their heavy use of protocols put me off at first as a novice Clojure dev.

注意:尽管文档看起来很棒,但我还没有玩过它。我可能会接下来尝试一下。有一些示例聊天项目看起来很有趣,他们大量使用协议使我最初成为新手Clojure dev。

Pros (6):

  • documentation ! Like all funcool projects, the doc is very pleasant to read.
  • 文档!像所有funcool项目一样,该文档阅读起来非常愉快。

  • pedestal-like routing syntax
  • 类似基座的路由语法

  • should be performant (on top of Ratpack)
  • 应该是高性能的(在Ratpack之上)

  • backpressure
  • websockets, sse, cors, security, ssl...
  • websockets,sse,cors,security,ssl ...

  • unique features to dig : postal
  • 独特的挖掘功能:邮政

Cons (2):

  • Not completely sure about how pleasant the ct/routes syntax is, and about ditching the Ring spec (supposedly for the async story, but I thought the pedestal guys fixed that)
  • 不完全确定ct / routes语法是多么令人愉快,以及放弃Ring规范(据说是异步故事,但我认为基座的人修复了这个)

  • Not sure how one would integrate swagger etc.
  • 不确定如何整合招摇等。

  • when I tried it, I was not able to make it work straight away
  • 当我尝试它时,我无法立即使用它

Note : a benchmark of Clojure web servers is available, if raw performance is all that matters.

注意:如果原始性能非常重要,可以使用Clojure Web服务器的基准测试。

#6


14  

These days Pedestal is a framework worth a look. It's a server-side framework that builds on top of Ring, but also frees the incoming request from the initial thread by being able to pause and resume that particular request (otherwise a slow request actually block that serverthread). Maybe sort of like a JavaBean.

这些天Pedestal是一个值得一看的框架。它是一个构建在Ring之上的服务器端框架,但也可以通过暂停和恢复该特定请求来释放来自初始线程的传入请求(否则慢速请求实际阻止该serverthread)。也许有点像JavaBean。

Other cool frameworks are hoplon.io and David Nolen's Om (based on React)

其他很酷的框架是hoplon.io和David Nolen的Om(基于React)

#7


11  

Webjure, a web programming framework for Clojure.

Webjure,Clojure的Web编程框架。

Features: Dispatch servlet calls Clojure functions. Dynamic HTML generation. SQL query interface (through JDBC).

功能:Dispatch servlet调用Clojure函数。动态HTML生成。 SQL查询接口(通过JDBC)。

This answer is meant as a placeholder for Webjure information.

这个答案是作为Webjure信息的占位符。

#8


8  

Compojure's what I used to build a tiny blogging application. It's modeled on Sinatra, which is a minimal, light-weight web framework for Ruby. I mostly just used the routing, which is just like Sinatra's. It looks like:

Compojure是我用来构建一个小型博客应用程序的东西。它以Sinatra为蓝本,这是一个用于Ruby的最小,轻量级Web框架。我大多只使用路由,这就像Sinatra的。看起来像:

(GET "/post/:id/:slug"
  (some-function-that-returns-html :id :slug))

There's no ORM or templating library, but it does have functions that turn vectors into HTML.

没有ORM或模板库,但它确实具有将向量转换为HTML的功能。

#9


5  

You can also have look at these frameworks (taken from disclojure/projects):

您还可以查看这些框架(取自disclojure / projects):

There is also one more related question on Stack Overflow: Mature Clojure web frameworks?

Stack Overflow上还有一个相关的问题:成熟的Clojure Web框架?

#10


3  

Disclaimer: I am the author.

免责声明:我是作者。

I put together a leiningen template which combines luminusweb and chestnut templates. So you get something that you can build clojure code with and clojurescript code for front and backend.
Additionally it provides user management plus some simple CRUD generation and some more small nice to haves: https://github.com/sveri/closp

我整理了一个结合了luminusweb和栗子模板的leiningen模板。因此,您可以使用前端和后端的clojurescript代码构建clojure代码。此外,它提供了用户管理以及一些简单的CRUD生成和一些更小的好处:https://github.com/sveri/closp

#11


3  

I'll throw in my two cents for Duct, also from @weavejester, the maintainer of Compojure and Ring.

我将为Duct投入两美分,也来自@weavejester,他是Compojure和Ring的维护者。

At it's core, it brings Component and the Ring router under one roof. Reasons why I use Duct:

在它的核心,它将Component和Ring路由器集中在一个屋檐下。我使用Duct的原因:

  • Excellent philosophical foundation: it encourages you to build your app as a series of small components, and it strikes a nice balance between holding few opinions while providing sane defaults.
  • 优秀的哲学基础:它鼓励您将应用程序构建为一系列小组件,并且在提供合理的默认值的同时保持少量意见之间取得了很好的平衡。

  • Stable path: I speak for myself, but over the years I've felt that the Clojure community has presented one less-than-credible web framework after another. A couple simply felt too experimental (my experience with Om and client-side Pedestal) for "getting things done" (not that they won't prove superior down the road). On the other hand, I feel like @weavejester has brought the same stability and measured progress to Duct that he did to Compojure and Ring, which have been superbly born out in the community.
  • 稳定的道路:我为自己说话,但多年来我一直认为Clojure社区已经提出了一个又一个不可靠的网络框架。一对夫妇只是感觉太过实验性(我对Om和客户端Pedestal的经验)“完成任务”(并不是说它们不会在未来发展得更好)。另一方面,我觉得@weavejester带来了与Duct相同的稳定性和测量进度,他对Compojure和Ring做了很多,这些都是在社区中出生的。

  • It's super lightweight, and out of the way of my components.
  • 它超级轻巧,不受我的组件影响。

Major features:

  • Organizes routes by "endpoints", small components that can you can think of as mini web servers (or, small cross sections of your HTTP routes).
  • 通过“端点”组织路由,这些小组件可以被视为迷你Web服务器(或HTTP路由的小横截面)。

  • Out-of-the-box support for the Reloaded Workflow.
  • 重新加载的工作流程的开箱即用支持。

  • Perfect integration with Ring and Compojure.
  • 与Ring和Compojure完美集成。

  • Development and production configurations (something I've found conspicuously missing elsewhere).
  • 开发和生产配置(我发现其他地方明显缺少的)。

  • Good documentation with examples.
  • 好的文档和示例。

Note: It goes without saying, but for the benefit of web development newcomers, like most Clojurey things Duct requires a solid grasp of Clojure the language. I also recommend reading about Component first.

注意:不言而喻,但为了网络开发新手的利益,像大多数Clojurey一样,Duct需要扎实掌握Clojure语言。我还建议先阅读有关Component的内容。

On another personal note, I've been using Duct in several production applications for over a year now and am extremely happy with it.

另一方面,我已经在几个生产应用程序中使用Duct超过一年了,我对它非常满意。

#12


2  

you can also try Clojure on Coils, http://github.com/zubairq/coils - disclaimer: I am the author

你也可以尝试Clojure on Coils,http://github.com/zubairq/coils-免责声明:我是作者

#13


2  

Another interesting webserver is Http-kit. It has good performance and is ring compliant, and has support for WebSockets as well. It is made mostly in clojure, and lacks some of the strange things in Jetty/Tomcat.

另一个有趣的网络服务器是Http-kit。它具有良好的性能并且符合环形,并且还支持WebSockets。它主要是在clojure中制作的,缺少Jetty / Tomcat中的一些奇怪的东西。

It's easy to tinker with.

修补很容易。

#14


2  

Reframe and om.next probably what you are looking for.

Reframe和om.next可能就是你要找的东西。

#15


1  

Arachne is a newcomer web framework. Quoting the site's description:

Arachne是一个新手网络框架。引用网站的描述:

Arachne is a full, highly modular web development framework for Clojure. It emphasizes ease, simplicity, and a solid, scalable design.

Arachne是Clojure的完整,高度模块化的Web开发框架。它强调简单,简洁,可靠,可扩展的设计。

It has a kickstarter campaign claiming to offer a "getting started" experience similar to Rails. It is developed by a Cognitect.

它有一个kickstarter活动声称提供类似于Rails的“入门”体验。它由Cognitect开发。

Here is a good discussion about it with the author of Luminus (yogthos).

以下是Luminus(yogthos)作者对它的一个很好的讨论。

#16


1  

I've been using Liberator successfully in production for a while now. It's a great framework if you want just the bare bones e.g. if you're building a RESTful web service or something similar. It's essentially a wrapper for ring and compojure and provides a decision graph when validating incoming requests. It's also extremely fast compared to other more bulky web frameworks. If you want to start somewhere fast and slowly build out then Liberator is a great choice.

我已经在生产中成功使用了Liberator一段时间了。如果你只想要裸骨,这是一个很棒的框架,例如如果您正在构建RESTful Web服务或类似的东西。它本质上是ring和compojure的包装器,并在验证传入请求时提供决策图。与其他更庞大的Web框架相比,它也非常快。如果你想快速开始并慢慢建立起来,那么Liberator是一个很好的选择。