javacloud-foundrypcfdev

How to display a custom error page when cloud foundry app is stopped?


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?


Solution

  • 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.

    1. 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.

    2. 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 like www.example.com or my-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.