✨
This commit is contained in:
84
StopShopping.Api/Program.cs
Normal file
84
StopShopping.Api/Program.cs
Normal 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user