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.

269 lines
10 KiB
C#

1 year ago
using Admin.Core.Common;
using Admin.Core.Extensions;
using Admin.Core.IService.ISys;
using Admin.Core.Plc;
using Admin.Core.RunPlc;
using Admin.Core.Tasks;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Admin.Core.Api
{
/// <summary>
/// Startup
/// </summary>
public class Startup
{
/// <summary>
/// Startup
/// </summary>
/// <param name="configuration"></param>
/// <param name="env"></param>
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Env = env;
}
/// <summary>
/// IConfiguration
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD>
/// </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)
{
//Appsettings
services.AddSingleton(new Appsettings(Configuration));
//<2F><>־<EFBFBD>ļ<EFBFBD>Ŀ¼
services.AddSingleton(new LogLock(Env.ContentRootPath));
//jwt<77><74>Ids4Ȩ<34>޷<EFBFBD><DEB7><EFBFBD>
Permissions.IsUseIds4 = Appsettings.app(new string[] { "Startup", "IdentityServer4", "Enabled" }).ObjToBool();
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD>ķ<EFBFBD><C4B7>ص<EFBFBD>ClaimType<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ʹ<EFBFBD><CAB9>Mapӳ<70><D3B3>
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
//Memory<72><79><EFBFBD><EFBFBD>
services.AddMemoryCacheSetup();
//Swagger
services.AddSwaggerSetup();
//Sqlsugar<61><72><EFBFBD>ݿ<EFBFBD>ģ<EFBFBD>Ͳ<EFBFBD>ѯ
services.AddSqlsugarSetup();
//AutoMapper
services.AddAutoMapperSetup();
//<2F><><EFBFBD><EFBFBD>
services.AddCorsSetup();
//<2F><><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>
//services.AddMiniProfilerSetup();
//HttpContext<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
services.AddHttpContextSetup();
// <20><>Ȩ+<2B><>֤ (jwt or ids4)
services.AddAuthorizationSetup();
if (Permissions.IsUseIds4) { services.AddAuthentication_Ids4Setup(); }
else { services.AddAuthentication_JWTSetup(); }
//IPLimit<69><74><EFBFBD><EFBFBD>
services.AddIpPolicyRateLimitSetup(Configuration);
//ɸѡ<C9B8><D1A1>
//services.AddScoped<UseServiceDIAttribute>();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
// Default is true, make it false
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
services.AddJobSetup();
//<2F><><EFBFBD><EFBFBD>PLC
services.AddPlcSetup();
//Redis<69><73><EFBFBD><EFBFBD>
//services.AddRedisCacheSetup();
//Redis<69><73><EFBFBD><EFBFBD>
//services.AddRedisInitMqSetup();
//EventBus
//services.AddEventBusSetup();
//SignalR
//services.AddSignalR().AddNewtonsoftJsonProtocol();
//kestrel
//services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
// .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
//Controller
services.AddControllers(o =>
{
// ȫ<><C8AB><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD>
o.Filters.Add(typeof(GlobalExceptionsFilter));
// ȫ<><C8AB>·<EFBFBD><C2B7>Ȩ<EFBFBD>޹<EFBFBD>Լ
//o.Conventions.Insert(0, new GlobalRouteAuthorizeConvention());
// ȫ<><C8AB>·<EFBFBD><C2B7>ǰ׺<C7B0><D7BA>ͳһ<CDB3>޸<EFBFBD>·<EFBFBD><C2B7>
o.Conventions.Insert(0, new GlobalRoutePrefixFilter(new RouteAttribute(RoutePrefix.Name)));
})
//ȫ<><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Json<6F><6E><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
.AddNewtonsoftJson(options =>
{
//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//<2F><>ʹ<EFBFBD><CAB9><EFBFBD>շ<EFBFBD><D5B7><EFBFBD>ʽ<EFBFBD><CABD>key
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//<2F><><EFBFBD><EFBFBD>Model<65><6C>Ϊnull<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʽ
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UTCʱ<43><CAB1>
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
});
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
//֧<>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>ȫ <20><><EFBFBD><EFBFBD><><D6A7> System.Text.Encoding.GetEncoding("GB2312") System.Text.Encoding.GetEncoding("GB18030")
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
//private X509Certificate2 GetCertificate()
//{
// var assembly = typeof(Startup).GetTypeInfo().Assembly;
// using (var stream = assembly.GetManifestResourceStream(
// assembly.GetManifestResourceNames().First(r => r.EndsWith("cnblogs.pfx"))))
// {
// if (stream == null)
// throw new ArgumentNullException(nameof(stream));
// var bytes = new byte[stream.Length];
// stream.Read(bytes, 0, bytes.Length);
// return new X509Certificate2(bytes);
// }
//}
/// <summary>
/// ע<><D7A2><EFBFBD><EFBFBD>Program.CreateHostBuilder<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Autofac<61><63><EFBFBD>񹤳<EFBFBD>
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModuleRegister());
builder.RegisterModule<AutofacPropertityModuleReg>();
}
/// <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, ISysTasksQzService tasksQzService,
ISchedulerCenter schedulerCenter, IRunPlcService plc)
{
// Ip<49><70><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>Źܵ<C5B9><DCB5><EFBFBD><EFBFBD><EFBFBD>
app.UseIpLimitMildd();
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
app.UseReuestResponseLog();
// <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>)
app.UseRecordAccessLogsMildd();
// signalr
//app.UseSignalRSendMildd();
// <20><>¼ip<69><70><EFBFBD><EFBFBD>
app.UseIPLogMildd();
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("/Error");
// <20>ڷǿ<DAB7><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ʹ<EFBFBD><CAB9>HTTP<54>ϸ<EFBFBD><CFB8><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>(or HSTS) <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>web<65><62>ȫ<EFBFBD>Ƿdz<C7B7><C7B3><EFBFBD>Ҫ<EFBFBD>ġ<EFBFBD>
// ǿ<><C7BF>ʵʩ HTTPS <20><> ASP.NET Core<72><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD> app.UseHttpsRedirection
//app.UseHsts();
}
// <20><>װSwaggerչʾ
app.UseSwaggerMildd(() => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Admin.Core.Api.index.html"));
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ע<><D7A2><EFBFBD>±<EFBFBD><C2B1><EFBFBD>Щ<EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>򣬺<EFBFBD><F2A3ACBA><EFBFBD>Ҫ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// CORS<52><53><EFBFBD><EFBFBD>
app.UseCors(Appsettings.app(new string[] { "Startup", "Cors", "PolicyName" }));
// <20><>תhttps
//app.UseHttpsRedirection();
// ʹ<>þ<EFBFBD>̬<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ע<EFBFBD><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>ӦĿ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B1A8>
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "file")),
RequestPath = "/file",
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=600");
}
});
// ʹ<>þ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>
app.UseStaticFiles();
app.UseSession();
//app.UseCookiePolicy();
// <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>
app.UseStatusCodePages();
// Routing
app.UseRouting();
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Գ<EFBFBD><D4B3>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƽ<EFBFBD>
// app.UseJwtTokenAuth();
// <20>ȿ<EFBFBD><C8BF><EFBFBD><EFBFBD><EFBFBD>֤
app.UseAuthentication();
// Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.UseAuthorization();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>
//app.UseMiniProfilerMildd();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
//ʹ<>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>
//endpoints.MapHub<ChatHub>("/chatHub");
//GlobalHost.Configuration.MaxIncomingWebSocketMessageSize = null;
});
// <20><><EFBFBD><EFBFBD>QuartzNetJob<6F><62><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>
app.UseQuartzJobMildd(tasksQzService, schedulerCenter);
//PLC
app.UsePlcMildd(plc);
// <20><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
//app.UseConsulMildd(Configuration, lifetime);
// <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
//app.ConfigureEventBus();
}
}
}