132 lines
3.8 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 Infrastructure.Extensions;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Middleware;
using ZR.Admin.WebApi.Hubs;
2023-03-08 13:02:38 +08:00
using ZR.Common.Cache;
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-11-28 17:08:48 +08:00
var corsUrls = builder.Configuration["corsUrls"]?.Split(',', StringSplitOptions.RemoveEmptyEntries);
2022-03-06 14:26:05 +08:00
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-11-28 17:08:48 +08:00
policy.WithOrigins(corsUrls == null ? Array.Empty<string>() : corsUrls)
2022-06-01 17:46:35 +08:00
.AllowAnyHeader()//允许任意头
.AllowCredentials()//允许cookie
.AllowAnyMethod();//允许任意方法
2022-03-06 14:26:05 +08:00
});
});
2023-03-08 13:02:38 +08:00
// 显示logo
builder.Services.AddLogo();
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
//普通验证码
2023-03-15 16:00:16 +08:00
builder.Services.AddCaptcha(builder.Configuration);
2022-03-06 14:26:05 +08:00
//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 服务
2023-03-08 13:02:38 +08:00
var openRedis = builder.Configuration["RedisServer:open"];
if (openRedis == "1")
2022-03-06 14:26:05 +08:00
{
2023-03-08 13:02:38 +08:00
RedisServer.Initalize();
}
2022-03-06 14:26:05 +08:00
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();
2022-07-09 15:51:59 +08:00
InternalApp.ServiceProvider = app.Services;
2022-07-01 22:39:39 +08:00
if (builder.Configuration["InitDb"].ParseToBool() == true)
{
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>();
2022-11-18 13:26:39 +08:00
//设置socket连接
app.MapHub<MessageHub>("/msgHub");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
2022-03-06 14:26:05 +08:00
app.MapControllers();
app.Run();