forked from whatyouhide/plug_heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
247 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,49 @@ | ||
name: CI | ||
|
||
on: [push, pull_request] | ||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
format: | ||
name: Format and compile with warnings as errors | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
|
||
- name: Install OTP and Elixir | ||
uses: actions/setup-elixir@v1 | ||
with: | ||
otp-version: 23.0 | ||
elixir-version: 1.11.2 | ||
|
||
- name: Install dependencies | ||
run: mix deps.get | ||
|
||
- name: Run "mix format" | ||
run: mix format --check-formatted | ||
|
||
- name: Compile with --warnings-as-errors | ||
run: mix compile --warnings-as-errors | ||
permissions: | ||
contents: read | ||
|
||
test: | ||
name: Test (Elixir ${{matrix.elixir}} | Erlang/OTP ${{matrix.erlang}}) | ||
runs-on: ubuntu-latest | ||
jobs: | ||
mix_check: | ||
name: Elixir ${{ matrix.elixir }} / OTP ${{ matrix.otp }} | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# Following matrix is created by following https://hexdocs.pm/elixir/compatibility-and-deprecations.html | ||
# Only the most recent 3 versions of Elixir are tested." | ||
include: | ||
- erlang: 23.0 | ||
elixir: 1.11.2 | ||
- erlang: 22.2 | ||
elixir: 1.10.0 | ||
- erlang: 22.1 | ||
elixir: 1.9.4 | ||
- erlang: 21.3 | ||
elixir: 1.8.2 | ||
|
||
# Elixir v1.16 | ||
- { elixir: '1.16.x', otp: '26.x' } | ||
- { elixir: '1.16.x', otp: '25.x' } | ||
- { elixir: '1.16.x', otp: '24.x' } | ||
# Elixir v1.15 | ||
- { elixir: '1.15.x', otp: '26.x' } | ||
- { elixir: '1.15.x', otp: '25.x' } | ||
- { elixir: '1.15.x', otp: '24.x' } | ||
# Elixir v1.14 | ||
- { elixir: '1.14.x', otp: '25.x' } | ||
- { elixir: '1.14.x', otp: '24.x' } | ||
- { elixir: '1.14.x', otp: '23.x' } | ||
steps: | ||
- uses: actions/checkout@v1 | ||
|
||
- name: Install OTP and Elixir | ||
uses: actions/setup-elixir@v1 | ||
with: | ||
otp-version: ${{matrix.erlang}} | ||
elixir-version: ${{matrix.elixir}} | ||
|
||
- name: Install dependencies | ||
run: mix deps.get | ||
|
||
- name: Run tests | ||
run: mix test --trace | ||
- uses: actions/checkout@v3 | ||
- name: Setup Elixir | ||
uses: erlef/setup-beam@v1 | ||
with: | ||
elixir-version: ${{ matrix.elixir }} | ||
otp-version: ${{ matrix.otp }} | ||
- name: Restore dependencies cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: deps | ||
key: ${{ runner.os }}-elixir_${{ matrix.elixir }}-otp_${{ matrix.otp }}-mix_${{ hashFiles('**/mix.lock') }} | ||
restore-keys: ${{ runner.os }}-elixir_${{ matrix.elixir }}-otp_${{ matrix.otp }}-mix | ||
- name: Install dependencies | ||
run: mix deps.get | ||
- name: Run mix check | ||
run: mix check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
/_build | ||
/cover | ||
/deps | ||
/doc | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
some-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,26 @@ | ||
# PlugHeartbeat | ||
# PlugProbe | ||
|
||
![](https://github.com/whatyouhide/plug_heartbeat/workflows/CI/badge.svg) | ||
[![CI](https://github.com/cozy-elixir/plug_probe/actions/workflows/ci.yml/badge.svg)](https://github.com/cozy-elixir/plug_probe/actions/workflows/ci.yml) | ||
[![Hex.pm](https://img.shields.io/hexpm/v/plug_probe.svg)](https://hex.pm/packages/plug_probe) | ||
|
||
A tiny plug for responding to heartbeat requests. | ||
> A plug for responding to HTTP probe requests. | ||
## Installation and docs | ||
|
||
[Documentation is available on hex.pm][docs]. | ||
|
||
Add a dependency to your application's `mix.exs` file: | ||
Add `plug_probe` to the list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
defp deps do | ||
[{:plug_heartbeat, "~> 1.0"}] | ||
def deps do | ||
[ | ||
{:plug_probe, <requirement>} | ||
] | ||
end | ||
``` | ||
|
||
then run `mix deps.get`. | ||
|
||
## Usage | ||
|
||
Just plug this plug (sorry) in your plug pipeline: | ||
|
||
```elixir | ||
defmodule MyServer do | ||
use Plug.Builder | ||
plug PlugHeartbeat | ||
# ... rest of the pipeline | ||
end | ||
``` | ||
|
||
With this setup, all `GET` and `HEAD` requests to `/heartbeat` will return a | ||
*200 OK* status and `OK` as the body. This path can be customized through the | ||
`:path` option: | ||
|
||
```elixir | ||
defmodule MyServer do | ||
use Plug.Builder | ||
plug PlugHeartbeat, path: "/health" | ||
# ... rest of the pipeline | ||
end | ||
``` | ||
|
||
That's pretty much it, but the [docs][docs] contain further details. | ||
For more information, see the [documentation](https://hexdocs.pm/plug_probe). | ||
|
||
## License | ||
|
||
MIT © Andrea Leopardi, see the [license file][license]. | ||
|
||
|
||
[license]: LICENSE.txt | ||
[docs]: https://hex.pm/packages/plug_heartbeat | ||
Apache License 2.0 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule PlugProbe do | ||
@default_path "/probe" | ||
|
||
@moduledoc """ | ||
A plug for responding to HTTP probe requests. | ||
This plug responds to `GET` or `HEAD` requests at a specific path | ||
(`#{@default_path}` by default), with: | ||
* status code: `200` | ||
* body: `OK` or `{}` (when `json: true` option is set) | ||
## Options | ||
The following options can be used when calling `plug PlugProbe`. | ||
* `:path` (string) - specify the path on which `PlugProbe` will be mounted | ||
to respond to probe requests. Default to `#{@default_path}`. | ||
* `:json` (boolean) - specify whether the response will be an | ||
`application/json` response. Default to `false`. | ||
## Examples | ||
In general, probe requests are separated from the functionality of the | ||
application, because of that, it's better to handle them as light as | ||
possible. | ||
In order to do that, this plug should be placed near the top of a plug | ||
pipeline, then it can match requests early so that subsequent plugs don't | ||
have the chance to tamper the connection. | ||
For a simple plug pipeline: | ||
defmodule DemoServer do | ||
use Plug.Builder | ||
# Put it before any other plugs | ||
plug PlugProbe | ||
# ... | ||
end | ||
For a Phoenix endpoint: | ||
defmodule DemoWeb.Endpoint do | ||
use Phoenix.Endpoint, otp_app: :demo | ||
# Put it before any other plugs | ||
plug PlugProbe | ||
# ... | ||
end | ||
Using a custom probe path is easy: | ||
defmodule DemoServer do | ||
use Plug.Builder | ||
# Put it before any other plugs | ||
plug PlugProbe, path: "/heartbeat" | ||
# ... | ||
end | ||
""" | ||
|
||
@behaviour Plug | ||
import Plug.Conn | ||
|
||
def init(opts), | ||
do: Keyword.merge([path: @default_path, json: false], opts) | ||
|
||
def call(%Plug.Conn{} = conn, opts) do | ||
expected_path_info = String.split(opts[:path], "/", trim: true) | ||
|
||
if conn.path_info == expected_path_info and conn.method in ~w(GET HEAD) do | ||
conn |> halt() |> response(opts[:json]) | ||
else | ||
conn | ||
end | ||
end | ||
|
||
defp response(conn, false = _json), | ||
do: send_resp(conn, 200, "OK") | ||
|
||
defp response(conn, true = _json), | ||
do: conn |> put_resp_content_type("application/json") |> send_resp(200, "{}") | ||
end |
Oops, something went wrong.