Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ endpoints.

## General

### Health

*This endpoint can be used as a health check for infrastructure integration
such as Docker
[`HEALTHCHECK`](https://docs.docker.com/reference/dockerfile/#healthcheck) or
load balancer probes.*

```
GET /self/v1/health
```

=== "200"

Empty response body.

=== "405"

The HTTP method is not `GET` or `HEAD`.

### List

*This endpoint lists the contents of a directory at the specified `{path}`
Expand Down
1 change: 1 addition & 0 deletions src/index/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ static auto index_main(const std::string_view &program,
sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_TRACE);
router.add("/self/v1/api/schemas/search",
sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_SEARCH);
router.add("/self/v1/health", sourcemeta::one::HANDLER_SELF_V1_HEALTH);
router.add("/self/v1/api/{+any}",
sourcemeta::one::HANDLER_SELF_V1_API_DEFAULT);

Expand Down
20 changes: 19 additions & 1 deletion src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ static auto handle_self_api_not_found(const std::filesystem::path &,
"There is nothing at this URL");
}

static auto handle_self_v1_health(const std::filesystem::path &,
const std::span<std::string_view>,
sourcemeta::one::HTTPRequest &request,
sourcemeta::one::HTTPResponse &response)
-> void {
if (request.method() != "get" && request.method() != "head") {
json_error(request, response, sourcemeta::one::STATUS_METHOD_NOT_ALLOWED,
"method-not-allowed",
"This HTTP method is invalid for this URL");
return;
}

response.write_status(sourcemeta::one::STATUS_OK);
response.write_header("Access-Control-Allow-Origin", "*");
send_response(sourcemeta::one::STATUS_OK, request, response);
}

static auto handle_self_static(const std::filesystem::path &,
const std::span<std::string_view> matches,
sourcemeta::one::HTTPRequest &request,
Expand Down Expand Up @@ -261,7 +278,8 @@ static const Handler HANDLERS[] = {handle_default,
handle_self_v1_api_schemas_trace,
handle_self_v1_api_schemas_search,
handle_self_api_not_found,
handle_self_static};
handle_self_static,
handle_self_v1_health};

static auto dispatch(const sourcemeta::core::URITemplateRouterView &router,
const std::filesystem::path &base,
Expand Down
1 change: 1 addition & 0 deletions src/shared/include/sourcemeta/one/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ constexpr auto HANDLER_SELF_V1_API_SCHEMAS_TRACE = 11;
constexpr auto HANDLER_SELF_V1_API_SCHEMAS_SEARCH = 12;
constexpr auto HANDLER_SELF_V1_API_DEFAULT = 13;
constexpr auto HANDLER_SELF_STATIC = 14;
constexpr auto HANDLER_SELF_V1_HEALTH = 15;

} // namespace sourcemeta::one

Expand Down
52 changes: 52 additions & 0 deletions test/e2e/populated/api/common/health.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
GET {{base}}/self/v1/health
HTTP 200
Access-Control-Allow-Origin: *
[Asserts]
body == ""
header "Link" not exists

HEAD {{base}}/self/v1/health
HTTP 200
Access-Control-Allow-Origin: *
[Asserts]
header "Link" not exists

POST {{base}}/self/v1/health
HTTP 405
Content-Type: application/problem+json
Access-Control-Allow-Origin: *
[Asserts]
jsonpath "$.status" == 405
jsonpath "$.title" == "sourcemeta:one/method-not-allowed"

PUT {{base}}/self/v1/health
HTTP 405
Content-Type: application/problem+json
Access-Control-Allow-Origin: *
[Asserts]
jsonpath "$.status" == 405
jsonpath "$.title" == "sourcemeta:one/method-not-allowed"

DELETE {{base}}/self/v1/health
HTTP 405
Content-Type: application/problem+json
Access-Control-Allow-Origin: *
[Asserts]
jsonpath "$.status" == 405
jsonpath "$.title" == "sourcemeta:one/method-not-allowed"

PATCH {{base}}/self/v1/health
HTTP 405
Content-Type: application/problem+json
Access-Control-Allow-Origin: *
[Asserts]
jsonpath "$.status" == 405
jsonpath "$.title" == "sourcemeta:one/method-not-allowed"

OPTIONS {{base}}/self/v1/health
HTTP 405
Content-Type: application/problem+json
Access-Control-Allow-Origin: *
[Asserts]
jsonpath "$.status" == 405
jsonpath "$.title" == "sourcemeta:one/method-not-allowed"