186 lines
6.3 KiB
C#
186 lines
6.3 KiB
C#
using asg_form;
|
||
using asg_form.Controllers;
|
||
using asg_form.Controllers.Hubs;
|
||
using Flandre.Adapters.OneBot.Extensions;
|
||
using Flandre.Framework;
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
using Microsoft.AspNetCore.HttpOverrides;
|
||
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;
|
||
using MrHuo.OAuth;
|
||
using MrHuo.OAuth.Github;
|
||
using MrHuo.OAuth.Microsoft;
|
||
using Scalar.AspNetCore;
|
||
using System;
|
||
using System.Text;
|
||
using Zack.EventBus;
|
||
|
||
Console.WriteLine("\n _____ _________ ________ \n / _ \\ / _____// _____/ \n / /_\\ \\ \\_____ \\/ \\ ___ \n/ | \\/ \\ \\_\\ \\\n\\____|__ /_______ /\\______ /\n \\/ \\/ \\/ \n__________ __ ___________ .___\n\\______ \\_____ ____ | | __\\_ _____/ ____ __| _/\n | | _/\\__ \\ _/ ___\\| |/ / | __)_ / \\ / __ | \n | | \\ / __ \\\\ \\___| < | \\ | \\/ /_/ | \n |______ /(____ /\\___ >__|_ \\/_______ /___| /\\____ | \n \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 添加控制器服务
|
||
builder.Services.AddControllers();
|
||
|
||
// 配置Swagger/OpenAPI
|
||
builder.Services.AddSwaggerGen(c =>
|
||
{
|
||
c.SwaggerDoc("v1", new OpenApiInfo
|
||
{
|
||
Title = "ASG 赛事官网-后端API文档",
|
||
Version = "V 1.9.7",
|
||
Description = "这是由罗澜使用ASP.NET.Core开发的ASG赛事组后端系统,包括官网和后台管理系统。使用 sqlserver作为数据库,identity框架进行账号控制。",
|
||
});
|
||
var file = Path.Combine(AppContext.BaseDirectory, "ASG后端.xml");
|
||
c.IncludeXmlComments(file, true);
|
||
c.OrderActionsBy(o => o.RelativePath);
|
||
|
||
var scheme = new OpenApiSecurityScheme
|
||
{
|
||
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);
|
||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||
{
|
||
[scheme] = new List<string>()
|
||
});
|
||
});
|
||
|
||
// 配置CORS策略
|
||
builder.Services.AddCors(options =>
|
||
options.AddDefaultPolicy(policy =>
|
||
policy.AllowAnyOrigin()
|
||
.AllowAnyMethod()
|
||
.AllowAnyHeader()
|
||
.AllowCredentials()));
|
||
|
||
// 配置数据库上下文
|
||
builder.Services.AddDbContext<IDBcontext>(opt =>
|
||
{
|
||
string connStr = @"Host=172.30.121.91;Port=2345;Database=postgres;Username=asg;Password=luolan12323;";
|
||
opt.UseNpgsql(connStr);
|
||
});
|
||
|
||
// 配置身份验证和授权
|
||
builder.Services.AddIdentityCore<User>(options =>
|
||
{
|
||
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;
|
||
});
|
||
new IdentityBuilder(typeof(User), typeof(Role), builder.Services)
|
||
.AddEntityFrameworkStores<IDBcontext>()
|
||
.AddDefaultTokenProviders()
|
||
.AddRoleManager<RoleManager<Role>>()
|
||
.AddUserManager<UserManager<User>>();
|
||
|
||
// 配置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)
|
||
};
|
||
});
|
||
|
||
// 配置OAuth
|
||
builder.Services.AddSingleton(new GithubOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:github")));
|
||
builder.Services.AddSingleton(new MicrosoftOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:microsoft")));
|
||
|
||
// 添加Application Insights
|
||
builder.Services.AddApplicationInsightsTelemetry();
|
||
|
||
// 配置全局异常处理过滤器
|
||
builder.Services.Configure<MvcOptions>(options =>
|
||
{
|
||
options.Filters.Add<MyExceptionFilter>();
|
||
});
|
||
|
||
// 构建应用程序
|
||
var app = builder.Build();
|
||
|
||
// 使用中间件
|
||
app.UseCors();
|
||
app.UseSwagger();
|
||
app.UseStaticFiles();
|
||
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
|
||
|
||
// 注册静态资源
|
||
void RegisterStaticFiles(string folderName, string requestPath)
|
||
{
|
||
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");
|
||
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
app.MapHub<room>("/room");
|
||
app.UseResponseCaching();
|
||
app.MapControllers();
|
||
|
||
// 启动Flandre机器人线程
|
||
new Thread(o =>
|
||
{
|
||
try
|
||
{
|
||
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();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
})
|
||
{ IsBackground = true }.Start();
|
||
|
||
app.Run(); |