What is the best way to run a ring application in REPL?

1,409 views
Skip to first unread message

C.F.

unread,
Nov 16, 2011, 11:04:35 PM11/16/11
to Ring
Hi,

I've read a few guides and I'm still wondering what would be the best
way to run and debug a ring application interactively in REPL.
I implemented my app as
(defroutes main-routes ...)
(def app (handler/site routes)

It runs with 'lein ring server', I can compile as WAR and deploy.

However, I couldn't find anything that would help me run it in REPL
mode.
I came across ring-serve, but it seems to support only separate ring
handlers, I couldn't make it work with the whole map of routes.
Another approach I figure out is to kick off Jetty internally, which I
found to be a bit overkill and I cannot use TomCat this way.

How do you do this?

Is Jetty the only viable approach?

Thank you,
- F

James Reeves

unread,
Nov 16, 2011, 11:13:53 PM11/16/11
to ring-c...@googlegroups.com
On 17 November 2011 04:04, C.F. <fra...@gmail.com> wrote:
> I came across ring-serve, but it seems to support only separate ring
> handlers, I couldn't make it work with the whole map of routes.

Could you explain what the problem was? The ring-serve library should
work the same way as "lein ring server", minus the automatic reloading
of modified source files. It's pretty much the same code in both
projects.

- James

C.F.

unread,
Nov 17, 2011, 12:07:34 AM11/17/11
to Ring

Thanks, man, it definitely does! Took me a while, it's not an easy
task to be a newb.

If anyone runs into this, first off define

;; in your source code
;; to get rid of "Warning: *html-mode* not declared dynamic and thus
is not dynamically rebindable, but its name suggests otherwise. Please
either indicate ^:dynamic *html-mode* or change the name."
(def ^:dynamic *html-mode* :xml)
(def ^:dynamic *base-url* nil)

(defroutes main-routes ...)
(def app (handler/site main-routes))


Then in REPL,
(use 'ring.util.serve)
(serve your-namespace.your-namespace/main-routes)


Beware that killing the REPL process seems to kill the browser as
well, at least on my Ubuntu.

Thanks again,
- F

James Reeves

unread,
Nov 17, 2011, 6:43:26 AM11/17/11
to ring-c...@googlegroups.com
On 17 November 2011 05:07, C.F. <fra...@gmail.com> wrote:
> ;; in your source code
> ;; to get rid of "Warning: *html-mode* not declared dynamic and thus
> is not dynamically rebindable, but its name suggests otherwise. Please
> either indicate ^:dynamic *html-mode* or change the name."
> (def ^:dynamic *html-mode* :xml)
> (def ^:dynamic *base-url* nil)

The latest Hiccup HEAD should solve that. I'll release a new version.

> (defroutes main-routes ...)
> (def app (handler/site main-routes))
>
> Then in REPL,
> (use 'ring.util.serve)
> (serve your-namespace.your-namespace/main-routes)

Why are you serving "main-routes" when your main handler is "app"?

- James

C.F.

unread,
Nov 19, 2011, 1:07:54 AM11/19/11
to Ring

Wow, I didn't realize the app is a handler by itself.

Probably a stupid question, but I cannot seem to figure this one out -

Is there an easy way to get all files compiled into REPL as a result
of clojure-jack-in? Do I need to C-c C-k each of them every time I
start REPL?

Thanks again,
- F

James Reeves

unread,
Nov 19, 2011, 8:41:58 AM11/19/11
to ring-c...@googlegroups.com
On 19 November 2011 06:07, C.F. <fra...@gmail.com> wrote:
> Is there an easy way to get all files compiled into REPL as a result
> of clojure-jack-in? Do I need to C-c C-k each of them every time I
> start REPL?

Just use or require the namespace your handler is in. All the
namespaces your handler namespace depends on will be loaded
automatically by Clojure.

(use 'your.handler.namespace)

If you want to reload files you can C-c C-k or use the :reload option:

(use 'your.handler.namespace :reload)

The :reload-all option should reload all dependent namespaces:

(use 'your.handler.namespace :reload-all)

- James

Reply all
Reply to author
Forward
0 new messages