Files
JudoWeb/API/Program.cs
Jonas e0ecdad408 - Enable UseAuthentication middleware.
- Fix variable typo from `postgreConnection` to `postgresConnection`.
- Update `.gitignore` to exclude SQLite database files.
2026-01-23 21:52:20 +01:00

79 lines
1.9 KiB
C#

using API.Database;
using API.Models.Internal.User;
using API.Repository.AgeGroup;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var webRoot = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "wwwroot"));
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
WebRootPath = webRoot
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Authentication
builder.Services.AddAuthorization();
builder.Services.AddAuthentication()
.AddCookie(IdentityConstants.ApplicationScheme);
builder.Services.AddIdentityCore<User>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddApiEndpoints();
// Database
var postgresConnection = builder.Configuration.GetConnectionString("PostgresConnection");
if (!string.IsNullOrEmpty(postgresConnection))
{
// Nutze PostgresSQL
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(postgresConnection));
}
else
{
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite("Data Source=app.db"));
}
// Adding Database Services
builder.Services.AddScoped<IAgeGroupService, AgeGroupService>();
// Adding S3 Services
// Adding (latler) Redis Services
// Add Database Services
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
using(var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
// Deliver frontend
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
// Map Auth Endpoints
app.MapIdentityApi<User>();
app.MapControllers();
// Frontend Fallback
app.MapFallbackToFile("index.html");
app.Run();