ibdknox/noir — Issue #77: Automatic Loading/Refresh of Noir App for Development
Author: semperos
Created: 2012-01-20 05:18:41+00:00
State: open

The following allows you to start up a Noir site with `lein run` and then keep making edits to the source, only needing to refresh the page to see changes instantly. It's a proof-of-concept, not sure how you'd want to organize everything, so I've just inserted the code below.

I copied your `noir.server/start` function and adapted it to use a var instead of directly passing the value of `(gen-handler opts)` to `run-jetty`. Here is a proof of concept:

```clj
(ns noir.server
...)

(defmacro define-handler
[opts]
`(def ~'noir-app-handler (gen-handler ~opts)))

(defn dev-start
"Create a noir server bound to the specified port with a map of options and return it.
..."
[port & [opts]]
(println "Starting server...")
(define-handler opts)
(let [jetty-opts (merge {:port port :join? false} (:jetty-options opts))
server (jetty/run-jetty #'noir-app-handler jetty-opts)]
(println (str "Server started on port [" port "]."))
(println (str "You can view the site at http://localhost:" port))
server))
```

You could then change the `lein noir new` template for a project's own `server.clj` to include a conditional that starts this type of server up by default if `mode` is `:dev`:

```clj
(defn -main
...
(if (= mode :dev)
(server/dev-start port {:mode mode
:ns 'my-app})
(server/start port {:mode mode
:ns 'my-app}))
```