asg_backend/asg_form/Program.cs

261 lines
8.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;
2024-08-10 19:53:32 +08:00
using Microsoft.Extensions.Hosting;
2024-10-02 12:35:39 +08:00
using Microsoft.Extensions.Options;
2024-08-03 20:40:34 +08:00
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Mirai.Net.Sessions;
using Mirai.Net.Sessions.Http.Managers;
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.Data;
2024-11-16 23:00:10 +08:00
using System.Drawing;
2024-08-03 20:40:34 +08:00
using System.Drawing.Drawing2D;
2024-10-19 00:43:22 +08:00
using System.Reflection;
2024-08-03 20:40:34 +08:00
using System.Text;
2024-10-19 00:43:22 +08:00
using Zack.EventBus;
2024-08-03 20:40:34 +08:00
2024-10-19 23:09:48 +08:00
2024-08-03 20:40:34 +08:00
Console.WriteLine("\n _____ _________ ________ \n / _ \\ / _____// _____/ \n / /_\\ \\ \\_____ \\/ \\ ___ \n/ | \\/ \\ \\_\\ \\\n\\____|__ /_______ /\\______ /\n \\/ \\/ \\/ \n__________ __ ___________ .___\n\\______ \\_____ ____ | | __\\_ _____/ ____ __| _/\n | | _/\\__ \\ _/ ___\\| |/ / | __)_ / \\ / __ | \n | | \\ / __ \\\\ \\___| < | \\ | \\/ /_/ | \n |______ /(____ /\\___ >__|_ \\/_______ /___| /\\____ | \n \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
var builder = WebApplication.CreateBuilder(args);
2024-08-17 18:31:23 +08:00
//builder.AddServiceDefaults();
2024-08-03 20:40:34 +08:00
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSignalR();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "ASG 赛事官网-后端API文档",
Version = "V 1.9.7",
Description = "这是由罗澜使用ASP.NET.Core开发的ASG赛事组后端系统包括官网和后台管理系统。使用 sqlserver作为数据库identity框架进行账号控制。",
});
var file = Path.Combine(AppContext.BaseDirectory, "ASG后端.xml");
var path = Path.Combine(AppContext.BaseDirectory, file);
c.IncludeXmlComments(path, true);
c.OrderActionsBy(o => o.RelativePath);
2025-01-27 20:32:23 +08:00
2024-08-03 20:40:34 +08:00
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);
var requirement = new OpenApiSecurityRequirement();
requirement[scheme] = new List<string>();
c.AddSecurityRequirement(requirement);
});
2025-01-27 20:32:23 +08:00
string[] urls = new[] { "https://idvasg.cn", "https://nocdn.idvasg.cn", "http://localhost:8080", "https://commentary.idvasg.cn", "https://admin.idvasg.cn", "https://www.idvasg.cn" };
2024-08-03 20:40:34 +08:00
builder.Services.AddCors(options =>
options.AddDefaultPolicy(builder => builder.WithOrigins(urls)
.AllowAnyMethod().AllowAnyHeader().AllowCredentials()));
IServiceCollection services = builder.Services;
services.AddDbContext<IDBcontext>(opt =>
{
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
});
2024-10-02 17:53:16 +08:00
//services.AddHostedService<qqbot>();
2024-08-03 20:40:34 +08:00
services.AddDataProtection();
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;
});
var idBuilder = new IdentityBuilder(typeof(User), typeof(Role), services);
idBuilder.AddEntityFrameworkStores<IDBcontext>()
.AddDefaultTokenProviders()
.AddRoleManager<RoleManager<Role>>()
.AddUserManager<UserManager<User>>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//add error 处理器
builder.Services.Configure<MvcOptions>(options =>
{
options.Filters.Add<MyExceptionFilter>();
});
services.Configure<JWTOptions>(builder.Configuration.GetSection("JWT"));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(x =>
{
var jwtOpt = builder.Configuration.GetSection("JWT").Get<JWTOptions>();
byte[] keyBytes = Encoding.UTF8.GetBytes(jwtOpt.SigningKey);
var secKey = new SymmetricSecurityKey(keyBytes);
x.TokenValidationParameters = new()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = secKey
};
});
2024-12-29 00:12:46 +08:00
services.AddSingleton(new GithubOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:github")));
services.AddSingleton(new MicrosoftOAuth(OAuthConfig.LoadFrom(builder.Configuration, "oauth:microsoft")));
2024-10-19 21:01:07 +08:00
builder.Services.AddApplicationInsightsTelemetry();
2024-08-03 20:40:34 +08:00
var app = builder.Build();
2024-08-17 16:00:05 +08:00
//app.MapDefaultEndpoints();
2024-08-03 20:40:34 +08:00
app.UseCors();
// Configure the HTTP request pipeline.
2025-01-27 20:32:23 +08:00
app.UseSwagger();
//app.UseSwaggerUI();
app.MapScalarApiReference(options =>
2024-08-03 20:40:34 +08:00
{
2025-01-27 20:32:23 +08:00
options.WithOpenApiRoutePattern("/swagger/v1/swagger.json");
// or
options.OpenApiRoutePattern = "/swagger/v1/swagger.json";
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
//发布时服务器注册静态资源
string fileUpload = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "loge");
if (!Directory.Exists(fileUpload))
{ Directory.CreateDirectory(fileUpload); }
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileUpload),
RequestPath = "/loge"
});
//发布时服务器注册静态资源
string fileUpload1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "video");
if (!Directory.Exists(fileUpload1))
{ Directory.CreateDirectory(fileUpload1); }
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileUpload1),
RequestPath = "/video"
});
//发布时服务器注册静态资源
string fileUpload2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "doc");
if (!Directory.Exists(fileUpload2))
2025-01-27 20:32:23 +08:00
{ Directory.CreateDirectory(fileUpload2 + "/rule"); }
2024-08-03 20:40:34 +08:00
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileUpload2),
RequestPath = "/doc"
});
string fileUpload5 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "excel");
if (!Directory.Exists(fileUpload5))
{ Directory.CreateDirectory(fileUpload5); }
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileUpload5),
RequestPath = "/excel"
});
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<room>("/room");
app.UseResponseCaching();
app.MapControllers();
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
});
// 安装一个适配器,并添加在这里。
// builder.Adapters.Add(new YourAdapter());
builder1.Adapters.AddOneBot(builder1.Configuration.GetSection("Adapters:OneBot"));
builder1.Plugins.Add<qqbot>();
var app1 = builder1.Build();
// 添加内置中间件。
// 这些中间件保证 Flandre 的正常运转。你也可以加入自己的中间件,并灵活调整其顺序。
app1.UseCommandSession();
// app.UseMiddleware(async (ctx, next) => { /* ... */ });
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
{
//输出详细全部信息
Console.WriteLine(ex.ToString());
2024-10-19 23:09:48 +08:00
2024-12-29 00:12:46 +08:00
}
2024-10-19 23:09:48 +08:00
2025-01-27 20:32:23 +08:00
2024-10-19 23:09:48 +08:00
})
{ IsBackground = true }.Start();
2024-11-04 11:03:25 +08:00
2024-08-03 20:40:34 +08:00
app.Run();