65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using QYZH.InteractiveMagazine.Infrastructure.Extensions;
|
||
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
|
||
using QYZH.InteractiveMagazine.Repository;
|
||
using Serilog;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 配置Serilog
|
||
Log.Logger = new LoggerConfiguration()
|
||
.ReadFrom.Configuration(builder.Configuration)
|
||
.Enrich.FromLogContext()
|
||
.WriteTo.Console()
|
||
.CreateLogger();
|
||
|
||
builder.Host.UseSerilog();
|
||
|
||
// 注册服务
|
||
builder.Services.AddControllers();
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwaggerGen();
|
||
|
||
// 基础设施服务注册(JWT、Redis、RabbitMQ)
|
||
builder.Services.AddInfrastructureServices(builder.Configuration);
|
||
|
||
// 注册中间件
|
||
builder.Services.AddTransient<GlobalExceptionMiddleware>();
|
||
builder.Services.AddTransient<OperationLogMiddleware>();
|
||
|
||
// 注册业务服务
|
||
builder.Services.AddScoped<QYZH.InteractiveMagazine.IService.IAuthService, QYZH.InteractiveMagazine.Service.AuthService>();
|
||
builder.Services.AddScoped<QYZH.InteractiveMagazine.IService.IWeChatMiniProgramService, QYZH.InteractiveMagazine.Service.WeChatMiniProgramService>();
|
||
|
||
// 初始化SqlSugar
|
||
SqlSugarDbContext.Init(builder.Configuration);
|
||
|
||
// 添加CORS
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddPolicy("AllowAll", policy =>
|
||
{
|
||
policy.AllowAnyOrigin()
|
||
.AllowAnyMethod()
|
||
.AllowAnyHeader();
|
||
});
|
||
});
|
||
|
||
var app = builder.Build();
|
||
|
||
// 配置HTTP请求管道
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseCors("AllowAll");
|
||
app.UseMiddleware<GlobalExceptionMiddleware>();
|
||
app.UseMiddleware<OperationLogMiddleware>();
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
app.MapControllers();
|
||
|
||
app.Run();
|