This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Api.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Controller that exposes simple health and readiness endpoints for the API.
|
||||||
|
/// Routes are prefixed with "api/health".
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public sealed class HealthController : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Liveness probe.
|
||||||
|
/// Indicates whether the process is running. Used by orchestration systems to confirm the process is alive.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON payload: { "status": "alive" } when the process is running.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health/live
|
||||||
|
[HttpGet("live")]
|
||||||
|
public IActionResult Live() => Ok(new { status = "alive" });
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Basic health check endpoint.
|
||||||
|
/// Returns overall status and the current server time in UTC.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON payload: { "status": "ok", "time": <UTC time> }.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Health() => Ok(new { status = "ok", time = DateTimeOffset.UtcNow });
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Echo endpoint.
|
||||||
|
/// Returns the received JSON payload unchanged. Useful for testing request/response plumbing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="payload">Arbitrary JSON from the request body. The endpoint returns the same object.</param>
|
||||||
|
/// <returns>200 OK with the same JSON payload provided in the request body.</returns>
|
||||||
|
// POST api/health/echo
|
||||||
|
[HttpPost("echo")]
|
||||||
|
public IActionResult Echo(object payload) => Ok(payload);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Readiness probe.
|
||||||
|
/// Indicates whether the service is ready to accept traffic. Typically checks downstream dependencies.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON { "status": "ready" } when ready;
|
||||||
|
/// 503 Service Unavailable with JSON { "status": "not_ready" } when not ready.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health/ready
|
||||||
|
[HttpGet("ready")]
|
||||||
|
public IActionResult Ready()
|
||||||
|
{
|
||||||
|
var ready = true;
|
||||||
|
|
||||||
|
return ready
|
||||||
|
? Ok(new { status = "ready" })
|
||||||
|
: StatusCode(503, new { status = "not_ready" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,7 +83,6 @@ try
|
|||||||
app.UseSwaggerInDevelopment(ServiceName, ServiceName);
|
app.UseSwaggerInDevelopment(ServiceName, ServiceName);
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapHealthEndpoint(ServiceName, appVersion);
|
|
||||||
|
|
||||||
Log.Information("Running EF Core migrations if any");
|
Log.Information("Running EF Core migrations if any");
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Api.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Controller that exposes simple health and readiness endpoints for the API.
|
||||||
|
/// Routes are prefixed with "api/health".
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public sealed class HealthController : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Liveness probe.
|
||||||
|
/// Indicates whether the process is running. Used by orchestration systems to confirm the process is alive.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON payload: { "status": "alive" } when the process is running.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health/live
|
||||||
|
[HttpGet("live")]
|
||||||
|
public IActionResult Live() => Ok(new { status = "alive" });
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Basic health check endpoint.
|
||||||
|
/// Returns overall status and the current server time in UTC.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON payload: { "status": "ok", "time": <UTC time> }.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Health() => Ok(new { status = "ok", time = DateTimeOffset.UtcNow });
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Echo endpoint.
|
||||||
|
/// Returns the received JSON payload unchanged. Useful for testing request/response plumbing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="payload">Arbitrary JSON from the request body. The endpoint returns the same object.</param>
|
||||||
|
/// <returns>200 OK with the same JSON payload provided in the request body.</returns>
|
||||||
|
// POST api/health/echo
|
||||||
|
[HttpPost("echo")]
|
||||||
|
public IActionResult Echo(object payload) => Ok(payload);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Readiness probe.
|
||||||
|
/// Indicates whether the service is ready to accept traffic. Typically checks downstream dependencies.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// 200 OK with JSON { "status": "ready" } when ready;
|
||||||
|
/// 503 Service Unavailable with JSON { "status": "not_ready" } when not ready.
|
||||||
|
/// </returns>
|
||||||
|
// GET api/health/ready
|
||||||
|
[HttpGet("ready")]
|
||||||
|
public IActionResult Ready()
|
||||||
|
{
|
||||||
|
var ready = true;
|
||||||
|
|
||||||
|
return ready
|
||||||
|
? Ok(new { status = "ready" })
|
||||||
|
: StatusCode(503, new { status = "not_ready" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,7 +71,6 @@ try
|
|||||||
app.UseSwaggerInDevelopment(ServiceName, ServiceName);
|
app.UseSwaggerInDevelopment(ServiceName, ServiceName);
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapHealthEndpoint(ServiceName, appVersion);
|
|
||||||
|
|
||||||
Log.Information("Running EF Core migrations if any");
|
Log.Information("Running EF Core migrations if any");
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
|
|||||||
@@ -215,15 +215,4 @@ public static class StartupExtensions
|
|||||||
options.RoutePrefix = "swagger";
|
options.RoutePrefix = "swagger";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void MapHealthEndpoint(this WebApplication app, string serviceName, string appVersion)
|
|
||||||
{
|
|
||||||
app.MapGet("/health", () => Results.Ok(new
|
|
||||||
{
|
|
||||||
status = "ok",
|
|
||||||
service = serviceName,
|
|
||||||
version = appVersion,
|
|
||||||
timeUtc = DateTimeOffset.UtcNow
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user