117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using Quartz;
|
||
using FutureMailAPI.Data;
|
||
using FutureMailAPI.Helpers;
|
||
using FutureMailAPI.Services;
|
||
using FutureMailAPI.Middleware;
|
||
using FutureMailAPI.Extensions;
|
||
using Microsoft.Extensions.FileProviders;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 配置服务器监听所有网络接口的5001端口
|
||
builder.WebHost.ConfigureKestrel(options =>
|
||
{
|
||
options.ListenAnyIP(5001);
|
||
});
|
||
|
||
// 配置数据库连接
|
||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||
builder.Services.AddDbContext<FutureMailDbContext>(options =>
|
||
options.UseSqlite(connectionString));
|
||
|
||
// 配置OAuth 2.0认证
|
||
// 注意:我们使用自定义中间件实现OAuth 2.0认证,而不是使用内置的JWT认证
|
||
|
||
// 配置Swagger/OpenAPI
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwaggerGen(c =>
|
||
{
|
||
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "FutureMail API", Version = "v1" });
|
||
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
|
||
|
||
// 添加文件上传支持
|
||
c.OperationFilter<FileUploadOperationFilter>();
|
||
});
|
||
|
||
// 注册服务
|
||
builder.Services.AddScoped<IPasswordHelper, PasswordHelper>();
|
||
builder.Services.AddScoped<IUserService, UserService>();
|
||
builder.Services.AddScoped<IMailService, MailService>();
|
||
builder.Services.AddScoped<ITimeCapsuleService, TimeCapsuleService>();
|
||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||
builder.Services.AddScoped<IAIAssistantService, AIAssistantService>();
|
||
builder.Services.AddScoped<IPersonalSpaceService, PersonalSpaceService>();
|
||
builder.Services.AddScoped<IFileUploadService, FileUploadService>();
|
||
builder.Services.AddScoped<INotificationService, NotificationService>();
|
||
builder.Services.AddScoped<IOAuthService, OAuthService>();
|
||
builder.Services.AddScoped<IInitializationService, InitializationService>();
|
||
|
||
// 配置Quartz任务调度
|
||
builder.Services.AddQuartz(q =>
|
||
{
|
||
// 注册邮件投递任务
|
||
var jobKey = new JobKey("MailDeliveryJob");
|
||
q.AddJob<MailDeliveryJob>(opts => opts.WithIdentity(jobKey));
|
||
|
||
// 创建触发器 - 每分钟执行一次
|
||
q.AddTrigger(opts => opts
|
||
.ForJob(jobKey)
|
||
.WithIdentity("MailDeliveryJob-trigger")
|
||
.WithCronSchedule("0 * * ? * *")); // 每分钟执行一次
|
||
});
|
||
|
||
// 添加Quartz主机服务
|
||
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
|
||
|
||
// 添加控制器
|
||
builder.Services.AddControllers();
|
||
|
||
// 添加CORS
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddPolicy("AllowAll", builder =>
|
||
{
|
||
builder.AllowAnyOrigin()
|
||
.AllowAnyMethod()
|
||
.AllowAnyHeader();
|
||
});
|
||
});
|
||
|
||
var app = builder.Build();
|
||
|
||
// 初始化系统数据
|
||
using (var scope = app.Services.CreateScope())
|
||
{
|
||
var initializationService = scope.ServiceProvider.GetRequiredService<IInitializationService>();
|
||
await initializationService.InitializeAsync();
|
||
}
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
|
||
// 配置静态文件服务
|
||
app.UseStaticFiles();
|
||
app.UseStaticFiles(new StaticFileOptions
|
||
{
|
||
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "uploads")),
|
||
RequestPath = "/uploads"
|
||
});
|
||
|
||
app.UseCors("AllowAll");
|
||
|
||
// 添加OAuth 2.0认证中间件
|
||
app.UseMiddleware<OAuthAuthenticationMiddleware>();
|
||
|
||
app.UseAuthorization();
|
||
|
||
app.MapControllers();
|
||
|
||
app.Run();
|