78 lines
1.9 KiB
C#
78 lines
1.9 KiB
C#
using API.Database;
|
|
using API.Models.Internal.User;
|
|
using API.Repository.AgeGroup;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
var webRoot = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "wwwroot"));
|
|
|
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
|
|
{
|
|
Args = args,
|
|
WebRootPath = webRoot
|
|
});
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// Authentication
|
|
builder.Services.AddAuthorization();
|
|
builder.Services.AddAuthentication().AddCookie(IdentityConstants.ApplicationScheme);
|
|
|
|
builder.Services.AddIdentityCore<User>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddApiEndpoints();
|
|
|
|
// Database
|
|
var postgreConnection = builder.Configuration.GetConnectionString("PostgresConnection");
|
|
if (!string.IsNullOrEmpty(postgreConnection))
|
|
{
|
|
// Nutze PostgresSQL
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(postgreConnection));
|
|
}
|
|
else
|
|
{
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite("Data Source=app.db"));
|
|
}
|
|
|
|
// Adding Database Services
|
|
builder.Services.AddScoped<IAgeGroupService, AgeGroupService>();
|
|
|
|
// Adding S3 Services
|
|
|
|
// Adding (latler) Redis Services
|
|
|
|
// Add Database Services
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
using(var scope = app.Services.CreateScope())
|
|
{
|
|
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
dbContext.Database.Migrate();
|
|
}
|
|
|
|
// Deliver frontend
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
// Map Auth Endpoints
|
|
app.MapIdentityApi<User>();
|
|
|
|
app.MapControllers();
|
|
// Frontend Fallback
|
|
app.MapFallbackToFile("index.html");
|
|
|
|
app.Run();
|