asg_backend/asg_form/Program.cs

186 lines
6.3 KiB
C#
Raw Normal View History

2024-08-03 20:40:34 +08:00
using asg_form;
using asg_form.Controllers;
using asg_form.Controllers.Hubs;
2024-10-19 14:20:39 +08:00
using Flandre.Adapters.OneBot.Extensions;
using Flandre.Framework;
2024-08-03 20:40:34 +08:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2024-11-10 12:24:46 +08:00
using Microsoft.AspNetCore.HttpOverrides;
2024-08-03 20:40:34 +08:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Mirai.Net.Sessions;
2024-12-29 00:12:46 +08:00
using MrHuo.OAuth;
using MrHuo.OAuth.Github;
using MrHuo.OAuth.Microsoft;
2025-01-27 00:16:06 +08:00
using Scalar.AspNetCore;
2024-08-03 20:40:34 +08:00
using System;
using System.Text;
2024-10-19 00:43:22 +08:00
using Zack.EventBus;
2024-08-03 20:40:34 +08:00
Console.WriteLine("\n _____ _________ ________ \n / _ \\ / _____// _____/ \n / /_\\ \\ \\_____ \\/ \\ ___ \n/ | \\/ \\ \\_\\ \\\n\\____|__ /_______ /\\______ /\n \\/ \\/ \\/ \n__________ __ ___________ .___\n\\______ \\_____ ____ | | __\\_ _____/ ____ __| _/\n | | _/\\__ \\ _/ ___\\| |/ / | __)_ / \\ / __ | \n | | \\ / __ \\\\ \\___| < | \\ | \\/ /_/ | \n |______ /(____ /\\___ >__|_ \\/_______ /___| /\\____ | \n \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
2025-04-04 12:02:11 +08:00
var builder = WebApplication.CreateBuilder(args);
2024-08-03 20:40:34 +08:00
2025-04-04 12:02:11 +08:00
// 添加控制器服务
2024-08-03 20:40:34 +08:00
builder.Services.AddControllers();
2025-04-04 12:02:11 +08:00
// 配置Swagger/OpenAPI
2024-08-03 20:40:34 +08:00
builder.Services.AddSwaggerGen(c =>
{
2025-04-04 12:02:11 +08:00
c.SwaggerDoc("v1", new OpenApiInfo
2024-08-03 20:40:34 +08:00
{
Title = "ASG 赛事官网-后端API文档",
Version = "V 1.9.7",
Description = "这是由罗澜使用ASP.NET.Core开发的ASG赛事组后端系统包括官网和后台管理系统。使用 sqlserver作为数据库identity框架进行账号控制。",
});
var file = Path.Combine(AppContext.BaseDirectory, "ASG后端.xml");
2025-04-04 12:02:11 +08:00
c.IncludeXmlComments(file, true);
2024-08-03 20:40:34 +08:00
c.OrderActionsBy(o => o.RelativePath);
2025-01-27 20:32:23 +08:00
2025-04-04 12:02:11 +08:00
var scheme = new OpenApiSecurityScheme
2024-08-03 20:40:34 +08:00
{
Description = "Authorization header. \r\nExample: 'Bearer 12345abcdef'",
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Authorization"
},
Scheme = "oauth2",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
};
c.AddSecurityDefinition("Authorization", scheme);
2025-04-04 12:02:11 +08:00
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
[scheme] = new List<string>()
});
2024-08-03 20:40:34 +08:00
});
2025-04-04 12:02:11 +08:00
// 配置CORS策略
2024-08-03 20:40:34 +08:00
builder.Services.AddCors(options =>
2025-04-04 12:02:11 +08:00
options.AddDefaultPolicy(policy =>
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()));
// 配置数据库上下文
builder.Services.AddDbContext<IDBcontext>(opt =>
2024-08-03 20:40:34 +08:00
{
2025-03-23 01:51:52 +08:00
string connStr = @"Host=172.30.121.91;Port=2345;Database=postgres;Username=asg;Password=luolan12323;";
2024-10-02 12:35:39 +08:00
opt.UseNpgsql(connStr);
2024-08-03 20:40:34 +08:00
});
2025-04-04 12:02:11 +08:00
// 配置身份验证和授权
builder.Services.AddIdentityCore<User>(options =>
2024-08-03 20:40:34 +08:00
{
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
});
2025-04-04 12:02:11 +08:00
new IdentityBuilder(typeof(User), typeof(Role), builder.Services)
.AddEntityFrameworkStores<IDBcontext>()
2024-08-03 20:40:34 +08:00
.AddDefaultTokenProviders()
.AddRoleManager<RoleManager<Role>>()
.AddUserManager<UserManager<User>>();
2025-04-04 12:02:11 +08:00
// 配置JWT身份验证
builder.Services.Configure<JWTOptions>(builder.Configuration.GetSection("JWT"));
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(x =>
{
var jwtOpt = builder.Configuration.GetSection("JWT").Get<JWTOptions>();
byte[] keyBytes = Encoding.UTF8.GetBytes(jwtOpt.SigningKey);
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(keyBytes)
};
});
2024-08-03 20:40:34 +08:00
2025-04-04 12:02:11 +08:00
// 配置OAuth
builder.Services.AddSingleton(new GithubOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:github")));
builder.Services.AddSingleton(new MicrosoftOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:microsoft")));
2024-08-03 20:40:34 +08:00
2025-04-04 12:02:11 +08:00
// 添加Application Insights
builder.Services.AddApplicationInsightsTelemetry();
2024-08-03 20:40:34 +08:00
2025-04-04 12:02:11 +08:00
// 配置全局异常处理过滤器
2024-08-03 20:40:34 +08:00
builder.Services.Configure<MvcOptions>(options =>
{
options.Filters.Add<MyExceptionFilter>();
});
2025-04-04 12:02:11 +08:00
// 构建应用程序
2024-08-03 20:40:34 +08:00
var app = builder.Build();
2025-04-04 12:02:11 +08:00
// 使用中间件
app.UseCors();
2025-01-27 20:32:23 +08:00
app.UseSwagger();
2024-08-03 20:40:34 +08:00
app.UseStaticFiles();
2024-11-04 11:03:25 +08:00
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
2024-08-03 20:40:34 +08:00
2025-04-04 12:02:11 +08:00
// 注册静态资源
void RegisterStaticFiles(string folderName, string requestPath)
2024-08-03 20:40:34 +08:00
{
2025-04-04 12:02:11 +08:00
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, folderName);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(folderPath),
RequestPath = requestPath
});
}
RegisterStaticFiles("loge", "/loge");
RegisterStaticFiles("video", "/video");
RegisterStaticFiles("doc", "/doc");
RegisterStaticFiles("excel", "/excel");
2024-08-03 20:40:34 +08:00
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<room>("/room");
app.UseResponseCaching();
app.MapControllers();
2025-04-04 12:02:11 +08:00
// 启动Flandre机器人线程
2024-10-19 23:09:48 +08:00
new Thread(o =>
{
2024-12-29 00:12:46 +08:00
try
2024-10-19 23:09:48 +08:00
{
2024-12-29 00:12:46 +08:00
var builder1 = FlandreApp.CreateBuilder(new HostApplicationBuilderSettings
{
Args = args,
ContentRootPath = AppDomain.CurrentDomain.BaseDirectory
});
builder1.Adapters.AddOneBot(builder1.Configuration.GetSection("Adapters:OneBot"));
builder1.Plugins.Add<qqbot>();
var app1 = builder1.Build();
app1.UseCommandSession();
app1.UseCommandParser();
app1.UseCommandInvoker();
runbot.runbotr = app1.Bots.First();
app1.Run();
}
2025-01-27 20:32:23 +08:00
catch (Exception ex)
2024-12-29 00:12:46 +08:00
{
2025-04-04 12:02:11 +08:00
}
2024-10-19 23:09:48 +08:00
})
{ IsBackground = true }.Start();
2024-08-03 20:40:34 +08:00
app.Run();