注意
鉴于此OP是大约两年前写的,我想知道是否存在分步说明,以便我可以集成Noir或其他内容,而不是再次提出相同的问题将 Clojure Web 应用程序集成到 Apache 中,无论是 Jetty、Tomcat 还是其他东西。 Django 也存在类似的说明,我想我明白 Python 在 Django 的情况下作为引擎而不是环形框架运行,因此 Clojure Web 应用程序的情况更加复杂。
尾注
我深深地爱上了 Clojure,Compojure 似乎是一个简洁的 Web 框架。
但当我想将我的应用程序作为 WAR 部署在常规应用程序服务器(如 Tomcat)上时,一切都崩溃了。实际上,我必须为静态文件和资源编写自定义的 Ring 处理程序,这些处理程序可以在本地 Jetty 和 Tomcat 上运行,因为库存处理程序不能,而且我必须手动在各处添加上下文根。
我非常惊讶我必须编写所有这些代码来创建可以在 Jetty 和 Tomcat 上运行的最简单的 Web 应用程序。对此我只有三种可能的解释:
- 除了使用 Jetty 进行本地开发(即在生产中)之外,没有人使用 Clojure/Compojure 进行其他任何事情
- 每个人都在没有上下文根的专用 Jetty 上部署 Clojure/Compojure 应用程序(就像人们使用 Node.js 应用程序一样)
- 有一种非常简单的方法可以解决我遇到的问题,我不知道
您认为属于哪种情况?或者是别的什么?
编辑:
请注意,使用 Maven/Leiningen 创建一个 war 文件是轻而易举的事情,而不是我的意思。我想知道我必须编写这么多代码才能使 Compojure 与 Tomcat 一起工作,诸如静态文件服务和上下文根感知之类的基本内容应该开箱即用。
Note
Given this OP was written about two years ago, rather than ask the same question again, I am wondering if step-by-step instructions exist, so that I can integrate a Noir or other Clojure web application into Apache, whether it's Jetty, Tomcat, or something else. Similar instructions exist for Django, and I think I understand that Python is being run in Django's case as an engine rather than a ring framework, so things are more complicated with Clojure web applications.
End Note
I'm deeply in love with Clojure, and Compojure seems like a neat web framework.
But it all fell apart when I wanted to deploy my application on a regular application server like Tomcat as a WAR. I actually had to write custom Ring handlers for static files and resources that would work on both local Jetty and Tomcat because the stock handlers didn't, and I had to prepend the context root everywhere manually.
I am negatively amazed that I had to write all this code to create the simplest web application conceivable that would run on both Jetty and Tomcat. I have only three possible explanations for this:
- Nobody uses Clojure/Compojure for anything other than local development with Jetty, i.e. in production
- Everybody deploys Clojure/Compojure apps on a dedicated Jetty without context root (LIke people do it with Node.js apps)
- There is a really simple way to circumvent the problems I ran into I wasn't aware of
Which of these is in your opinion the case? Or is it something else?
Edit:
Please note that creating a war file is a no-brainer with Maven/Leiningen and not what I mean. I'm wondering that I have to write so much code to make Compojure work with Tomcat, basic stuff like static file serving and context root awareness that should work out of the box.
发布评论
评论(7)
我使用以下组合来使这变得相当轻松:
Cake (包括部署命令)
A Cake Lau Jensen 开发的网络项目模板。
Vagrant (Ruby VM(Virtualbox) 管理工具,依赖于 Chef 或 Puppet)
VPS (来自 Slicehost)
关键部分是Lau制作的webdev模板。 webdev 文件夹应放置在
~/.cake/templates
中。要基于它创建一个新项目,请使用:请注意,该模板包括 log4j 和 Java 邮件,如果不需要,可以/应该排除它们。它还假设您正在使用 Enlive 和 Mustache,但如果这是您的毒药,则将其更改为 Compojure/Hiccup 是微不足道的。
该模板负责在开发过程中从 jetty 为应用程序提供服务(您只需评估 server.clj),并在 Tomcat 下运行时充当战争。如果在 Tomcat 下作为 ROOT.war 部署到服务器,则路由保持不变。所有静态文件应位于资源目录中。 Jetty 将从那里为他们提供服务(感谢 Ring 文件中间件)。在生产中,它们被移动到 web 应用程序的根目录,并由 Tomcat 从那里提供服务(web.xml 负责处理)。
devbox 文件夹包含创建安装了 Tomcat 的 Virtualbox VM 所需的 Vagrantfile 和说明书。我使用 cake 将 .war 文件部署到
/home/vagrant
目录(这是由 project.clj 中的开发上下文的定义控制的)。 .war 文件作为 ROOT.war 符号链接到 Tomcat 的 webapps 目录 (/var/lib/tomcat6/webapps
)。有关如何使用 Vagrant 的更多信息,请参阅 Vagrant 网站。这个gist展示了如何调整project.clj以使用cake部署命令的示例。该示例创建了两个上下文 @dev 和 @prod,您可以使用它们进行部署:
我已在此 zip。
I use a combination of the following to make this fairly painless:
Cake (incl. the deploy command)
A Cake template for webprojects developed by Lau Jensen.
Vagrant (Ruby VM(Virtualbox) management tool, which relies on Chef or Puppet)
VPS (from Slicehost)
The key part is the webdev template that Lau made. The webdev folder should be placed in the
~/.cake/templates
. To create a new project based on it use:Pls note that the template includes log4j and Java mail which can/should be excluded if not needed. It further assumes you are using Enlive and Moustache but changing that to Compojure/Hiccup is trivial if that is your poison.
The template takes care of serving the app from jetty in development (you just eval server.clj) and works as a war when running under Tomcat. Routes remain identical if deployed to the server as ROOT.war under Tomcat. All static files should be located in the resources dir. Jetty will serve them from there (thanks to the Ring file middleware). In production these are moved to the root of the webapp and served from there by Tomcat(the web.xml takes care of that).
The devbox folder contains a Vagrantfile and cookbooks necessary to create a Virtualbox VM with Tomcat installed. I use cake to deploy the .war file to the
/home/vagrant
dir (this is controlled from the definition of the dev context in project.clj). The .war file is symlinked into Tomcat's webapps dir (/var/lib/tomcat6/webapps
) as ROOT.war. For further info on how to use Vagrant please see the Vagrant site.This gist shows an example of how to adapt the project.clj to use the cake deploy command. The example creates two contexts @dev and @prod which you can deploy to using:
I have collected the Cake webdev template and the Vagrant files in this zip.
人们正在将 Compojure 应用程序部署到非 Jetty servlet 容器。
查看:
另请查看 lein-war
People are deploying Compojure apps to non-Jetty servlet containers.
Check out:
Also check out lein-war
我使用 leiningen-war 生成通用 war 文件取得了一些成功(当然,假设您使用的是 leiningen) 。它允许您指定静态 html 的位置、web.xml 的位置以及 project.clj 文件中的其他资源。
对我来说,生成一个能够部署到 JBoss 的通用 war 文件(将 Tomcat 作为 servlet 容器运行)并不太困难,但我认为您必须非常熟悉 web.xml 格式。我更喜欢编写自己的 web.xml,因此这可能是我更喜欢这种方法的原因。
看来 leiningen-war 背后的人现在正在推荐 lein-ring。我已经开始研究它,但到目前为止我还无法轻松地从中获取通用的战争文件。
我同意,但考虑生产部署是这里的一个弱点。
I've had some success using leiningen-war to generate a generic war file (assuming you are using leiningen, of course). It allows you to specify locations for static html, the location of a web.xml and other resources in your project.clj file.
It wasn't too difficult for me to produce a generic war file that I was able to deploy to JBoss (running Tomcat as a servlet container) but I think you have to be pretty familiar with the web.xml format. I'm more comfortable with authoring my own web.xml so that may account for my liking this approach more.
It appears that the person behind leiningen-war is recommending lein-ring now. I've started looking at that but so far I haven't been able to get a generic war file from it as easily.
I agree though that accounting for production deployment is a weakness here.
我正在使用 Noir,这是一个构建在 Ring 和 Compojure 之上的 Web 框架。
我使用 lein noir new my-proj 创建了项目。然后我创建了 my-proj/web 目录并添加了以下几行,
以在 Tomcat 开发过程中将 my-proj/web 目录设置为上下文根。
对于静态文件服务,我将内容放在
my-proj/resources/public
目录下。要通过代码访问(读/写)文件,可以使用环请求标头中的
:servlet-context
。通过上述设置,上下文路径将为:(.getRealPath (ring-request-header :servlet-context) "/WEB-INF/classes/myfile.txt")
。 Myfile.txt 位于my-proj/resources
下。I am using Noir, a web framework built on top of Ring and Compojure.
I have created project using
lein noir new my-proj
. Then I createdmy-proj/web
directory and added following lines toI have set
my-proj/web
directory as context root during development for Tomcat.For static file serving, I put stuff under
my-proj/resources/public
directory.For accessing (read/write) files through code,
:servlet-context
from ring request header can be used. With above settings, contextual path would be:(.getRealPath (ring-request-header :servlet-context) "/WEB-INF/classes/myfile.txt")
. Myfile.txt is undermy-proj/resources
.如果您要在 Google App Engine 上进行部署,这里有一个很棒的博客 http://compojureongae.posterous.com/
即使您没有部署到 GAE,您仍然可能会获得一些有用的提示。
If you're deploying on Google App Engine, here's a great blog http://compojureongae.posterous.com/
You may still get some useful tips even if you're not deploying to GAE.
我已经在生产环境中发布了一个 Compojure+Vaadin 应用程序(已经过去 6 个多月了)。打包是使用 lein-war 插件完成的,我没有遇到任何大问题。
该应用程序名为 halo,包含在 halo.war 文件中,上下文为 /halo,tomcat 服务器还运行 Hudson 和另一个定制的应用程序。
Vaadin 正在正确解析我所有的静态文件、CSS、图像......
我主要使用博客文章来编写需要很少的胶水。
另一方面,我也在 Noir .com/" rel="nofollow">Heroku 并且在打包和部署方面没有任何问题,而且这也在生产中。
这与您的 Compojure 问题没有直接关系,但是 Clojure 在 Web 应用程序的生产中,是的,绝对如此。
I have released in production (for over 6 months now) a Compojure+Vaadin application. Packaging has been done using the lein-war plugin and I did not encounter any major problems.
The application is named halo, contained in a halo.war file, the context is /halo and the tomcat server is also running Hudson, and another custom made application.
Vaadin is resolving all my static files properly, CSS, images...
I mainly used that blog post to write the little glue needed for it.
On another note, I am also running Noir on Heroku and haven't had any problems regarding packaging and deployment, and this is in production too.
This is not directly related to your Compojure question, but Clojure in production in web applications, yes, definitely.
如果您使用基于环的服务器(compojure、noir/lib-noir、luminus 等),并且想要部署为 uberjar,请避免
简单地使用 leinring uberjar 创建 uberjar。请注意 lein uberjar 中添加的“环”。这是假设您正在使用 lein-ring 插件。
If you are using a ring-based server (compojure, noir/lib-noir, luminus, etc.), and want to deploy as an uberjar, to avoid
simply create the uberjar with
lein ring uberjar
. Note the 'ring' addition to lein uberjar. This is assuming you're using the lein-ring plugin.