| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 | 
							
- using StackExchange.Redis;
 
- using Autofac.Core;
 
- var builder = WebApplication.CreateBuilder(args);
 
- var basePath = AppContext.BaseDirectory;
 
- //引入配置文件
 
- var _config = new ConfigurationBuilder()
 
-                  .SetBasePath(basePath)
 
-                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
 
-                  .Build();
 
- builder.Services.AddSingleton(new AppSettingsHelper(_config));
 
- // Add services to the container.
 
- builder.Services.AddControllersWithViews();
 
- #region redis
 
- //redis缓存
 
- var section = builder.Configuration.GetSection("Redis:Default");
 
- //连接字符串
 
- string _connectionString = section.GetSection("Connection").Value;
 
- //实例名称
 
- string _instanceName = section.GetSection("InstanceName").Value;
 
- //默认数据库 
 
- int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
 
- builder.Services.AddSingleton(new RedisHelper(_connectionString+",password=123456", _instanceName, _defaultDB));
 
- #endregion
 
- #region 接口分组
 
- var groups = new List<Tuple<string, string>>
 
- {
 
-     //new Tuple<string, string>("Group1","分组一"),
 
-     //new Tuple<string, string>("Group2","分组二")
 
- };
 
- #endregion
 
- #region 注入数据库
 
- builder.Services.AddScoped(options =>
 
- {
 
-     return new SqlSugarClient(new List<ConnectionConfig>()
 
-     {
 
-         new ConnectionConfig() { 
 
-             ConfigId = DBEnum.OA2023DB, 
 
-             ConnectionString = _config.GetConnectionString("OA2023DB"), 
 
-             DbType = DbType.SqlServer, IsAutoCloseConnection = true }
 
-     });
 
- });
 
- #endregion
 
- #region 注入Swagger注释(启用)
 
- if (AppSettingsHelper.Get("UseSwagger").ToBool())
 
- {
 
-     builder.Services.AddSwaggerGen(a =>
 
-     {
 
-         a.SwaggerDoc("v1", new OpenApiInfo
 
-         {
 
-             Version = "v1",
 
-             Title = "Api",
 
-             Description = "Api接口文档"
 
-         });
 
-         foreach (var item in groups)
 
-         {
 
-             a.SwaggerDoc(item.Item1, new OpenApiInfo { Version = item.Item1, Title = item.Item2, Description = $"{item.Item2}接口文档" });
 
-         }
 
-         a.DocumentFilter<SwaggerApi>();
 
-         a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Api.xml"), true);
 
-         a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Domain.xml"), true);
 
-         a.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 
-         {
 
-             Description = "Value: Bearer {token}",
 
-             Name = "Authorization",
 
-             In = ParameterLocation.Header,
 
-             Type = SecuritySchemeType.ApiKey,
 
-             Scheme = "Bearer"
 
-         });
 
-         a.AddSecurityRequirement(new OpenApiSecurityRequirement()
 
-         {{
 
-             new OpenApiSecurityScheme
 
-             {
 
-                 Reference = new OpenApiReference
 
-                 {
 
-                     Type = ReferenceType.SecurityScheme,
 
-                     Id = "Bearer"
 
-                 }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header }, new List<string>()
 
-             }
 
-         });
 
-     });
 
- }
 
- #endregion
 
- #region 添加swagger注释
 
- //if (AppSettingsHelper.Get("UseSwagger").ToBool())
 
- //{
 
- //    builder.Services.AddSwaggerGen(c =>
 
- //    {
 
- //        c.SwaggerDoc("v1", new OpenApiInfo
 
- //        {
 
- //            Version = "v1",
 
- //            Title = "文华商旅支付测试接口",
 
- //            Description = "请先从Auth鉴权=>login获取token进行认证,输入:token进行认证(Bearer)"
 
- //        });
 
- //        c.DocumentFilter<SwaggerApi>();
 
- //        var xmlPath = Path.Combine(basePath, "OASystem.API.xml");
 
- //        c.IncludeXmlComments(xmlPath, true);
 
- //        var xmlDomainPath = Path.Combine(basePath, "OASystem.Domain.xml");
 
- //        c.IncludeXmlComments(xmlDomainPath, true);
 
- //        c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 
- //        {
 
- //            Description = "Value: Bearer {token}",
 
- //            Name = "Authorization",
 
- //            In = ParameterLocation.Header,
 
- //            Type = SecuritySchemeType.ApiKey,
 
- //            Scheme = "Bearer"
 
- //        });
 
- //        c.AddSecurityRequirement(new OpenApiSecurityRequirement()
 
- //        {
 
- //          {
 
- //            new OpenApiSecurityScheme
 
- //            {
 
- //              Reference = new OpenApiReference
 
- //              {
 
- //                Type = ReferenceType.SecurityScheme,
 
- //                Id = "Bearer"
 
- //              },Scheme = "oauth2",Name = "Bearer",In = ParameterLocation.Header,
 
- //            },new List<string>()
 
- //          }
 
- //        });
 
- //    });
 
- //}
 
- #endregion
 
- #region 添加校验
 
- builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
 
- {
 
-     options.TokenValidationParameters = new TokenValidationParameters
 
-     {
 
-         ValidateIssuer = true,
 
-         ValidateAudience = true,
 
-         ValidateLifetime = true,
 
-         ValidateIssuerSigningKey = true,
 
-         ValidAudience = "OASystem.com",
 
-         ValidIssuer = "OASystem.com",
 
-         IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
 
-     };
 
- });
 
- #endregion
 
- #region 初始化日志
 
- Log.Logger = new LoggerConfiguration()
 
-        .MinimumLevel.Debug()
 
-        .WriteTo.File(Path.Combine("Logs", @"Log.txt"), rollingInterval: RollingInterval.Day)
 
-        .CreateLogger();
 
- #endregion
 
- #region 引入注册Autofac OASystem.Infrastructure中所有以Repository结尾的文件都会被注入到项目
 
- builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
 
- var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
 
- {
 
-     try
 
-     {
 
-         var assemblyServices = Assembly.Load("OASystem.Infrastructure");
 
-         builder.RegisterAssemblyTypes(assemblyServices).Where(a => a.Name.EndsWith("Repository")).AsSelf();
 
-     }
 
-     catch (Exception ex)
 
-     {
 
-         throw new Exception(ex.Message + "\n" + ex.InnerException);
 
-     }
 
- });
 
- #endregion
 
- #region AutoMapper
 
- AutoMapper.IConfigurationProvider config = new MapperConfiguration(cfg =>
 
- {
 
-     cfg.AddProfile<_baseMappingProfile>();
 
- });
 
- builder.Services.AddSingleton(config);
 
- builder.Services.AddScoped<IMapper, Mapper>();
 
- #endregion
 
- var app = builder.Build();
 
- // Configure the HTTP request pipeline.
 
- if (!app.Environment.IsDevelopment())
 
- {
 
-     app.UseExceptionHandler("/Home/Error");
 
- }
 
- app.UseStaticFiles();
 
- app.UseRouting();
 
- app.UseAuthentication();
 
- app.UseAuthorization();
 
- #region 启用swaggerUI
 
- if (AppSettingsHelper.Get("UseSwagger").ToBool())
 
- {
 
-     app.UseSwagger();
 
-     app.UseSwaggerUI(c =>
 
-     {
 
-         c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ver0.1");
 
-         foreach (var item in groups)
 
-         {
 
-             c.SwaggerEndpoint($"/swagger/{item.Item1}/swagger.json", item.Item2);
 
-         }
 
-         c.RoutePrefix = string.Empty;
 
-         c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
 
-         c.DefaultModelsExpandDepth(-1);
 
-     });
 
- }
 
- #endregion
 
- app.MapControllerRoute(
 
-     name: "default",
 
-     pattern: "{controller=Home}/{action=Index}/{id?}");
 
- app.Run();
 
 
  |