fengling-console/docs/task-07-create-health-check.md

1.8 KiB

Task 7: Create Health Check Endpoint

Task Description

Files:

  • Modify: src/Fengling.AuthService/Program.cs
  • Modify: src/Fengling.AuthService/Fengling.AuthService.csproj

Implementation Steps

Step 1: Add health check package

Edit: src/Fengling.AuthService/Fengling.AuthService.csproj

Add package reference:

<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Npgsql.HealthChecks" Version="10.0.0" />

Step 2: Add health check configuration

Edit: src/Fengling.AuthService/Program.cs (add after builder services)

builder.Services.AddHealthChecks()
    .AddNpgSql(builder.Configuration.GetConnectionString("DefaultConnection")!);

Step 3: Add health check endpoint

Edit: src/Fengling.AuthService/Program.cs (before app.Run())

app.MapHealthChecks("/health");

Step 4: Test health check

Run:

dotnet run

Test:

curl http://localhost:5000/health

Expected: "Healthy"

Step 5: Commit

git add src/Fengling.AuthService/Program.cs src/Fengling.AuthService/Fengling.AuthService.csproj
git commit -m "feat(auth): add health check endpoint"

Context

This task adds a health check endpoint to monitor service and database connectivity. Health checks are essential for container orchestration and monitoring.

Tech Stack: ASP.NET Core Health Checks, PostgreSQL

Verification

  • Health check packages added
  • Health check configuration added
  • Health check endpoint mapped
  • Endpoint returns "Healthy"
  • Build succeeds
  • Committed to git

Notes

  • Health check endpoint: /health
  • Monitors PostgreSQL connection
  • Ready for Kubernetes liveness/readiness probes