initial commit

This commit is contained in:
2026-03-20 23:52:10 +01:00
parent 05bea695bd
commit ce04cd8d77
38 changed files with 3006 additions and 52 deletions

View File

@@ -1,13 +1,87 @@
var builder = WebApplication.CreateBuilder(args);
using CityInfo.API.DbContexts;
using CityInfo.API.Models;
using CityInfo.API.Services;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/cityinfo.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
//builder.logging.clearproviders();
//builder.logging.addconsole();
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddControllers((options) =>
{
options.ReturnHttpNotAcceptable = true;
})
.AddNewtonsoftJson()
.AddXmlDataContractSerializerFormatters();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddSingleton<FileExtensionContentTypeProvider>();
builder.Services.AddSingleton<CityDataStore>();
builder.Services.AddDbContext<CityInfoContext>(
(dbContextOptins) => dbContextOptins.UseSqlite(
builder.Configuration["ConnectionStrings:CityInfoDBConnectionString"]
)
);
builder.Services.AddScoped<ICityInfoRepository, CityInfoRepository>();
builder.Services.AddProblemDetails();
//builder.Services.AddProblemDetails((options) =>
// {
// options.CustomizeProblemDetails = ctx =>
// {
// ctx.ProblemDetails.Extensions.Add("additionalInfo", "Addional info example");
// ctx.ProblemDetails.Extensions.Add("server", Environment.MachineName);
// };
// });
#if DEBUG
builder.Services.AddTransient<IMailService, LocalMailService>();
#else
builder.Services.AddTransient<IMailService, CloudMailService>();
#endif
builder.Services.AddAutoMapper(cfg => { }, AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Authentication:Issuer"],
ValidAudience = builder.Configuration["Authentication:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(builder.Configuration["Authentication:SecretForKey"]))
};
}
);
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("MustBeFromCharleroi", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("city", "Charleroi");
});
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@@ -21,8 +95,15 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();