Files
WorkTime/src/WorkTime.Api/Program.cs
2025-03-04 13:56:38 +01:00

56 lines
1.7 KiB
C#

using MartinCostello.OpenApi;
using Microsoft.AspNetCore.Authentication;
using WorkTime.Api;
using WorkTime.Api.Logic;
using WorkTime.Api.Repositories;
using WorkTime.Api.Services;
using WorkTime.Shared.Repositories;
using WorkTime.Shared.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi(options => {
options.AddDocumentTransformer(AuthHandler.ConfigureOpenApi);
});
builder.Services.AddOpenApiExtensions(options => {
options.AddServerUrls = true;
});
builder.AddServiceDefaults();
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
builder.Services.AddHttpContextAccessor();
builder.Services.AddNpgsql<DatabaseContext>(builder.Configuration.GetConnectionString("data"));
builder.Services.AddScoped<IEntryRepository, EntryRepository>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<EntryLogic>();
builder.Services.AddAuthentication(nameof(AuthHandler))
.AddScheme<AuthenticationSchemeOptions, AuthHandler>(nameof(AuthHandler), _ => { });
builder.Services.AddAuthorization();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.MapOpenApi();
app.UseSwaggerUI(options => {
options.SwaggerEndpoint("/openapi/v1.json", "WorkTime.Api");
});
await using var scope = app.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
db.Database.EnsureCreated();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();