clojure/lein/ring: I have two ring handlers doing different things, how do I wrap this into a servlet?

StackOverflow https://stackoverflow.com/questions/9986479

Question

I have a clojure/ring project that has two distinct app/handlers running on different ports (one for an api and one for a web frontend). They share a lot of code, but each has its own namespace where it does all the work particular to that interface. Now I want to deploy this as a servlet running in tomcat or something similar (really it needs to work in any servlet container). I am having trouble though because it seems like lein-ring makes the assumption that there will be only one handler in the servlet. For instance, in my project.clj I have this line:

:ring {:handler caribou.api/app
       :servlet-name "caribou"
       :init caribou.api/init}

This is great for the api, but now what about the frontend? Do I need to make another project that includes this one so that it can have its own handler and servlet? Does a servlet always run on one port?

There are two things I'm not sure about basically: I am not coming from a java background so I'm not sure what is going on with the servlet approach and what limitations it has, and I am unclear on how exactly clojure translates into the servlet paradigm enough to structure this project in a general way.

Any help is appreciated, thanks!

Was it helpful?

Solution

All servlets in the same container are being served from the same server and therefore the same port. Usually you identify different servlets by giving them different URI prefixes such as /servlet1 or /my/servlet.

I don't know if there is anything preventing you from creating separate servlets with Ring, but in general it doesn't seem like a good idea if your entire app is Clojure-based. At the very least, as you have pointed out, the lein-ring plugin enforces that only one servlet is used for the web application.

One thing you could do is create a parent handler that delegates to either the app or API handlers based on the URI. This essentially gives you control with out needing to delegate the logic to the Servlet API.

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