Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var host       = CreateHostBuilder(args).Build();
            var isSeedData = EnvironmentConfiguration.GetEnvironmentVariableFromAppSettings("Database:SeedData") == "true";

            // Only seed data if appsettings flag is set and we are not in production
            if (isSeedData && !EnvironmentConfiguration.IsProduction())
            {
                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;
                    var logger   = services.GetRequiredService <ILogger <Program> >();

                    try
                    {
                        using (var context = services.GetRequiredService <HedwigContext>())
                        {
                            var initializer = new DbInitializer(context);
                            logger.LogInformation("Attempting to seed database");
                            initializer.Initialize();
                            logger.LogInformation("Successfully seeded database");
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, "An error occurred while seeding the database.");
                    }
                }
            }

            host.Run();
        }
Ejemplo n.º 2
0
        public static IWebHostBuilder CreateHostBuilder(string[] args)
        {
            var environment = EnvironmentConfiguration.GetEnvironmentVariableFromAppSettings("EnvironmentName");

            return(WebHost.CreateDefaultBuilder(args)
                   .ConfigureLogging((context, logging) =>
            {
                logging.ClearProviders();
                logging.AddConfiguration(context.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();

                if (!EnvironmentConfiguration.IsDevelopment())
                {
                    logging.AddAWSProvider(context.Configuration.GetAWSLoggingConfigSection());
                    logging.Services.Configure <Sentry.Extensions.Logging.SentryLoggingOptions>(context.Configuration.GetSection("Sentry"));
                    logging.AddSentry();
                }
            })
                   .UseSentry()
                   .UseEnvironment(environment)
                   .UseStartup <Startup>());
        }
Ejemplo n.º 3
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var isAutomaticallyApplyMigrations = EnvironmentConfiguration.GetEnvironmentVariableFromAppSettings("Database:AutomaticallyApplyMigrations") == "true";

            if (isAutomaticallyApplyMigrations)
            {
                app.UpdateDatabase();
            }

            // We want to be able to see errors in every non-prod env
            // Not just dev
            if (!env.IsProduction())
            {
                app.UseDeveloperExceptionPage();
                IdentityModelEventSource.ShowPII = true;
            }

            // We only need CORS support in development
            if (env.IsDevelopment())
            {
                app.UseCors();
            }

            app.UseHttpsRedirection();

            if (env.IsDevelopment())
            {
                // Creates an endpoint for viewing the generated
                // JSON schema of the API.
                //
                // Connected to note on services.ConfigureSwagger()
                app.UseSwagger();
            }

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            // If we are not in development, try serving static files according to
            // the path specified in AddSpaStaticFiles. This is necessary for
            // returning static compiled resources like the JS and CSS.
            // Note that AddSpaStaticFiles is called in
            // ServiceExtensions.ConfigureSpa()
            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }

            // UseSpa internally calls UseStaticFiles which only supports GET or OPTIONS
            // requests. Guard this middleware so only requests with those methods are
            // able to reach this. This prevents a 500 error when a POST/HEAD/DELETE/PUT
            // reaches this middleware.
            app.UseWhen(
                context => HttpMethods.IsGet(context.Request.Method) || HttpMethods.IsOptions(context.Request.Method),
                // UseSpa rewrites all non-endpoint requests (aka paths that do not map to
                // a controller). Thus, UseSpaStaticFiles must be called before UseSpa
                app => app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                // If we are development, that means we aren't serving SPA files
                // as static, compiled resources. So we need to forward requests a
                // development server.
                if (env.IsDevelopment())
                {
                    var isDocker = Environment.GetEnvironmentVariable("DOCKER_DEVELOPMENT");
                    // If we are using docker (compose), the client container will serve
                    // responses for us. We need to forward to its hostname.
                    if (isDocker == "true")
                    {
                        string CLIENT_HOST = Environment.GetEnvironmentVariable("CLIENT_HOST") ?? "http://localhost:3000";
                        spa.UseProxyToSpaDevelopmentServer(CLIENT_HOST);
                    }
                    // If we aren't using docker, we need .NET to create a server for us
                    // to serve responses.
                    else
                    {
                        // Note: While the parameter is called npmScript it will also run yarn
                        // scripts. Specifically, it will run the corresponding package.json
                        // script and provided the referenced executable exists on the path
                        // the script will run.
                        spa.UseReactDevelopmentServer(npmScript: "start");
                    }
                }

                // If we aren't in development, UseSpa will internally call UseStaticFiles
                // on the directory specified by AddSpaStaticFiles
            })
                );

            // Catch any remaining requests and return 404 not found
            app.Run(
                context =>
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return(Task.CompletedTask);
            });
        }