Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            // Global CORS policy
            app.UseCors(x => x
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader());

            app.UseAuthentication();

            app.UseHttpsRedirection();
            app.UseMvc();

            // Seed the DB with an user if none exist
            UserSeeder.Initialize(app.ApplicationServices.CreateScope().ServiceProvider);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                       .UseKestrel()
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseIISIntegration()
                       .ConfigureAppConfiguration((context, configBuilder) =>
            {
                HostingEnvironment = context.HostingEnvironment;

                configBuilder.SetBasePath(HostingEnvironment.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{HostingEnvironment.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

                Configuration = configBuilder.Build();
                GcpProjectId  = GetProjectId(Configuration);
            })
                       .ConfigureServices(services =>
            {
                // Add framework services.Microsoft.VisualStudio.ExtensionManager.ExtensionManagerService
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

                if (HasGcpProjectId)
                {
                    // Enables Stackdriver Trace.
                    services.AddGoogleTrace(options => options.ProjectId = GcpProjectId);
                    // Sends Exceptions to Stackdriver Error Reporting.
                    services.AddGoogleExceptionLogging(
                        options =>
                    {
                        options.ProjectId   = GcpProjectId;
                        options.ServiceName = GetServiceName(Configuration);
                        options.Version     = GetVersion(Configuration);
                    });

                    services.AddSingleton <ILoggerProvider>(sp => GoogleLoggerProvider.Create(GcpProjectId));
                    services.AddServicesDependencies(GcpProjectId);
                }
            })
                       .ConfigureLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
                if (HostingEnvironment.IsDevelopment())
                {
                    // Only use Console and Debug logging during development.
                    loggingBuilder.AddConsole(options =>
                                              options.IncludeScopes = Configuration.GetValue <bool>("Logging:IncludeScopes"));
                    loggingBuilder.AddDebug();
                }
            })
                       .Configure((app) =>
            {
                app.UseForwardedHeaders(new ForwardedHeadersOptions
                {
                    ForwardedHeaders = ForwardedHeaders.XForwardedProto
                });

                var options = new RewriteOptions();

                options.Rules.Add(new NonWwwRule());
                options.AddRedirectToHttps();

                app.UseRewriter(options);

                var logger = app.ApplicationServices.GetService <ILoggerFactory>().CreateLogger("Startup");
                if (HasGcpProjectId)
                {
                    // Sends logs to Stackdriver Error Reporting.
                    app.UseGoogleExceptionLogging();
                    // Sends logs to Stackdriver Trace.
                    app.UseGoogleTrace();

                    logger.LogInformation(
                        "Stackdriver Logging enabled: https://console.cloud.google.com/logs/");
                    logger.LogInformation(
                        "Stackdriver Error Reporting enabled: https://console.cloud.google.com/errors/");
                    logger.LogInformation(
                        "Stackdriver Trace enabled: https://console.cloud.google.com/traces/");
                }
                else
                {
                    logger.LogWarning(
                        "Stackdriver Logging not enabled. Missing Google:ProjectId in configuration.");
                    logger.LogWarning(
                        "Stackdriver Error Reporting not enabled. Missing Google:ProjectId in configuration.");
                    logger.LogWarning(
                        "Stackdriver Trace not enabled. Missing Google:ProjectId in configuration.");
                }

                if (HostingEnvironment.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseStaticFiles(new StaticFileOptions
                    {
                        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
                        RequestPath  = new PathString("/lib")
                    });
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    app.UseHsts();
                }

                app.UseHttpsRedirection();
                app.UseStatusCodePages();
                app.UseAuthentication();
                app.UseStaticFiles();

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

                UserSeeder.Initialize(app.ApplicationServices);
            }).Build();

            host.Run();
        }