110 lines
2.8 KiB
C#
110 lines
2.8 KiB
C#
|
||
using Microsoft.OpenApi.Models;
|
||
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
builder.Services.Inject(new List<string> {
|
||
"Application.Web","Application.Domain"
|
||
});//框架初始化
|
||
|
||
builder.Configuration.RegisterConfig();//注入配置
|
||
builder.Services.InjectMemory();
|
||
builder.Services.InjectSql(builder.Configuration);
|
||
|
||
#region JWT与接口配置
|
||
|
||
Dictionary<string, OpenApiInfo> groups = new Dictionary<string, OpenApiInfo>();
|
||
groups.Add("Login", new OpenApiInfo
|
||
{
|
||
Title = "登录接口文档",
|
||
Version = "v1",
|
||
Description = "航海时代登录相关接口。",
|
||
});
|
||
groups.Add("User", new OpenApiInfo
|
||
{
|
||
Title = "用户接口文档",
|
||
Version = "v1",
|
||
Description = "航海时代用户相关接口。",
|
||
});
|
||
|
||
builder.Services.InjectJwt(builder.Configuration, new OpenApiInfo
|
||
{
|
||
Title = "航海时代接口文档",
|
||
Version = "v1",
|
||
Description = "航海时代接口文档,包含航海时代的所有接口,项目采用AI开发。",
|
||
}, "Application.Web", op =>
|
||
{
|
||
op.AddTransient<IJwtValidator, Application.Web.JwtHandle>();
|
||
}, groups);
|
||
|
||
#endregion
|
||
|
||
//定时功能
|
||
builder.Services.InjectTimer(services =>
|
||
{
|
||
services.AddTransient<ITimerJobManager, TimerJobManager>();//注册管理器
|
||
services.AddSingleton<AutoJob>();
|
||
services.AddHostedService<JobStart>();
|
||
});
|
||
|
||
//日志
|
||
builder.Logging.InjectLog();
|
||
// Add services to the container.
|
||
|
||
|
||
builder.Services.AddControllers();
|
||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||
|
||
#region 配置跨域处理,允许所有来源
|
||
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddPolicy("all", builder =>
|
||
{
|
||
builder.AllowAnyOrigin() //允许任何来源的主机访问
|
||
.AllowAnyMethod()
|
||
.AllowAnyHeader();
|
||
});
|
||
});
|
||
|
||
#endregion 配置跨域处理,允许所有来源
|
||
|
||
var app = builder.Build();
|
||
|
||
app.Services.UseInject();//启用框架
|
||
app.Configuration.UseConfig();//启用配置
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseExceptionHandler("/Home/Error");
|
||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||
app.UseHsts();
|
||
|
||
|
||
#region 测试swagger配置
|
||
app.UseJwtSwagger("swagger", "航海时代接口文档",new List<JwtGroupConfig> {
|
||
new JwtGroupConfig(){ groupId="Login", groupName="登录接口文档"},
|
||
new JwtGroupConfig(){ groupId="User", groupName="用户接口文档"}
|
||
});
|
||
|
||
#endregion
|
||
}
|
||
|
||
app.UseCors("all");
|
||
app.UseStaticFiles();
|
||
app.UseHttpsRedirection();
|
||
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
app.MapControllers();
|
||
|
||
app.MapControllerRoute(
|
||
name: "default",
|
||
pattern: "{controller=Home}/{action=Index}/{id?}")
|
||
.WithStaticAssets();
|
||
|
||
SnowflakeAssist.Initialize(0, 0);
|
||
app.Run();
|