Add health check endpoint
Currently, the dotnet-fake-json-server does not have a health check endpoint. This makes it difficult to monitor the health and status of the server in a standardized way.
Implement a health check endpoint that returns the state of the server. The endpoint should be accessible via GET /health and return a JSON response with the status of the server.
Example response when service is healthy:
{
"status": "Healthy",
"uptime": "3 days, 4 hours, 2 minutes",
"version": "1.0.0"
}
Example response when service is down (e.g. can't access JSON data):
{
"status": "Unhealthy"
}
Acceptance criteria:
- [ ] A new endpoint GET /health is added.
- [ ] The endpoint should return a JSON response with the status of the server.
- [ ] Healthy state should include the server status, uptime, and version and return response with status code
200 - [ ] Unhealthy state should return the server status and return response with status code
503
- [ ] Healthy state should include the server status, uptime, and version and return response with status code
- [ ] Health endpoint should be always open, regardless of the authentication settings
- [ ] Documentation is updated
- [ ] Endpoint has tests
I am interested on working on this issue. @ttu Do you know how do we simulate "Can't access json data", I tried to rename the file but the file is recreated again?
Below is my logic ` try { // Check if data store is accessible _ds.Reload();
return Ok(new
{
status = "Healthy",
uptime = formattedUptime,
version = _version
});
} catch (Exception ex) { return StatusCode(StatusCodes.Status503ServiceUnavailable, new { status = "Unhealthy", version = _version }); } `
Hi! I think best way to simulate this is in the unit/integration test by using some mock framework e.g. NSubstitute. Here is an example unit test with NSubstitute. Not sure if the syntax is correct, but you might get the idea from this:
[Fact]
public void GetHealth_ErrorStatusCode()
{
var dataStore = Substitute.For<IDataStore>();
dataStore.Reload()
.Throws(new Exception( "Failed to reload data from file"))
var controller = new HelloController(dataStore);
var response = controller.GetHealth();
...
}
You can check more examples how to create this kind of unit tests from e.g. DynamicControllerTests.
Just let me know if you have any other questions!
@ttu - please check the PR https://github.com/ttu/dotnet-fake-json-server/pull/131
Implemented in https://github.com/ttu/dotnet-fake-json-server/pull/131