This commit is contained in:
john 2025-05-16 16:10:01 +02:00
parent 14fd359ea8
commit a4ef2b4a20
26 changed files with 331 additions and 78 deletions

View file

@ -2,11 +2,18 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Femto.Api;
using Femto.Api.Auth;
using Femto.Api.Middleware;
using Femto.Common;
using Femto.Common.Domain;
using Femto.Modules.Auth.Application;
using Femto.Modules.Blog.Application;
using Femto.Modules.Media.Application;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.WebUtilities;
using Serilog;
const string CorsPolicyName = "DefaultCorsPolicy";
@ -22,6 +29,7 @@ var blobStorageRoot = builder.Configuration.GetValue<string>("BlobStorageRoot");
if (blobStorageRoot is null)
throw new Exception("no blob storage root found");
builder.Services.InitializeBlogModule(connectionString);
builder.Services.InitializeMediaModule(connectionString, blobStorageRoot);
builder.Services.InitializeAuthenticationModule(connectionString);
@ -29,15 +37,16 @@ builder.Services.InitializeAuthenticationModule(connectionString);
builder.Services.AddScoped<CurrentUserContext, CurrentUserContext>();
builder.Services.AddScoped<ICurrentUserContext>(s => s.GetRequiredService<CurrentUserContext>());
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy(
CorsPolicyName,
b =>
{
b.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:5173");
b.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins("http://localhost:5173")
.AllowCredentials();
}
);
});
@ -60,12 +69,51 @@ builder
options => { }
);
builder.Services.AddAuthorization(); // if not already added
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseCors(CorsPolicyName);
app.UseAuthentication();
app.UseAuthorization();
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlerFeature = context.Features.Get<IExceptionHandlerFeature>();
var exception = exceptionHandlerFeature?.Error;
var problemDetailsFactory =
errorApp.ApplicationServices.GetRequiredService<ProblemDetailsFactory>();
var statusCode = exception switch
{
DomainError => 400,
_ => 500,
};
var message = exception switch
{
DomainError domainError => domainError.Message,
{ } e => e.Message,
_ => ReasonPhrases.GetReasonPhrase(statusCode),
};
var problemDetails = problemDetailsFactory.CreateProblemDetails(
httpContext: context,
title: "An error occurred",
detail: message,
statusCode: statusCode
);
// problemDetails.Extensions["traceId"] = context.TraceIdentifier;
context.Response.StatusCode = statusCode;
context.Response.ContentType = "application/problem+json";
await context.Response.WriteAsJsonAsync(problemDetails);
});
});
// app.UseMiddleware<ExceptionMapperMiddleware>();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();