Can I start the server without a header in a specific servlet context?

I have a Ring app that deploys as an uberwar object; myservice.war. During WAR production, the file gets into Jetty, where it runs in the context that follows its name.

 $ curl -i -X GET http://myservice.qa1.example.com:8080/myservice/healthz HTTP/1.1 200 OK ... 

When I run locally through the lein ring, I need it to run in the same context; MyService.

 $lein ring server-headless 2015-10-14 14:04:03,457 level=INFO [main] Server:271 - jetty-7.6.13.v20130916 2015-10-14 14:04:03,482 level=INFO [main] AbstractConnector:338 - Started SelectChannelConnector@0.0.0.0 :10313 Started server on port 10313 

But the same twisting on me is all locally local.

 $ curl -i -X GET http://localhost:10313/myservice/healthz HTTP/1.1 404 Not Found ... 

The lane ring affair set him up in the root context.

 $ curl -i -X GET http://localhost:10313/healthz HTTP/1.1 200 OK ... 

What's up with that? How can I bind a lane ring to an expansion in the name of the context of my choice? I need curl -i -X GET http://localhost:10313/myservice/healthz to work with lein ring

+5
source share
1 answer

One way around this problem is to create a second (stand-alone) set of routes for your application. You also create a second handler for an individual case. You can then use Leiningen profiles to specify different handlers for the individual case and the uberwar case. The default profile is used when running the application offline. Profile :uberjar used when creating uberwar. As a result, your standalone handler, which will be used with the lein ring server-headless and your regular handler, will be used when the war is deployed to the container.

No additional code is required to create a second set of routes. You can simply wrap existing routes in your chosen context. Suppose your routes and call handler:

 (defroutes app-routes (GET "/healthz" [] "Hello World") (route/not-found "Not Found")) (def app (wrap-defaults app-routes site-defaults)) 

Additional routes and a handler for a particular case will look like this:

 (defroutes standalone-routes (context "/myservice" req app-routes) (route/not-found "Not Found")) (def standalone-app (wrap-defaults standalone-routes site-defaults)) 

Now, on the lein-ring configuration in project.clj . We want the ring handler to point to standalone-app by default. The ring handler for uberwar should point to the app . The entry :ring on the project map in project.clj should look like this (adjust for your actual namespace):

 :ring {:handler myservice.handler/standalone-app} 

Also, combine the following on a map :profiles in project.clj :

 :uberjar {:ring {:handler myservice.handler/app}} 

Please use the latest lein-ring plugin. Version 0.9.7 worked for me. Earlier versions, such as 0.8.3, did not work because they did not use the :uberjar profile when running the uberwar task.

If you do all this and assume that your war file is called myservice.war, the context part of the URI will be the same regardless of whether your application is launched using lein ring server-headless or if the war file is deployed to Jetty.

 $ curl http://localhost:[port]/myservice/healthz 
+1
source

Source: https://habr.com/ru/post/1233739/


All Articles