using System; using Microsoft.Extensions.Logging; using StellaOps.Auth.ServerIntegration; using StellaOps.Configuration; using StellaOps.Platform.WebService.Constants; using StellaOps.Platform.WebService.Endpoints; using StellaOps.Platform.WebService.Options; using StellaOps.Platform.WebService.Services; using StellaOps.Router.AspNet; using StellaOps.Telemetry.Core; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddStellaOpsDefaults(options => { options.BasePath = builder.Environment.ContentRootPath; options.EnvironmentPrefix = "PLATFORM_"; options.BindingSection = PlatformServiceOptions.SectionName; options.ConfigureBuilder = configurationBuilder => { configurationBuilder.AddYamlFile("../etc/platform.yaml", optional: true); configurationBuilder.AddYamlFile("platform.yaml", optional: true); }; }); var bootstrapOptions = builder.Configuration.BindOptions( PlatformServiceOptions.SectionName, static (options, _) => options.Validate()); builder.Services.AddOptions() .Bind(builder.Configuration.GetSection(PlatformServiceOptions.SectionName)) .Validate(options => { options.Validate(); return true; }) .ValidateOnStart(); builder.Services.AddRouting(options => options.LowercaseUrls = true); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOpenApi(); builder.Services.AddProblemDetails(); builder.Services.AddMemoryCache(); builder.Services.AddSingleton(TimeProvider.System); builder.Services.AddStellaOpsTelemetry( builder.Configuration, serviceName: "StellaOps.Platform", serviceVersion: typeof(Program).Assembly.GetName().Version?.ToString(), configureMetrics: meterBuilder => { meterBuilder.AddMeter("StellaOps.Platform.Aggregation"); }); builder.Services.AddTelemetryContextPropagation(); builder.Services.AddStellaOpsResourceServerAuthentication( builder.Configuration, configurationSection: null, configure: resourceOptions => { resourceOptions.Authority = bootstrapOptions.Authority.Issuer; resourceOptions.RequireHttpsMetadata = bootstrapOptions.Authority.RequireHttpsMetadata; resourceOptions.MetadataAddress = bootstrapOptions.Authority.MetadataAddress; resourceOptions.Audiences.Clear(); foreach (var audience in bootstrapOptions.Authority.Audiences) { resourceOptions.Audiences.Add(audience); } resourceOptions.RequiredScopes.Clear(); foreach (var scope in bootstrapOptions.Authority.RequiredScopes) { resourceOptions.RequiredScopes.Add(scope); } resourceOptions.RequiredTenants.Clear(); foreach (var tenant in bootstrapOptions.Authority.RequiredTenants) { resourceOptions.RequiredTenants.Add(tenant); } resourceOptions.BypassNetworks.Clear(); foreach (var network in bootstrapOptions.Authority.BypassNetworks) { resourceOptions.BypassNetworks.Add(network); } }); builder.Services.AddAuthorization(options => { options.AddStellaOpsScopePolicy(PlatformPolicies.HealthRead, PlatformScopes.OpsHealth); options.AddStellaOpsScopePolicy(PlatformPolicies.HealthAdmin, PlatformScopes.OpsAdmin); options.AddStellaOpsScopePolicy(PlatformPolicies.QuotaRead, PlatformScopes.QuotaRead); options.AddStellaOpsScopePolicy(PlatformPolicies.QuotaAdmin, PlatformScopes.QuotaAdmin); options.AddStellaOpsScopePolicy(PlatformPolicies.OnboardingRead, PlatformScopes.OnboardingRead); options.AddStellaOpsScopePolicy(PlatformPolicies.OnboardingWrite, PlatformScopes.OnboardingWrite); options.AddStellaOpsScopePolicy(PlatformPolicies.PreferencesRead, PlatformScopes.PreferencesRead); options.AddStellaOpsScopePolicy(PlatformPolicies.PreferencesWrite, PlatformScopes.PreferencesWrite); options.AddStellaOpsScopePolicy(PlatformPolicies.SearchRead, PlatformScopes.SearchRead); options.AddStellaOpsScopePolicy(PlatformPolicies.MetadataRead, PlatformScopes.MetadataRead); }); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); var routerOptions = builder.Configuration.GetSection("Platform:Router").Get(); builder.Services.TryAddStellaRouter( serviceName: "platform", version: typeof(Program).Assembly.GetName().Version?.ToString() ?? "1.0.0", routerOptions: routerOptions); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } if (!string.Equals(bootstrapOptions.Storage.Driver, "memory", StringComparison.OrdinalIgnoreCase)) { app.Logger.LogWarning("Platform storage driver {Driver} is not implemented; using in-memory stores.", bootstrapOptions.Storage.Driver); } app.UseStellaOpsTelemetryContext(); app.UseAuthentication(); app.UseAuthorization(); app.TryUseStellaRouter(routerOptions); app.MapPlatformEndpoints(); app.MapGet("/healthz", () => Results.Ok(new { status = "ok" })) .WithTags("Health") .AllowAnonymous(); app.MapGet("/readyz", () => Results.Ok(new { status = "ready" })) .WithTags("Health") .AllowAnonymous(); app.TryRefreshStellaRouterEndpoints(routerOptions); app.Run(); public partial class Program;