using Npgsql; var builder = WebApplication.CreateBuilder(args); // The connection string comes from configuration: appsettings.json, or the environment // variable ConnectionStrings__Clinic, which is how compose.yaml passes it to the container. var connectionString = builder.Configuration.GetConnectionString("Clinic") ?? "Host=localhost;Port=15850;Database=clinic;Username=clinic;Password=clinic"; builder.Services.AddSingleton(NpgsqlDataSource.Create(connectionString)); var app = builder.Build(); app.MapGet("/", () => Results.Ok(new { app = "ClinicApi", machine = Environment.MachineName, version = "2" })); app.MapGet("/health", async (NpgsqlDataSource db) => { await using var cmd = db.CreateCommand("SELECT 1"); await cmd.ExecuteScalarAsync(); return Results.Ok(new { status = "ok", database = "reachable" }); }); app.MapGet("/patients", async (NpgsqlDataSource db) => { var patients = new List(); await using var cmd = db.CreateCommand("SELECT id, full_name, phone FROM patients ORDER BY id"); await using var reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) { patients.Add(new { id = reader.GetInt64(0), fullName = reader.GetString(1), phone = reader.GetString(2) }); } return Results.Ok(patients); }); app.Run();