mirror of
https://github.com/azaion/missions.git
synced 2026-06-21 09:21:07 +00:00
7025f4d075
Updated JWT authentication to use configuration values instead of hardcoded secrets, improving security and flexibility. Enhanced CORS policy to conditionally allow origins based on configuration settings, with logging for permissive defaults. Updated README to reflect project renaming and clarify service context.
91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using LinqToDB;
|
|
using LinqToDB.Data;
|
|
using Azaion.Flights.Auth;
|
|
using Azaion.Flights.Database;
|
|
using Azaion.Flights.Infrastructure;
|
|
using Azaion.Flights.Middleware;
|
|
using Azaion.Flights.Services;
|
|
|
|
const string DatabaseUrlEnvVar = "DATABASE_URL";
|
|
const string DatabaseUrlConfigKey = "Database:Url";
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var databaseUrl = ConfigurationResolver.ResolveRequiredOrThrow(
|
|
builder.Configuration,
|
|
DatabaseUrlEnvVar,
|
|
DatabaseUrlConfigKey,
|
|
"Database connection string");
|
|
|
|
var connectionString = databaseUrl.StartsWith("postgresql://")
|
|
? ConvertPostgresUrl(databaseUrl)
|
|
: databaseUrl;
|
|
|
|
builder.Services.AddScoped(_ =>
|
|
{
|
|
var options = new DataOptions().UsePostgreSQL(connectionString);
|
|
return new AppDataConnection(options);
|
|
});
|
|
|
|
builder.Services.AddScoped<FlightService>();
|
|
builder.Services.AddScoped<WaypointService>();
|
|
builder.Services.AddScoped<AircraftService>();
|
|
|
|
builder.Services.AddJwtAuth(builder.Configuration);
|
|
|
|
var allowedOrigins = builder.Configuration.GetSection("CorsConfig:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
|
|
var allowAnyOrigin = builder.Configuration.GetValue<bool>("CorsConfig:AllowAnyOrigin");
|
|
CorsConfigurationValidator.EnsureSafeForEnvironment(allowedOrigins, allowAnyOrigin, builder.Environment.EnvironmentName);
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
if (CorsConfigurationValidator.ShouldUsePermissivePolicy(allowedOrigins, allowAnyOrigin))
|
|
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
|
|
else
|
|
policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (CorsConfigurationValidator.ShouldWarnAboutPermissiveDefault(allowedOrigins, allowAnyOrigin))
|
|
{
|
|
app.Services
|
|
.GetRequiredService<ILogger<Program>>()
|
|
.LogWarning(CorsConfigurationValidator.PermissiveDefaultWarning, app.Environment.EnvironmentName);
|
|
}
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDataConnection>();
|
|
DatabaseMigrator.Migrate(db);
|
|
}
|
|
|
|
app.UseMiddleware<ErrorHandlingMiddleware>();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
app.MapControllers();
|
|
app.MapGet("/health", () => Results.Ok(new { status = "healthy" }));
|
|
|
|
app.Run();
|
|
|
|
static string ConvertPostgresUrl(string url)
|
|
{
|
|
var uri = new Uri(url);
|
|
var userInfo = uri.UserInfo.Split(':');
|
|
var host = uri.Host;
|
|
var port = uri.Port > 0 ? uri.Port : 5432;
|
|
var database = uri.AbsolutePath.TrimStart('/');
|
|
return $"Host={host};Port={port};Database={database};Username={userInfo[0]};Password={userInfo.ElementAtOrDefault(1) ?? ""}";
|
|
}
|