Emberend/Program.cs
2026-01-20 19:32:45 +01:00

92 lines
2.0 KiB
C#

using Emberend.Database;
using Emberend.Endpoints;
using Microsoft.EntityFrameworkCore;
using System;
using System.Net;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Any, 8085);
//options.Listen(IPAddress.Any, 4431, listenOptions =>
//{
// listenOptions.UseHttps("/etc/letsencrypt/live/x/letsencrypt.pfx", "");
//});
});
builder.Services.AddMemoryCache();
builder.Logging.ClearProviders();
builder.Services.AddDbContextPool<AppDbContext>(opt =>
{
opt.UseMySql(
builder.Configuration.GetConnectionString("Default"),
ServerVersion.AutoDetect(
builder.Configuration.GetConnectionString("Default")
)
);
});
builder.Services.AddAuthentication("auth")
.AddCookie("auth", o =>
{
o.Cookie.Name = "auth";
o.Cookie.HttpOnly = true;
o.Cookie.SameSite = SameSiteMode.Strict;
o.Events.OnRedirectToLogin = ctx =>
{
ctx.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
await DatabaseHelper.InitDatabaseAsync(app);
app.UseAuthentication();
app.UseAuthorization();
app.MapUserAccessEndpoints();
//app.UseHttpsRedirection();
app.MapGet("/", async (HttpContext context) =>
{
await context.Response.WriteAsync("Hello, alex-api.onlystefan.com");
});
app.MapGet("/test", async (HttpContext context) =>
{
await context.Response.WriteAsync("test");
});
app.Run();
public sealed record RegisterDto(string Email, string Password);
public sealed record LoginDto(string Email, string Password);
static class DatabaseHelper
{
public static async Task InitDatabaseAsync(WebApplication app)
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.EnsureCreatedAsync();
}
}