111 lines
3.1 KiB
C#
111 lines
3.1 KiB
C#
using Seasoned.Backend.Services;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Seasoned.Backend.Data;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using DotNetEnv;
|
|
|
|
Env.Load("../.env");
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddScoped<IRecipeService, RecipeService>();
|
|
|
|
builder.Services.AddIdentityApiEndpoints<IdentityUser>( options => {
|
|
options.Password.RequireDigit = false;
|
|
options.Password.RequiredLength = 6;
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
options.Password.RequireUppercase = false;
|
|
options.Password.RequireLowercase = false;
|
|
options.User.RequireUniqueEmail = true;
|
|
})
|
|
.AddEntityFrameworkStores<ApplicationDbContext>();
|
|
|
|
builder.Services.ConfigureApplicationCookie(options =>
|
|
{
|
|
options.Cookie.Name = "Seasoned.Session";
|
|
options.Cookie.HttpOnly = true;
|
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
|
|
options.Cookie.MaxAge = options.ExpireTimeSpan;
|
|
options.SlidingExpiration = true;
|
|
options.Events.OnRedirectToLogin = context =>
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
return Task.CompletedTask;
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddControllers()
|
|
.AddJsonOptions(options => {
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
|
});
|
|
|
|
builder.Services.AddOpenApi();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("SeasonedOriginPolicy", policy =>
|
|
{
|
|
policy.WithOrigins("https://seasoned.ddns.net", "https://www.seasoned.ddns.net")
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"),
|
|
o => o.UseVector()));
|
|
|
|
var app = builder.Build();
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var services = scope.ServiceProvider;
|
|
var db = services.GetRequiredService<ApplicationDbContext>();
|
|
|
|
try
|
|
{
|
|
if (db.Database.GetPendingMigrations().Any())
|
|
{
|
|
db.Database.Migrate();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Migration notice: {ex.Message}");
|
|
}
|
|
|
|
try
|
|
{
|
|
Console.WriteLine("--> Checking if Seeding is needed...");
|
|
DbInitializer.Initialize(db);
|
|
Console.WriteLine("--> Database Seed Completed!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Seeding failed: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
app.UseCors("SeasonedOriginPolicy");
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.MapGroup("/api/auth").MapIdentityApi<IdentityUser>();
|
|
app.MapControllers();
|
|
app.MapFallbackToFile("index.html");
|
|
app.Run(); |