This commit is contained in:
2026-03-25 14:55:34 +08:00
commit 2c44b3a4b2
131 changed files with 7453 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Scalar.AspNetCore;
using Serilog;
using StopShopping.Api.Extensions;
using StopShopping.Api.Routes;
using StopShopping.Api.Workers;
const string CORS_POLICY = "default";
// 将启动日志写入控制台用于捕获启动时异常启动后WriteTo被后续配置替代
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilog((services, seriLogger) =>
{
seriLogger.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services);
});
var jwtConfiguration = builder.Configuration.GetSection("JwtOptions");
var appConfiguration = builder.Configuration.GetSection("AppOptions");
builder.Services.AddCommonServices(
CORS_POLICY,
jwtConfiguration,
appConfiguration,
builder.Environment.IsDevelopment());
builder.Services.AddServices(dbContextOptions =>
{
dbContextOptions.UseNpgsql(
builder.Configuration.GetConnectionString("StopShopping"));
if (builder.Environment.IsDevelopment())
dbContextOptions.EnableSensitiveDataLogging();
},
appConfiguration,
builder.Configuration.GetSection("OpenPlatformOptions"));
builder.Services.AddHostedService<DbSeederBackgroundService>();
/**********************************************************************/
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDevelopmentCookie();
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.AddPreferredSecuritySchemes(JwtBearerDefaults.AuthenticationScheme);
});
}
app.UseGlobalExceptionHandler();
app.UseRouting();
app.UseCors(CORS_POLICY);
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets().ShortCircuit();
Root.MapRoutes(app);
app.UseAntiforgery();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "启动异常!");
}
finally
{
Log.CloseAndFlush();
}