Foxnouns.NET/Foxnouns.Backend/Program.cs

52 lines
1.2 KiB
C#
Raw Normal View History

using Foxnouns.Backend;
2024-05-27 15:53:54 +02:00
using Foxnouns.Backend.Database;
using Serilog;
using Foxnouns.Backend.Extensions;
using Microsoft.AspNetCore.Mvc;
2024-05-27 15:53:54 +02:00
using Newtonsoft.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
var config = builder.AddConfiguration();
builder.AddSerilog(config.LogEventLevel);
builder.Services
.AddControllers()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = actionContext => new BadRequestObjectResult(
new ApiError.BadRequest("Bad request", actionContext.ModelState).ToJson()
);
});
2024-05-27 15:53:54 +02:00
builder.Services
.AddDbContext<DatabaseContext>()
.AddCustomServices()
.AddCustomMiddleware()
2024-05-27 15:53:54 +02:00
.AddEndpointsApiExplorer()
.AddSwaggerGen();
var app = builder.Build();
await app.Initialize(args);
2024-05-27 15:53:54 +02:00
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors();
app.UseCustomMiddleware();
2024-05-27 15:53:54 +02:00
app.MapControllers();
app.Urls.Clear();
app.Urls.Add(config.Address);
app.Run();
Log.CloseAndFlush();