132 lines
3.8 KiB
C#
132 lines
3.8 KiB
C#
using Seasoned.Backend.Services;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using System.Text.Json;
|
|
using System.Text;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Seasoned.Backend.Data;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using DotNetEnv;
|
|
|
|
Env.Load("../.env");
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.Configuration.AddEnvironmentVariables();
|
|
|
|
var jwtKey = builder.Configuration.GetValue<string>("Jwt:Key");
|
|
|
|
if (string.IsNullOrWhiteSpace(jwtKey))
|
|
{
|
|
throw new InvalidOperationException("CRITICAL: JWT Key is missing or empty! Check your .env/Docker environment mapping.");
|
|
}
|
|
|
|
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "SeasonedAPI";
|
|
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "SeasonedFrontend";
|
|
|
|
builder.Services.AddAuthentication(options => {
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
|
|
.AddJwtBearer(options => {
|
|
options.TokenValidationParameters = new TokenValidationParameters {
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtIssuer,
|
|
ValidAudience = jwtAudience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
|
};
|
|
});
|
|
|
|
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>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
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")
|
|
.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.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
});
|
|
|
|
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(); |