using GerbilManagerWebAPI.DAL; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); // LAN reachability: bind on all interfaces so devices on the home network (the // SPA on a phone/laptop) can reach the API. We keep whatever port Aspire / // launchSettings assigned and only widen the host from localhost to 0.0.0.0. var aspnetUrls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS"); if (!string.IsNullOrWhiteSpace(aspnetUrls)) { var lanUrls = string.Join(';', aspnetUrls .Split(';', StringSplitOptions.RemoveEmptyEntries) .Select(u => u.Replace("localhost", "0.0.0.0").Replace("127.0.0.1", "0.0.0.0"))); builder.WebHost.UseUrls(lanUrls); } // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.AddNpgsqlDbContext("gerbilmanager"); builder.Services.AddScoped(); // Permissive CORS for the trusted home LAN (no auth — see hive scope rule). // The SPA calls the API cross-origin from other devices on the network. const string LanCorsPolicy = "lan"; builder.Services.AddCors(options => options.AddPolicy(LanCorsPolicy, policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod())); var app = builder.Build(); app.MapDefaultEndpoints(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); // Aspire provisions an empty database in dev — bring the schema up to date. using var scope = app.Services.CreateScope(); scope.ServiceProvider.GetRequiredService().Database.Migrate(); } // No HTTPS redirection: the LAN clients (phone) talk plain HTTP to avoid // dev-certificate trust issues on the device. app.UseCors(LanCorsPolicy); app.UseAuthorization(); app.MapControllers(); app.Run();