You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CoreShop/CoreCms.Net.Web.Admin/Startup.cs

247 lines
8.7 KiB
C#

using System;
using System.IO;
using System.Linq;
using Autofac;
using CoreCms.Net.Auth;
using CoreCms.Net.Configuration;
using CoreCms.Net.Core.AutoFac;
using CoreCms.Net.Core.Config;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.Mapping;
using CoreCms.Net.Middlewares;
using CoreCms.Net.Swagger;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.WeChatPay;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Yitter.IdGenerator;
namespace CoreCms.Net.Web.Admin
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public class Startup
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><ECBAAF>
/// </summary>
/// <param name="configuration"></param>
/// <param name="env"></param>
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Env = env;
}
/// <summary>
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// </summary>
public IWebHostEnvironment Env { get; }
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
//<2F><><EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ȡ֧<C8A1><D6A7>
services.AddSingleton(new AppSettingsHelper(Env.ContentRootPath));
services.AddSingleton(new LogLockHelper(Env.ContentRootPath));
//Memory<72><79><EFBFBD><EFBFBD>
services.AddMemoryCacheSetup();
//Redis<69><73><EFBFBD><EFBFBD>
services.AddRedisCacheSetup();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>SqlSugarע<72><D7A2>֧<EFBFBD><D6A7>
services.AddSqlSugarSetup();
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>CORS<52><53>
services.AddCorsSetup();
//<2F><><EFBFBD><EFBFBD>session֧<6E><D6A7>(session<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>cache<68><65><EFBFBD>д洢)
services.AddSession();
// AutoMapper֧<72><D6A7>
services.AddAutoMapper(typeof(AutoMapperConfiguration));
//ʹ<><CAB9> SignalR
services.AddSignalR();
// <20><><EFBFBD><EFBFBD>Payment <20><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><><D6A7><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><><CEA2>֧<EFBFBD><D6A7>)
services.AddAlipay();
services.AddWeChatPay();
// <20><> appsettings.json <20><> <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
//ע<><D7A2><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>΢<EFBFBD>Žӿ<C5BD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
services.Configure<WeChat.Service.Options.WeChatOptions>(Configuration.GetSection(nameof(WeChat.Service.Options.WeChatOptions)));
// ע<><EFBFBD><EBB9A4> HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD>
services.AddHttpClient();
services.AddSingleton<WeChat.Service.HttpClients.IWeChatApiHttpClientFactory, WeChat.Service.HttpClients.WeChatApiHttpClientFactory>();
//Swagger<65>ӿ<EFBFBD><D3BF>ĵ<EFBFBD>ע<EFBFBD><D7A2>
services.AddAdminSwaggerSetup();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD>ӡ<EFBFBD><D3A1>
services.AddYiLianYunSetup();
//jwt<77><74>Ȩ֧<C8A8><D6A7>ע<EFBFBD><D7A2>
services.AddAuthorizationSetupForAdmin();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
services.AddHttpContextSetup();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>AutoFac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
//ע<><D7A2>mvc<76><63>ע<EFBFBD><D7A2>razor<6F><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
services.AddMvc(options =>
{
//ʵ<><CAB5><EFBFBD><EFBFBD>֤
options.Filters.Add<RequiredErrorForAdmin>();
//<2F><EFBFBD><ECB3A3><EFBFBD><EFBFBD>
options.Filters.Add<GlobalExceptionsFilterForAdmin>();
//Swagger<65>޳<EFBFBD><DEB3><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>apiչʾ<D5B9><CABE><EFBFBD>б<EFBFBD>
options.Conventions.Add(new ApiExplorerIgnores());
options.EnableEndpointRouting = false;
})
.AddNewtonsoftJson(p =>
{
//<2F><><EFBFBD>ݸ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ĸСд <20><>ʹ<EFBFBD><CAB9><EFBFBD>շ<EFBFBD>
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//<2F><>ʹ<EFBFBD><CAB9><EFBFBD>շ<EFBFBD><D5B7><EFBFBD>ʽ<EFBFBD><CABD>key
//p.SerializerSettings.ContractResolver = new DefaultContractResolver();
//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʽ
p.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
});
// ѩ<><D1A9>Ư<EFBFBD><C6AF><EFBFBD>
// <20><><EFBFBD><EFBFBD> IdGeneratorOptions <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerId<49><64>
var options = new IdGeneratorOptions(1);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
YitIdHelper.SetIdGenerator(options);
}
/// <summary>
/// Autofac<61><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModuleRegister());
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
var controllerBaseType = typeof(ControllerBase);
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
.PropertiesAutowired();
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><EFBFBD><E2BFAA>Ȩ<EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>д<EFBFBD><D0B4>)
app.UseRequestResponseLog();
// <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ʼ<EFBFBD>¼(<28><><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD><E3A3AC>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><E1B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD>)(ע<><EFBFBD><E2BFAA>Ȩ<EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>д<EFBFBD><D0B4>)
app.UseRecordAccessLogsMildd();
// <20><>¼ip<69><70><EFBFBD><EFBFBD><><EFBFBD><E2BFAA>Ȩ<EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>д<EFBFBD><D0B4>)
app.UseIpLogMildd();
// signalr
app.UseSignalRSendMildd();
app.UseSwagger().UseSwaggerUI(c =>
{
//<2F><><EFBFBD>ݰ汾<DDB0><E6B1BE><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD> <20><><EFBFBD><EFBFBD>չʾ
typeof(CustomApiVersion.ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
version =>
{
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Doc {version}");
});
c.RoutePrefix = "doc";
});
//ʹ<><CAB9> Session
app.UseSession();
if (env.IsDevelopment())
{
// <20>ڿ<EFBFBD><DABF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>쳣ҳ<ECB3A3><EFBFBD><E6A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// CORS<52><53><EFBFBD><EFBFBD>
app.UseCors(AppSettingsConstVars.CorsPolicyName);
// <20><>תhttps
//app.UseHttpsRedirection();
// ʹ<>þ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>
app.UseStaticFiles();
// ʹ<><CAB9>cookie
app.UseCookiePolicy();
// <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>
app.UseStatusCodePages();
// Routing
app.UseRouting();
// <20>ȿ<EFBFBD><C8BF><EFBFBD><EFBFBD><EFBFBD>֤
app.UseAuthentication();
// Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"areas",
"{area:exists}/{controller=Default}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
//<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ʼҳ<CABC><D2B3><EFBFBD><EFBFBD>default.html<6D><6C>
//<2F>˴<EFBFBD><CBB4><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wwwroot<6F>ļ<EFBFBD><C4BC>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
var defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(defaultFilesOptions);
app.UseStaticFiles();
}
}
}