You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
6.2 KiB
224 lines
6.2 KiB
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using Essensoft.Paylink.WeChatPay;
|
|
using Hangfire;
|
|
using Hangfire.Dashboard.BasicAuthorization;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using Zncy.CloudCar.WeChat.Service.Mediator;
|
|
using Zncy.CloudCar.WeChat.Service.Options;
|
|
using Zncy.CloudCar.WeChat.Service.Services.HttpClients;
|
|
using Znyc.CloudCar.Configuration;
|
|
using Znyc.CloudCar.Core.AutoFac;
|
|
using Znyc.CloudCar.Core.Config;
|
|
using Znyc.CloudCar.Filter;
|
|
using Znyc.CloudCar.Middlewares;
|
|
using Znyc.CloudCar.Swagger;
|
|
using Znyc.CloudCar.Task;
|
|
|
|
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//添加静态文件支持
|
|
builder.Services.AddSingleton(new AppSettingsHelper(builder.Environment.ContentRootPath, builder.Environment.EnvironmentName));
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
//Swagger接口文档
|
|
builder.Services.AddSwaggerSetup();
|
|
|
|
//FreeSql
|
|
builder.Services.AddFreeSqlSetup();
|
|
|
|
//跨域
|
|
builder.Services.AddCoresSetup();
|
|
|
|
//MediatR
|
|
builder.Services.AddMediatR(typeof(TextMessageEventCommand).Assembly);
|
|
|
|
//Redis
|
|
builder.Services.AddRedisCacheSetup();
|
|
|
|
//Redis消息队列
|
|
builder.Services.AddRedisMessageQueueSetup();
|
|
|
|
// 引入Payment 依赖注入微信支付
|
|
builder.Services.AddWeChatPay();
|
|
|
|
// 在 appsettings.json 中 配置选项
|
|
builder.Services.Configure<WeChatPayOptions>(builder.Configuration.GetSection("WeChatPay"));
|
|
|
|
//注册自定义微信接口配置文件
|
|
builder.Services.Configure<WeChatOptions>(builder.Configuration.GetSection(nameof(WeChatOptions)));
|
|
builder.Services.Configure<TenpayOptions>(builder.Configuration.GetSection(nameof(TenpayOptions)));
|
|
|
|
// 注入工厂 HTTP 客户端
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddSingleton<IWeChatApiHttpClientFactory, WeChatApiHttpClientFactory>();
|
|
builder.Services.AddSingleton<IWechatTenpayCertificateManagerFactory, WechatTenpayCertificateManagerFactory>();
|
|
builder.Services.AddSingleton<IWechatTenpayHttpClientFactory, WechatTenpayHttpClientFactory>();
|
|
|
|
//注册Hangfire定时任务
|
|
builder.Services.AddHangFireSetup();
|
|
|
|
//Auth
|
|
builder.Services.AddAuthorizationSetup();
|
|
|
|
//上下文注入
|
|
builder.Services.AddHttpContextSetup();
|
|
|
|
//注册mvc
|
|
builder.Services.AddMvc(
|
|
options =>
|
|
{
|
|
//实体验证
|
|
//options.Filters.Add<RequiredError>();
|
|
//异常处理
|
|
options.Filters.Add<ExceptionFilter>();
|
|
})
|
|
.AddNewtonsoftJson(p =>
|
|
{
|
|
//数据格式首字母小写 不使用驼峰
|
|
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
//不使用驼峰样式的key
|
|
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
//忽略循环引用
|
|
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
//设置时间格式
|
|
p.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
});
|
|
|
|
//AutoFac
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
|
|
{
|
|
builder.RegisterModule(new AutofacModuleRegister());
|
|
var controllerBaseType = typeof(ControllerBase);
|
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) & t != controllerBaseType).PropertiesAutowired();
|
|
});
|
|
//服务配置中加入AutoFac控制器替换规则。
|
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
|
|
|
//NLog: Setup NLog for Dependency injection
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
|
builder.Host.UseNLog();
|
|
|
|
|
|
|
|
//关闭参数自动校验,我们需要返回自定义的格式
|
|
builder.Services.Configure<ApiBehaviorOptions>((o) =>
|
|
{
|
|
o.SuppressModelStateInvalidFilter = true;
|
|
});
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// 记录请求与返回数据 (注意开启权限,不然本地无法写入)
|
|
app.UseRequestResponseLog();
|
|
// 用户访问记录(必须放到外层,不然如果遇到异常,会报错,因为不能返回流)(注意开启权限,不然本地无法写入)
|
|
app.UseRecordAccessLogsMildd();
|
|
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
|
app.UseIpLogMildd();
|
|
// signalr
|
|
app.UseSignalRSendMildd();
|
|
|
|
|
|
//if (!app.Environment.IsProduction())
|
|
//{
|
|
//注册swagger
|
|
app.UseSwagger().UseSwaggerUI(c =>
|
|
{
|
|
//根据版本名称倒序 遍历展示
|
|
typeof(CustomApiVersion.ApiVersion).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
|
|
version =>
|
|
{
|
|
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{version}");
|
|
});
|
|
//设置默认跳转到swagger-ui
|
|
c.RoutePrefix = "swagger";
|
|
//c.RoutePrefix = string.Empty;
|
|
});
|
|
//}
|
|
|
|
|
|
//授权
|
|
var filter = new BasicAuthAuthorizationFilter(
|
|
new BasicAuthAuthorizationFilterOptions
|
|
{
|
|
SslRedirect = false,
|
|
RequireSsl = false,
|
|
LoginCaseSensitive = false,
|
|
Users = new[]
|
|
{
|
|
new BasicAuthAuthorizationUser
|
|
{
|
|
Login=AppSettingsConstVars.HangFireLogin,
|
|
PasswordClear=AppSettingsConstVars.HangFirePassWord
|
|
}
|
|
}
|
|
});
|
|
var options = new DashboardOptions
|
|
{
|
|
AppPath = "/hangfire",//返回时跳转的地址
|
|
DisplayStorageConnectionString = false,//是否显示数据库连接信息
|
|
Authorization = new[]
|
|
{
|
|
filter
|
|
},
|
|
IsReadOnlyFunc = Context =>
|
|
{
|
|
return false;//是否只读面板
|
|
}
|
|
};
|
|
app.UseHangfireDashboard("/cloud_pro_hangfire");
|
|
HangfireDispose.HangfireService();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
app.UseHsts();
|
|
}
|
|
|
|
//跨域
|
|
app.UseCors(AppSettingsConstVars.CorsPolicyName);
|
|
|
|
app.UseRouting();
|
|
|
|
//使用静态文件
|
|
app.UseStaticFiles();
|
|
|
|
// 先开启认证
|
|
app.UseAuthentication();
|
|
// 然后是授权中间件
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllerRoute(
|
|
"areas",
|
|
"{area:exists}/{controller=Default}/{action=Index}/{id?}"
|
|
);
|
|
endpoints.MapControllerRoute(
|
|
"default",
|
|
"{controller=Default}/{action=Index}/{id?}"
|
|
);
|
|
//endpoints.MapControllers();
|
|
});
|
|
|
|
|
|
|
|
app.Run("http://*:8001");
|
|
|