Init Functions

49 views
Skip to first unread message

Rob Lachlan

unread,
Jun 19, 2010, 7:32:43 PM6/19/10
to Ring
I've spent some time looking at ring, and I really like it. One thing
that would be nice to have is the option of passing an init function
to the adapter. The init function would be called once when the web
application is loaded, to setup database connections, or perform
whatever other initializations are required.

The way that to do this seems to be to modify the servlet function in
servlet.clj, along the lines of

(defn servlet
"Create a servlet from a Ring handler.."
[handler init-fn]
(proxy [HttpServlet] []
(service [request response]
((make-service-method handler)
this request response)
(init [servletconfig]
(init-fn))))

in accordance with the servlet interface. What do you guys think?

Cheers
Rob

James Reeves

unread,
Jun 22, 2010, 5:47:22 AM6/22/10
to ring-c...@googlegroups.com
On 20 June 2010 00:32, Rob Lachlan <robert...@gmail.com> wrote:
> I've spent some time looking at ring, and I really like it.  One thing
> that would be nice to have is the option of passing an init function
> to the adapter.  The init function would be called once when the web
> application is loaded, to setup database connections, or perform
> whatever other initializations are required.

I'm not completely sure what the point of this would be. If you have
an AOT compiled servlet, adding in the init method is straightforward:

(defn -init [config]
(my-init-stuff))

(defservice my-handler)

And if you have an adapter, then you can just do something like:

(defn run []
(my-init-stuff)
(run-jetty my-handler {:port 8080}))

So why would an init function on the adapter be useful?

- James

Rob Lachlan

unread,
Jun 22, 2010, 12:58:09 PM6/22/10
to Ring
In the above example, you're running jetty embedded. But suppose that
you want to later package your application up as a war file, and
deploy to a standalone installation of Jetty or Tomcat, or whatever
app server. In that case, I don't see any other way to do startup
initialization, other than to pass a function that will be called by
the servlet init method.

I think that deploying to a standalone servlet container will be a
fairly common thing that people will want to do. And even when the
day comes that clojure web apps aren't limited to servlet containers,
e.g. when clojure-clr becomes production ready, having a common
interface for initialization still won't be a bad idea. To put it
another way, the need for initialization code is sufficiently
universal that having a common place to put that sort of thing isn't a
bad idea.

cheers,
Rob






On Jun 22, 2:47 am, James Reeves <jree...@weavejester.com> wrote:

James Reeves

unread,
Jun 22, 2010, 1:28:55 PM6/22/10
to ring-c...@googlegroups.com
On 22 June 2010 17:58, Rob Lachlan <robert...@gmail.com> wrote:
> In the above example, you're running jetty embedded.  But suppose that
> you want to later package your application up as a war file, and
> deploy to a standalone installation of Jetty or Tomcat, or whatever
> app server.

A proxy object can only be used within Clojure. If you want to
generate a HttpServlet class suitable for loading into a servlet
container like Jetty or Tomcat, you need AOT compilation. For example:

(ns my-servlet
(:gen-class :name MyServlet :extends javax.servlet.HttpServlet)
(:use [ring.util.servlet :only (defservice)]))

(defn -init [config]
your-init-code-goes-here)

(defservice
your-handler-function)

In this case, adding an "init" method is pretty straightforward.

- James

Rob Lachlan

unread,
Jun 22, 2010, 9:46:47 PM6/22/10
to Ring
All right, fair enough.

Thanks
Rob

On Jun 22, 10:28 am, James Reeves <jree...@weavejester.com> wrote:

Oleg

unread,
Jun 28, 2010, 10:17:46 AM6/28/10
to Ring
Hi James!

I think that this snippet should be added to your compojure
documentation as example of deploying application.
I think that many people have questions on "how to deploy compojure
application" and good section with a lot of different examples should
be helpful.

P.S. Idea with leiningen task is great too.

Cheers,
Oleg

On 22 июн, 21:28, James Reeves <jree...@weavejester.com> wrote:

James Reeves

unread,
Jun 30, 2010, 7:49:32 AM6/30/10
to ring-c...@googlegroups.com
On 28 June 2010 15:17, Oleg <oleg.r...@gmail.com> wrote:
> I think that this snippet should be added to your compojure
> documentation as example of deploying application.
> I think that many people have questions on "how to deploy compojure
> application" and good section with a lot of different examples should
> be helpful.

I'll add it to the Clojure Web Development book I'm writing, as it's a
general technique that isn't specific to Compojure.

- James

Reply all
Reply to author
Forward
0 new messages