using Management.Data; using Management.Security; using Management.Services; var builder = WebApplication.CreateBuilder(args); // Container-friendly HTTP binding var port = Environment.GetEnvironmentVariable("PORT") ?? "8080"; builder.WebHost.UseUrls($"http://0.0.0.0:{port}"); // CORS — allowed origins from env var, comma-separated var allowedOrigins = (builder.Configuration["CORS__AllowedOrigins"] ?? "") .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); builder.Services.AddCors(options => { options.AddDefaultPolicy(policy => { if (allowedOrigins.Length > 0) policy.WithOrigins(allowedOrigins) .AllowAnyHeader() .AllowAnyMethod(); else policy.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); // Services builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new() { Title = "AdPlatform Management API", Version = "v1" }); }); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); // Registration Function client (typed HttpClient) builder.Services.AddHttpClient(); // Graph API service — app-only credentials for org tenant user listing builder.Services.AddSingleton(); var app = builder.Build(); // Middleware pipeline app.UseSwagger(); app.UseSwaggerUI(); // Health check (before auth) app.MapGet("/health", () => Results.Ok(new { ok = true, service = "Management", timestamp = DateTimeOffset.UtcNow })); // Root endpoint app.MapGet("/", () => Results.Ok(new { service = "AdPlatform Management API", version = "1.0.0", status = "running", endpoints = new { onboarding = new[] { "GET /api/onboarding/status", "POST /api/onboarding/register" }, monitoring = new[] { "GET /api/monitoring/health", "GET /api/monitoring/stats" }, admin = new { clients = new[] { "GET/POST /api/admin/clients", "GET/PUT/DELETE /api/admin/clients/{id}" }, users = new[] { "GET/POST /api/admin/users", "GET/PUT/DELETE /api/admin/users/{id}" }, sessions = new[] { "GET /api/admin/sessions", "POST /api/admin/sessions/{id}/revoke" }, templates = new[] { "GET/POST /api/admin/templates", "GET/PUT/DELETE /api/admin/templates/{id}", "GET /api/admin/templates/categories" }, objectives = new[] { "GET/POST /api/admin/objectives", "GET/PUT/DELETE /api/admin/objectives/{id}" }, reporting = new[] { "GET /api/admin/reporting/summary", "GET /api/admin/reporting/campaigns", "GET /api/admin/reporting/campaigns/{id}", "GET /api/admin/reporting/insights", "GET /api/admin/reporting/analysis" } } } })); // CORS — must be before auth middleware app.UseCors(); // Authentication middleware app.UseMiddleware(); // Activity logging — fires after auth so ClientContext is populated app.UseMiddleware(); app.UseAuthorization(); app.MapControllers(); app.Run();