View Source Existence.Plug (Existence v0.3.1)
Plug responding with a health-check state.
Plug sends a text/plain response depending on the Existence overall health-check state
returned by an Existence.get_state/1 or Existence.get_state!/1 function.
configuration
Configuration
Plug is configured with a keyword list.
Plug response http status code and body are configurable with the following keys:
:ok_status- response status code for the healthy state. Default:200.:ok_body- response body for the healthy state. Default:"OK".:error_status- response status code for the unhealthy state. Default:503.:error_body- response body for the unhealthy state. Default:"Service Unavailable".
Other configuration options:
:raising?- if set totrueplug will useExistence.get_state!/1raising function to get an overall health-check state. If set to false, not raisingExistence.get_state/1function will be used. Default:false.:name- instance name defined when startingExistencechild with supervision tree. Default:Existence.
usage
Usage
Example module use with a Plug.Router.get/3 inside / route scope with a custom unhealthy
state response:
defmodule MyAppWeb.Router do
use MyAppWeb, :router
#...
scope "/", MyAppWeb do
pipe_through(:browser)
get(
"/healthcheck",
Existence.Plug,
[error_status: 500, error_body: "Internal Server Error"],
alias: false
)
end
end
Notice alias: false use to disable scoping on an external Existence.Plug in get/3 function.
Code example above will produce following http responses:
- healthy state:
$> curl -i http://127.0.0.1:4000/healthcheck HTTP/1.1 200 OK ... OK - unhealthy state:
$> curl -i http://127.0.0.1:4000/healthcheck HTTP/1.1 500 Internal Server Error ... Internal Server Error