I'm new to clojure and compojure and I was wondering if there's a way to do a hot reload of code changes as you can with Ruby's Sinatra (with the shotgun gem) or Java's Play!
I'm following along with this Heroku tutorial so if there's a way to do it with the foreman gem that would work too.
thanks!
Take a look at this SOq:
Compojure development without web server restarts
and this tutorial:
http://mmcgrana.github.com/2010/03/clojure-web-development-ring.html
The suggestion is to use ring's reload middleware.
Recent versions of Compojure make it even easier by incorporating all of the middleware into a single function call:
https://github.com/weavejester/compojure/wiki/Getting-Started
In particular...
(require '[compojure.handler as handler])
; define my-routes in here as normal
(handler/site my-routes)
I'm using some of this in my own projects. It works well, but I'll switch back to choosing ring middleware directly if I decide that I need to add or remove anything.
Related
Using lein re-frame template I see that there's a -main function that runs a jetty server for the backend code located in clj/. Using emacs I've been able to cider-jack-in-cljs and get the frontend running in the browser, hot reload works and all but I've tried to add routes to the routes function in handler.clj inside the clj/ and haven't been able to figure out how to get that "backend side" of things to work.
I found this other question sort of related but in that case the user just wanted to know why the clj/ folder was there, not how to run that code. I haven't been able to find any documentation so far, any help is greatly appreciated.
PD: I know that having the backend and frontend in the same project/repo is not recommended (it's mentioned in the other question I linked above) but I just want to get a simple "first app" working and running first and then hopefully get the backend out into another project/repo.
Assuming you created the project with something like lein new re-frame myapp +handler the code to start the server is on the file src/clj/myapp/server.clj
You can open the file and run cider-jack-in-clj, which will ask if you want to launch lein or shadow-cljs. Since it's a CLJ file, choose lein. Once CIDER starts, you can evaluate the -main function (eg. (-main)) to start the server.
You can open the URL at http://localhost:3000 and Jetty will serve the resources that are already compiled by shadow-cljs, so you'll see the same output as viewing the other port from CLJS directly.
Note that the backend code from the template starts the Jetty server but won't help with reloading the backend. To see how enable hot reloading for the backend, check https://github.com/ring-clojure/ring/wiki/Setup-for-development
I would like my browser to refresh automatically when I change my ring handler (I'm using Hiccup to generate my html). I'm using the :reload option of serve from boot-http which works fine, but I always have to manually refresh the browser.
Leiningen seems to have an auto-refresh? option, but I couldn't find something similar for boot. There's also ring-refresh, but that's really old and seems incompatible with the latest version of Clojure.
Is there a simple way for boot to refresh the browser after my code changes?
The problem with Ring-Refresh was an outdated dependency on Compojure. Adding the latest version of Compojure as a dependency solves the problem.
I am using Pedestal (pedestal.io) and in development am running the server from inside a repl.
When I make a change to the application code (such as changing the html template) how can I have the repl reflect these changes automatically upon save of file and have them show up on browser refresh?
You might want to refer to the auto-reload-server sample which uses ns-tracker for watching changes in your code.
I'm creating a web app with Clojure using Selmer templates. I'm using Jetty, and I have to restart it to see my changes. I want to see my changes without restarting the server. How can I achieve this?
I suggest you to migrate to http-kit to get hot code reload, it's very easy
Add [http-kit "2.1.18"] to your project.clj and org.httpkit.server to your use block, and just replace run-jetty with run-server. See details in the link above
I am looking for a good production web server/servlet container for my compojure web appliction. What are the pros and cons of using Jetty or Tomcat or other server for a Clojure web app using compojure? Is there any good documentation for using a web server with Clojure for production, or tools?
I would prefer a web server that is flexible, easy to configure and has good documentation on how to configure and use it.
I think there is not yet a pure Clojure Webserver, but I heard that people at Apache were working on something like that. On the meanwhile I have some links I found useful myself.
A commentary on Apache + Jetty: http://briancarper.net/blog/510/deploying-clojure-websites
Take a look at this for implementing your own webserver via servlets: Clojure web application - where do I start?
If you don't already have your own infrastructure to run on, you might consider Heroku, which supports Clojure. See their tutorial Getting Started With Clojure on Heroku/Cedar. It's free to start and very easy to scale.
Chas Emerick has a rather old blog post + slides on doing continous integration on webapps right (Heroku, Pallet, War-file or whatever nescessary to NOT roll your own solution).