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.

225 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");