126 lines
3.6 KiB
C#
Raw Normal View History

2022-03-06 14:26:05 +08:00
using Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using ZR.Admin.WebApi.Framework;
using Hei.Captcha;
using Infrastructure.Extensions;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Middleware;
using ZR.Admin.WebApi.Hubs;
2021-08-23 16:57:25 +08:00
2022-03-06 14:26:05 +08:00
var builder = WebApplication.CreateBuilder(args);
// 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.AddSwaggerGen();
2022-06-01 17:46:35 +08:00
//注入HttpContextAccessor
2022-03-06 14:26:05 +08:00
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2022-06-01 17:46:35 +08:00
//配置跨域
2022-03-06 14:26:05 +08:00
builder.Services.AddCors(c =>
2021-08-23 16:57:25 +08:00
{
2022-03-06 14:26:05 +08:00
c.AddPolicy("Policy", policy =>
2021-08-23 16:57:25 +08:00
{
2022-03-06 14:26:05 +08:00
policy.WithOrigins(builder.Configuration["corsUrls"].Split(',', StringSplitOptions.RemoveEmptyEntries))
2022-06-01 17:46:35 +08:00
.AllowAnyHeader()//允许任意头
.AllowCredentials()//允许cookie
.AllowAnyMethod();//允许任意方法
2022-03-06 14:26:05 +08:00
});
});
2022-06-01 17:46:35 +08:00
//注入SignalR实时通讯默认用json传输
2022-03-06 14:26:05 +08:00
builder.Services.AddSignalR();
2022-06-01 17:46:35 +08:00
//消除Error unprotecting the session cookie警告
2022-03-06 14:26:05 +08:00
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
2022-06-01 17:46:35 +08:00
//普通验证码
2022-03-06 14:26:05 +08:00
builder.Services.AddHeiCaptcha();
//builder.Services.AddSession();
builder.Services.AddHttpContextAccessor();
2022-06-01 17:46:35 +08:00
//绑定整个对象到Model上
2022-03-06 14:26:05 +08:00
builder.Services.Configure<OptionsSetting>(builder.Configuration);
2022-06-01 17:46:35 +08:00
//jwt 认证
2022-03-06 14:26:05 +08:00
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddCookie()
.AddJwtBearer(o =>
{
o.TokenValidationParameters = JwtUtil.ValidParameters();
});
//InternalApp.InternalServices = builder.Services;
2022-03-06 14:26:05 +08:00
builder.Services.AddAppService();
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
2022-06-01 17:46:35 +08:00
//开启计划任务
2022-03-06 14:26:05 +08:00
builder.Services.AddTaskSchedulers();
2022-06-01 17:46:35 +08:00
//初始化db
2022-03-06 14:26:05 +08:00
DbExtension.AddDb(builder.Configuration);
2022-06-01 17:46:35 +08:00
//注册REDIS 服务
2022-03-06 14:26:05 +08:00
Task.Run(() =>
{
//RedisServer.Initalize();
});
builder.Services.AddMvc(options =>
{
2022-06-01 17:46:35 +08:00
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
2022-03-06 14:26:05 +08:00
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
});
builder.Services.AddSwaggerConfig();
var app = builder.Build();
//InternalApp.ServiceProvider = app.Services;
//app.Services.InitDb();
2022-05-19 18:34:52 +08:00
app.UseSwagger();
2022-06-01 17:46:35 +08:00
//使可以多次多去body内容
2022-03-06 14:26:05 +08:00
app.Use((context, next) =>
{
context.Request.EnableBuffering();
if (context.Request.Query.TryGetValue("access_token", out var token))
{
context.Request.Headers.Add("Authorization", $"Bearer {token}");
}
return next();
});
2022-06-01 17:46:35 +08:00
//开启访问静态文件/wwwroot目录文件要放在UseRouting前面
2022-03-06 14:26:05 +08:00
app.UseStaticFiles();
2022-06-01 17:46:35 +08:00
//开启路由访问
2022-03-06 14:26:05 +08:00
app.UseRouting();
2022-06-01 17:46:35 +08:00
app.UseCors("Policy");//要放在app.UseEndpoints前。
2022-03-06 14:26:05 +08:00
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
2022-06-01 17:46:35 +08:00
//开启缓存
2022-03-06 14:26:05 +08:00
app.UseResponseCaching();
2022-06-01 17:46:35 +08:00
//恢复/启动任务
2022-03-06 14:26:05 +08:00
app.UseAddTaskSchedulers();
2022-06-01 17:46:35 +08:00
//使用全局异常中间件
2022-03-06 14:26:05 +08:00
app.UseMiddleware<GlobalExceptionMiddleware>();
app.UseEndpoints(endpoints =>
{
2022-06-01 17:46:35 +08:00
//设置socket连接
2022-03-06 14:26:05 +08:00
endpoints.MapHub<MessageHub>("/msgHub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.MapControllers();
app.Run();