using System.Text.Json; using Elements.Backend.Services; using Elements.Data; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Google; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRouting(options => options.LowercaseUrls = true); builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; }); builder.Services.AddDbContext(options => { options.UseNpgsql(Environment.GetEnvironmentVariable("ELEM_DB_CONN_STR") ?? "Server=database;Port=5432;Database=elements;User Id=elements;Password=elementspass"); }); builder.Services .AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(options => { options.Events = new() { OnRedirectToLogin = (ctx) => { ctx.Response.StatusCode = 401; return Task.CompletedTask; }, OnRedirectToAccessDenied = (ctx) => { ctx.Response.StatusCode = 401; return Task.CompletedTask; } }; }); builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddHostedService( provider => provider.GetRequiredService()); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { ApplicationDbContext context = scope.ServiceProvider.GetRequiredService(); context.Database.Migrate(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();