Question

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?

Was it helpful?

Solution

What do you understand under 'frontend' in your case? I see two possibilities.

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...

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.

Update.

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})

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top