将WebApp前端添加到现有的Clojure应用程序 [英] Add webapp frontend to existing clojure app
问题描述
我有一个基于Clojure的聊天机器人,我以lein run
的典型leiningen方式启动.我想在此应用程序中添加前端,但不完全确定该怎么做.通过阅读有关compojure,lib-noir和ring的文档,看起来标准的服务方式是使用lein ring server
.如果可能的话,我宁愿只用一个lein命令启动应用程序和前端.这会涉及使用(run-jetty handler {:port 3000})
之类的手动启动服务器(也许在另一个线程中),还是有人可以推荐一种更好的方法?
I have a Clojure-based chat bot that I start up in typical leiningen fashion with lein run
. I'd like to add a front end to this app, but not totally sure how to go about it. From reading docs on compojure, lib-noir and ring, looks like the standard way to serve is with lein ring server
. I'd rather just start up the app and the front end with a single lein command if possible. Would this involve manually starting up the server (in another thread perhaps) with something like (run-jetty handler {:port 3000})
, or could anyone recommend a better approach?
推荐答案
您对"frontend"下的情况有什么了解?我看到两种可能性.
What do you understand under 'frontend' in your case? I see two possibilities.
第一个是您的Clojure机器人完全独立并且具有一些可与之交互的外部接口时.在这种情况下,您的前端将是单独的应用程序,通过该外部接口与bot进行通信;实际上,在这种情况下,如果您要使用单个lein命令启动程序,则应在Webapp中使用显式的-main
函数,该函数将首先运行您的机器人,然后启动服务器.我不知道启动服务器的确切命令.您的情况对我来说很好,但我想我在某处读到这种启动已被弃用...
First one is when your Clojure bot is completely standalone and has some external interface to interact with. In this case your frontend will be separate application talking with the bot via this external interface, and indeed in this case if you want to start your programs with single lein command you should be using explicit -main
function in your webapp which will first run your bot and then start the server. I don't know exact command to start the server though; yours looks fine for me, but I think I read somewhere that this kind of startup was deprecated...
另一种可能性是当您希望将Web应用程序集成到机器人中时.在这种情况下,您只需以直接使用漫游器名称空间的方式编写webapp;不需要-main
功能,只需执行lein ring server
命令即可.
Another possibility is when you want the webapp to be integrated into the bot. In this case you just write the webapp in such way that it uses namespaces of the bot directly; no -main
function is required, and all you have to do is run the lein ring server
command.
对我来说,第二个看起来更清晰,但这取决于您的机器人的总体体系结构.
The second one looks clearer to me, but it depends on overall architecture of your bot.
更新.
我已经更彻底地研究了ring和leiningen的工作方式,看来让您获得所需东西的最简单方法如下.首先,按照其自述文件的指示安装lein-ring
插件.
接下来,类似于以下内容配置您的project.clj
:
I have looked more thoroughly at how ring and leiningen work together, and it seems that the simplest way for you to get what you want is as follows. First, install lein-ring
plugin as its readme directs.
Next, configure your project.clj
similarly to the following:
(defproject your-project "0.0.1"
:dependencies [...]
... ; All other configuration
:ring {:handler your-namespace.web/handler
:init your-namespace.bot/init})
请参见,您在project.clj
文件中应该有其他选项(我在上面链接到的自述文件中对此进行了描述). :handler
是您的主要Web应用程序处理程序(有关它的含义以及为什么需要此原因,请参阅Ring文档). :init
应该是您的初始化函数. 这正是您应该添加代码以启动bot的地方.
最后,发出lein ring server
命令启动您的Web应用程序.这将首先调用您在project.clj
中指定为:init
的函数,该函数随后将启动您的机器人,然后将启动您的Web应用程序.
See, you should have additional options in your project.clj
file (they are described in the readme I have linked to above). :handler
is your main web application handler (refer to ring documentation on what it is and why it is needed). :init
should be your initialization function. This is exactly the place you should add the code to to start your bot.
Finally, issue lein ring server
command to start your webapp. This will first call a function you specified as :init
in your project.clj
, which in turn will start your bot, and then your webapp will be launched.
这篇关于将WebApp前端添加到现有的Clojure应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!