예제 #1
0
        void IStartup.Configure(IApplicationBuilder app)
        {
            IHostingEnvironment  env           = app.ApplicationServices.GetService <IHostingEnvironment>();
            ILoggerFactory       loggerFactory = app.ApplicationServices.GetService <ILoggerFactory>();
            IApplicationLifetime lifetime      = app.ApplicationServices.GetService <IApplicationLifetime>();

            loggerFactory.AddProvider(new MailDemonLogProvider());
            bool enableWeb = bool.Parse(Configuration.GetSection("mailDemonWeb")["enable"]);
            IServerAddressesFeature serverAddressesFeature = app.ServerFeatures.Get <IServerAddressesFeature>();
            string address = serverAddressesFeature?.Addresses.LastOrDefault();

            if (address == null)
            {
                ServerUrl = "http://" + GetLocalIPAddress() + ":52664";
            }
            else
            {
                ServerUrl = address;
            }

            if (enableWeb)
            {
                InitializeDB(app);
                app.UseStatusCodePagesWithReExecute("/Error/{0}");
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                }
                app.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
                var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("en-US") };
                app.UseRequestLocalization(new RequestLocalizationOptions
                {
                    DefaultRequestCulture = new RequestCulture("en"),
                    SupportedCultures     = supportedCultures,
                    SupportedUICultures   = supportedCultures
                });
                app.UseStaticFiles(new StaticFileOptions
                {
                    OnPrepareResponse = (ctx) =>
                    {
                        ctx.Context.Response.GetTypedHeaders().CacheControl =
                            new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                        {
                            Public = true,
                            MaxAge = TimeSpan.FromDays(7.0)
                        };
                        ctx.Context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
                            new string[] { "Accept-Encoding" };
                    }
                });
                app.UseAuthentication();
                app.UseResponseCompression();
                app.UseResponseCaching();
                app.UseMiddleware <RateLimitMiddleware>();
                app.UseMvc(routes =>
                {
                    routes.MapRoute("home", "/{action=Index}/{id?}", new { controller = "Home" });
                });
                // confusingly and strangely, WTF Microsoft, the UseCookiePolicy must come AFTER app.UseMvc for TempData to work.
                // this is not documented anywhere that I could find
                app.UseCookiePolicy(new CookiePolicyOptions
                {
                });
            }
            ServerUrl = ServerUrl.Trim('/', '?');
            if (string.IsNullOrWhiteSpace(Authority))
            {
                Authority = ServerUrl;
            }
            else
            {
                Authority = Authority.Trim('/', '?');
            }
            lifetime.ApplicationStopping.Register(OnShutdown);
            MailDemonLog.Warn("Mail demon web service started");
            runningEvent.Set();
        }