How to display a custom error page if a cloud foundry app instance is stopped? I don't want to display the default error page (404 app not available). Is there any way to implement this behavior using routing or using CF Java API?
1 Answers
The reason you're seeing a 404 here is because when your application is stopped, nothing is mapped to the routes for your application. This means that the Gorouter has no entries in it's routing table for the routes used by your app, thus it will return a 404 (i.e. that route doesn't exist).
If you want to have a custom message displayed for your route when the app isn't in use, there's a couple options that come to mind.
Deploy a small static app, probably using staticfile_buildpack or nginx_buildpack, that displays your custom message.
Now, before you stop your main app, swap the routes for it to the small static app (
cf unmap-route
/cf map-route
). The routes will still be present so Gorouter won't return a 404, instead the requests will go to your small static app which can return whatever it wants.Deploy a small static app, probably using staticfile_buildpack or nginx_buildpack, that displays your custom message.
Create a wildcard route and map it to the small static app. In this case you don't need to unmap/map the route to your small static app.
Instead you map the wildcard route, like
*.example.com
to your small static app. Then you map your normal routes likewww.example.com
ormy-cool-app.example.com
to your actual app. When the actual app is up and running, its routes are more specific so Gorouter will send traffic to that app. When you stop your main app, the routes will be pruned from Gorouter's routing table so any requests that come in will match the wildcard route and go to your small static app.

- 13,716
- 1
- 22
- 28
-
Thanks for the response. `map-route` will do :) Do you have a concrete example to automate this using CF Java API? Is this the right one? -> https://github.com/cloudfoundry/cf-java-client/blob/54c9acdc2cd35a4e1ddeaef02368b1bc5baac1a9/integration-test/src/test/java/org/cloudfoundry/client/v2/RouteMappingsTest.java#L74-L79 (Application id -> main application, routeId -> app/route name to which it should be mapped.) . Correct me if I'm wrong. – Rahul Raj Apr 25 '20 at 07:08
-
I think that's one option. I would encourage checking out the cloud foundry operations before cloud foundry client. Operations is typically a bit easier to use. See https://javadoc.io/static/org.cloudfoundry/cloudfoundry-operations/4.6.0.RELEASE/org/cloudfoundry/operations/routes/Routes.html#map-org.cloudfoundry.operations.routes.MapRouteRequest- – Daniel Mikusa Apr 26 '20 at 00:55
-
1Example: https://github.com/cloudfoundry/cf-java-client/blob/master/integration-test/src/test/java/org/cloudfoundry/operations/RoutesTest.java#L338-L344 – Daniel Mikusa Apr 26 '20 at 00:56
-
Thanks a lot Daniel! I was exactly looking for an example using `cloud foundry operations`. – Rahul Raj Apr 26 '20 at 06:09