dotnet-fake-json-server icon indicating copy to clipboard operation
dotnet-fake-json-server copied to clipboard

Add health check endpoint

Open ttu opened this issue 8 months ago • 3 comments

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
  • [ ] Health endpoint should be always open, regardless of the authentication settings
  • [ ] Documentation is updated
  • [ ] Endpoint has tests

ttu avatar Apr 06 '25 05:04 ttu

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 }); } `

fransiscuss avatar Apr 09 '25 05:04 fransiscuss

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 avatar Apr 20 '25 05:04 ttu

@ttu - please check the PR https://github.com/ttu/dotnet-fake-json-server/pull/131

fransiscuss avatar Apr 25 '25 12:04 fransiscuss

Implemented in https://github.com/ttu/dotnet-fake-json-server/pull/131

ttu avatar Jul 11 '25 04:07 ttu