diff options
| author | ivar <i@oiee.no> | 2025-12-03 21:49:20 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2025-12-03 21:49:20 +0100 |
| commit | cd70f54266d708867a1eb35870bc755bc5b2df32 (patch) | |
| tree | f0a8ec571ef3f345ac74293b4cb11918878b3ed5 /api/WhatApi/Program.cs | |
| parent | 5bd9ad8bd1740dcff179d66718532086304ca4c4 (diff) | |
| download | what-cd70f54266d708867a1eb35870bc755bc5b2df32.tar.xz what-cd70f54266d708867a1eb35870bc755bc5b2df32.zip | |
Refactor db
Diffstat (limited to 'api/WhatApi/Program.cs')
| -rw-r--r-- | api/WhatApi/Program.cs | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/api/WhatApi/Program.cs b/api/WhatApi/Program.cs index ac7e825..fa50661 100644 --- a/api/WhatApi/Program.cs +++ b/api/WhatApi/Program.cs @@ -7,15 +7,24 @@ global using NetTopologySuite.Geometries; global using Microsoft.AspNetCore.Http.Extensions; global using Microsoft.AspNetCore.Mvc; global using NetTopologySuite; +global using WhatApi.Database.Tables; +global using WhatApi.Database; +global using System.Text; +global using WhatApi.Extras; +global using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; using Npgsql; using WhatApi; using WhatApi.Middleware; var builder = WebApplication.CreateBuilder(args); var dev = builder.Environment.IsDevelopment(); + builder.Services.AddHttpContextAccessor(); -builder.Services.AddDbContextPool<Database>(b => { - var dataSourceBuilder = new NpgsqlDataSourceBuilder(builder.Configuration.GetConnectionString("Master")); +builder.Services.AddDbContextPool<AppDatabase>(b => { + var connectionString = builder.Configuration.GetValue<string>(Constants.Env.MasterDbConnectionString); + var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString); dataSourceBuilder.EnableDynamicJson(); if (dev) { b.EnableSensitiveDataLogging(); @@ -28,9 +37,35 @@ builder.Services.AddDbContextPool<Database>(b => { o.UseNetTopologySuite(); }); }); + if (dev) builder.Configuration["DISABLE_AUDIT_TRAILS"] = "true"; + builder.Services.AddCors(o => o.AddDefaultPolicy(p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); +var tokenEntropy = builder.Configuration.GetValue<string>(Constants.Env.TokenEntropy); +ArgumentException.ThrowIfNullOrEmpty(tokenEntropy); +var tokenIssuer = builder.Configuration.GetValue<string>(Constants.Env.TokenIssuer); +var tokenAudience = builder.Configuration.GetValue<string>(Constants.Env.TokenAudience); + +builder.Services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(options => { + options.RequireHttpsMetadata = false; + options.SaveToken = true; + options.TokenValidationParameters = new TokenValidationParameters { + ValidateIssuer = true, + ValidateAudience = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + ValidIssuer = tokenIssuer, + ValidAudience = tokenAudience, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(tokenEntropy)), + ClockSkew = TimeSpan.Zero + }; + }); +builder.Services.AddAuthorization(); builder.Services.AddControllers() .AddJsonOptions(o => { o.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; @@ -39,19 +74,25 @@ builder.Services.AddControllers() o.JsonSerializerOptions.Converters.Add(new GeoJsonConverterFactory()); }); + var app = builder.Build(); + if (dev) { using var scope = app.Services.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService<Database>(); + var db = scope.ServiceProvider.GetRequiredService<AppDatabase>(); Seed.Full(db, opt => { opt.ClearTables = false; }); } + app.UseRouting(); app.UseForwardedHeaders(); app.UseCors(); app.MapStaticAssets(); app.UseMiddleware<UserLastSeenMiddleware>(); +app.UseAuthentication(); app.MapControllers(); +app.MapGet("/", () => Results.Redirect("/map")); app.Run(); + return 0;
\ No newline at end of file |
