// using MoYu.DependencyInjection; using SafeCampus.Application.Manager.DeepelephManager; using SafeCampus.Core.Extension; namespace SafeCampus.Web.Core; /// /// Web启动项配置 /// [AppStartup(99)] public class Startup : AppStartup { public void ConfigureServices(IServiceCollection services) { //启动LoggingMonitor操作日志写入数据库组件 services.AddComponent(); //认证组件 services.AddComponent(); //启动Web设置ConfigureServices组件 services.AddComponent(); //gip压缩 services.AddComponent(); //定时任务 //services.AddSchedule(); //添加控制器相关 services.AddControllers().AddNewtonsoftJson(options => //配置json { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();// 首字母小写(驼峰样式) options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";// 时间格式化 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;// 忽略循环引用 }).AddInjectWithUnifyResult()//配置统一返回模型 ; //Nginx代理的话获取真实IP services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; //新增如下两行 options.KnownNetworks.Clear(); options.KnownProxies.Clear(); }); services.AddRemoteRequest(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //var isDevelopment = App.HostEnvironment.IsDevelopment(); //var isProduction = App.HostEnvironment.IsProduction(); if (env.IsDevelopment()) { //启动执行消息订阅 Scoped.Create((_, scope) => { var deepeleph = scope.ServiceProvider.GetRequiredService(); deepeleph.SubscribeAlarm(); deepeleph.SubscriberRoomCall(); }); } //启动Web设置Configure组件 app.UseComponent(env); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { //启用gzip压缩 app.UseResponseCompression(); } // 启用HTTPS app.UseHttpsRedirection(); // 添加状态码拦截中间件 app.UseUnifyResultStatusCodes(); app.UseStaticFiles();//静态文件访问配置 //静态文件访问配置,暂时不开启 // app.UseStaticFiles(new StaticFileOptions // { // ServeUnknownFileTypes = true, // FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),//wwwroot相当于真实目录 // OnPrepareResponse = c => // { // c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); // c.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800"); // }, // RequestPath = new PathString("/files")//src相当于别名,为了安全 // }); app.UseRouting();//路由配置 app.UseCorsAccessor();//跨域配置 app.UseWebStatus();//网站开启状态 app.UseAuthentication();//认证 app.UseAuthorization();//授权 app.UseInject(string.Empty); app.UseForwardedHeaders();//Nginx代理的话获取真实IP app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); var path = Path.Combine(Directory.GetCurrentDirectory(), "Files"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var alarmImg = Path.Combine(Directory.GetCurrentDirectory(), "Files","alarmImg"); if (!Directory.Exists(alarmImg)) { Directory.CreateDirectory(alarmImg); } app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider(path), RequestPath = new PathString("/Files"), EnableDirectoryBrowsing = true }); } }