-
Notifications
You must be signed in to change notification settings - Fork 23
Add health check command and document health checks #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3786e1f
Allow load balancers and kubernetes to call the health check endpoint…
sallam-dev 61e5dc5
Provide health check command for AWS ECS and Docker Compose without e…
sallam-dev 26e2929
Document health checks
sallam-dev c545ec2
Move health check documentation to its own file
sallam-dev 1ce54ad
Fix whitespace
sallam-dev 37aeaed
Add tests for basic auth middleware
sallam-dev d7e08f4
Fix filename
sallam-dev 8b413c2
Follow file naming convention
sallam-dev 5b8d514
Disable parallel tests for auth middleware
sallam-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,79 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/riverqueue/river/rivershared/riversharedtest" | ||
| ) | ||
|
|
||
| func TestAuthMiddleware(t *testing.T) { | ||
| var ( | ||
| ctx = context.Background() | ||
| databaseURL = cmp.Or(os.Getenv("TEST_DATABASE_URL"), "postgres://localhost/river_test") | ||
| basicAuthUser = "test_auth_user" | ||
| basicAuthPassword = "test_auth_pass" | ||
| ) | ||
|
|
||
| t.Setenv("DEV", "true") | ||
| t.Setenv("DATABASE_URL", databaseURL) | ||
| t.Setenv("RIVER_BASIC_AUTH_USER", basicAuthUser) | ||
| t.Setenv("RIVER_BASIC_AUTH_PASS", basicAuthPassword) | ||
|
|
||
| setup := func(t *testing.T, prefix string) http.Handler { | ||
| t.Helper() | ||
| initRes, err := initServer(ctx, riversharedtest.Logger(t), prefix) | ||
| require.NoError(t, err) | ||
| t.Cleanup(initRes.dbPool.Close) | ||
|
|
||
| return initRes.httpServer.Handler | ||
| } | ||
|
|
||
| t.Run("Unauthorized", func(t *testing.T) { //nolint:paralleltest | ||
| handler := setup(t, "/") | ||
| req := httptest.NewRequest(http.MethodGet, "/api/jobs", nil) | ||
| recorder := httptest.NewRecorder() | ||
|
|
||
| handler.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusUnauthorized, recorder.Code) | ||
| }) | ||
|
|
||
| t.Run("Authorized", func(t *testing.T) { //nolint:paralleltest | ||
| handler := setup(t, "/") | ||
| req := httptest.NewRequest(http.MethodGet, "/api/jobs", nil) | ||
| req.SetBasicAuth(basicAuthUser, basicAuthPassword) | ||
|
|
||
| recorder := httptest.NewRecorder() | ||
|
|
||
| handler.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, recorder.Code) | ||
| }) | ||
|
|
||
| t.Run("Healthcheck exemption", func(t *testing.T) { //nolint:paralleltest | ||
| handler := setup(t, "/") | ||
| req := httptest.NewRequest(http.MethodGet, "/api/health-checks/complete", nil) | ||
| recorder := httptest.NewRecorder() | ||
|
|
||
| handler.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, recorder.Code) | ||
| }) | ||
|
|
||
| t.Run("Healthcheck exemption with prefix", func(t *testing.T) { //nolint:paralleltest | ||
| handler := setup(t, "/test-prefix") | ||
| req := httptest.NewRequest(http.MethodGet, "/test-prefix/api/health-checks/complete", nil) | ||
| recorder := httptest.NewRecorder() | ||
|
|
||
| handler.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, recorder.Code) | ||
| }) | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,55 @@ | ||
| # River UI Health Checks | ||
| River UI exposes two types of health checks: | ||
| 1. `minimal`: Will succeed if the server can return a response regardless of the database connection. | ||
| 2. `complete`: Will succeed if the database connection is working. | ||
|
|
||
| For production deployments, it is recommended to use the `complete` health check. | ||
|
|
||
| ## How to use | ||
| ### HTTP Endpoint | ||
| Useful when running on Kubernetes or behind load balancer that can hit the HTTP endpoint. | ||
|
|
||
| The URL would be `{prefix}/api/health-checks/{name}` | ||
|
|
||
| - `{prefix}` is the path prefix set in the environment variable `PATH_PREFIX` or `-prefix` flag | ||
| - `{name}` is the health check name. Can be `minimal` or `complete`. | ||
|
|
||
| **Example:** When setting `PATH_PREFIX=/my-prefix` and wanting to include the database connection in the health check the path would be | ||
| ```text | ||
| /my-prefix/api/health-checks/complete | ||
| ``` | ||
|
|
||
| ### CLI Flag | ||
| The riverui binary provides `-healthcheck=<minimal|complete>` flag. This flag allows the binary to perform a health check as a command. | ||
|
|
||
| This useful when the container orchestrator cannot hit the health check endpoint natively. Like in AWS ECS Tasks or Docker Compose file. | ||
|
|
||
| The CLI flag will query the HTTP endpoint internally and exit based on the response. | ||
|
|
||
| This keeps the container image small without having to install additional dependencies. | ||
|
|
||
| **Example:** When using a prefix like `/my-prefix` and wanting to include the database connection in the health check the command would be | ||
| ```text | ||
| /bin/riverui -prefix=/my-prefix -healthcheck=complete | ||
| ``` | ||
|
|
||
| When setting this command in ECS tasks for healtechecks it would something like this: | ||
| ```json | ||
| { | ||
| "containerDefinitions": [ | ||
| { | ||
| "name": "riverui", | ||
| "image": "ghcr.io/riverqueue/riverui:latest", | ||
| "essential": true, | ||
| "healthCheck": { | ||
| "command": [ | ||
| "CMD", | ||
| "/bin/riverui", | ||
| "-prefix=/my-prefix", | ||
| "-healthcheck=complete" | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, was going to ask for a test for this one, but I guess we don't have any existing auth middleware tests. @bgentry there isn't one I'm missing somewhere is there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests to the auth middleware including the exemption of healthcheck endpoint