- AddCors lan policy (AllowAnyOrigin/Header/Method) — trusted home LAN, no auth - Widen ASPNETCORE_URLS host localhost/127.0.0.1 -> 0.0.0.0 (keep Aspire-assigned port) - Remove dev HTTPS redirection so phones reach the API over plain HTTP Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
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<ApplicationContext>("gerbilmanager");
|
|
builder.Services.AddScoped<UnitOfWork>();
|
|
|
|
// 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<ApplicationContext>().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();
|