Files
Kevin Harlay ebfaf6f5ec Initial commit
2026-09-07 13:33:22 +02:00

77 lines
4.0 KiB
C#

using ExpenseTracker.Api.Data;
using ExpenseTracker.Api.Services;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml;
var builder = WebApplication.CreateBuilder(args);
// ── Kestrel: bind only on localhost ─────────────────────────────────────────
builder.WebHost.UseUrls("http://127.0.0.1:5000");
// ── CORS ─────────────────────────────────────────────────────────────────────
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularDev", policy =>
policy.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod());
});
// ── Controllers ───────────────────────────────────────────────────────────────
builder.Services.AddControllers();
// ── Swagger / OpenAPI ─────────────────────────────────────────────────────────
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "ExpenseTracker API", Version = "v1" });
});
// ── EF Core + PostgreSQL ──────────────────────────────────────────────────────
// Connection string is read from the environment variable
// "ConnectionStrings__DefaultConnection" (set in Docker or local env).
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("DefaultConnection")));
// ── FluentValidation ─────────────────────────────────────────────────────────
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.Services.AddFluentValidationAutoValidation();
// ── Application Services ──────────────────────────────────────────────────────
builder.Services.AddScoped<CsvImportService>();
builder.Services.AddScoped<ExportService>();
// ── EPPlus licence (NonCommercial) ────────────────────────────────────────────
ExcelPackage.License.SetNonCommercialPersonal("ExpenseTracker");
// ─────────────────────────────────────────────────────────────────────────────
var app = builder.Build();
// ── Swagger UI (all environments for now; restrict in production if needed) ──
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExpenseTracker API v1");
c.RoutePrefix = "swagger";
});
// ── Auto-migrate + seed on startup ───────────────────────────────────────────
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync();
}
// ── Pipeline ──────────────────────────────────────────────────────────────────
app.UseCors("AllowAngularDev");
app.UseAuthorization();
app.MapControllers();
// Health check endpoint — used by Docker Compose healthcheck
app.MapGet("/health", () => Results.Ok(new { status = "healthy" }));
app.Run();