Files
Extrudex/backend/Program.cs
rex-bot 7d0369b8e9
Some checks failed
Dev Build / build-test (push) Failing after 1m1s
Dev Build / deploy-dev (push) Has been skipped
Dev Build / notify-success (push) Has been skipped
Dev Build / notify-failure (push) Successful in 4s
chore: add Docker deployment setup and health check wiring
- Add multi-stage Dockerfile for backend (SDK build → ASP.NET runtime,
  non-root user, /health HEALTHCHECK)
- Add docker-compose.dev.yml orchestrating extrudex-api + control-center-web
- Add deploy.sh convenience script wrapping docker compose up --build
- Wire ASP.NET health checks: AddHealthChecks().AddNpgSql() + MapHealthChecks("/health")
- Add backend .dockerignore (comprehensive pattern list)
- Exclude frontend/dist, frontend/node_modules, frontend/.angular from git

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-26 17:28:06 +00:00

110 lines
4.7 KiB
C#

using System.Reflection;
using Extrudex.API.Hubs;
using Extrudex.Domain.Interfaces;
using Extrudex.Infrastructure.Data;
using Extrudex.Infrastructure.Services;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// ── Database ───────────────────────────────────────────────
// Connection string resolution (highest priority first):
// 1. EXTRUDEX_DB_CONNECTION_STRING env var (Docker / production)
// 2. Individual env vars: EXTRUDEX_DB_HOST, EXTRUDEX_DB_PORT, etc.
// 3. appsettings.json ConnectionStrings:ExtrudexDb
// 4. Hardcoded default for local dev
var connectionString = Environment.GetEnvironmentVariable("EXTRUDEX_DB_CONNECTION_STRING")
?? BuildConnectionStringFromEnvVars()
?? builder.Configuration.GetConnectionString("ExtrudexDb")
?? "Host=localhost;Port=5432;Database=extrudex;Username=extrudex;Password=changeme";
builder.Services.AddDbContext<ExtrudexDbContext>(options =>
options.UseNpgsql(connectionString));
// ── API Services ───────────────────────────────────────────
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new()
{
Title = "Extrudex API",
Version = "v1",
Description = "Filament inventory and print tracking system"
});
// Include XML doc comments in Swagger output
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
});
// ── QR Code Generation ──────────────────────────────────────
builder.Services.AddSingleton<IQrCodeService, QrCodeService>();
// ── FluentValidation ──────────────────────────────────────
// Registers all validators from the API assembly into DI.
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
// ── CORS (kiosk + remote browser) ─────────────────────────
// AllowAnyOrigin disallows credentials by spec; this is fine for
// REST API calls. SignalR WebSockets negotiate without credentials
// by default, so no special CORS policy is needed. If browser clients
// require credentials (cookies, auth headers), replace AllowAnyOrigin
// with .WithOrigins(...) and add .AllowCredentials().
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// ── SignalR (real-time printer updates) ────────────────────
builder.Services.AddSignalR();
// ── Health Checks ───────────────────────────────────────────
builder.Services.AddHealthChecks()
.AddNpgSql(connectionString);
var app = builder.Build();
// ── Middleware ──────────────────────────────────────────────
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors();
app.UseAuthorization();
app.MapControllers();
// ── Hub Endpoints ───────────────────────────────────────────
app.MapHub<PrinterHub>("/hubs/printer");
// ── Health Check Endpoint ──────────────────────────────────
app.MapHealthChecks("/health");
app.Run();
// Helper: builds a connection string from individual env vars.
// Returns null if EXTRUDEX_DB_HOST is not set.
static string? BuildConnectionStringFromEnvVars()
{
var host = Environment.GetEnvironmentVariable("EXTRUDEX_DB_HOST");
if (string.IsNullOrEmpty(host)) return null;
var port = Environment.GetEnvironmentVariable("EXTRUDEX_DB_PORT") ?? "5432";
var database = Environment.GetEnvironmentVariable("EXTRUDEX_DB_NAME") ?? "extrudex";
var username = Environment.GetEnvironmentVariable("EXTRUDEX_DB_USER") ?? "extrudex";
var password = Environment.GetEnvironmentVariable("EXTRUDEX_DB_PASSWORD") ?? "changeme";
return $"Host={host};Port={port};Database={database};Username={username};Password={password}";
}