Пример #1
0
        public ApiBuilder BuildApp(IApplicationBuilder app, IHostingEnvironment env,
                                   IApplicationLifetime applicationLifetime, IDataSeeder seeder, IServiceProvider serviceProvider)
        {
            if (_useSwagger)
            {
                new ApiBuilderSwaggerHelper(_swaggerSpecs).Bind(app, env);
            }

            foreach (var apiSpecifications in _specifications)
            {
                apiSpecifications.ConfigureApp(app, serviceProvider);
            }

            var shouldMigrate  = _configuration.GetValue <bool>("migrate");
            var shouldLoadSeed = _configuration.GetValue <bool>("seed");

            if (shouldMigrate)
            {
                Console.WriteLine("Migrating...");
                seeder.MigrateDatabase().Wait();
                Console.WriteLine("Migrating done.");
                if (!shouldLoadSeed)
                {
                    applicationLifetime.StopApplication();
                }
            }
            else
            {
                seeder.EnsureMigrated().Wait();
            }

            if (shouldLoadSeed)
            {
                Console.WriteLine("Seeding data...");
                seeder.LoadSeed().Wait();
                Console.WriteLine("Seeding done.");
                applicationLifetime.StopApplication();
            }


            var generateSeed = _configuration.GetValue <string>("generate-seed");

            if (generateSeed != null)
            {
                Console.WriteLine("Generating seed...");
                seeder.SeedToFile(generateSeed).Wait();
                Console.WriteLine("Generating seed done.");
                applicationLifetime.StopApplication();
            }

            var adminEmail = _configuration.GetValue <string>("give-admin");

            if (adminEmail != null)
            {
                Console.WriteLine("Giving admin to " + adminEmail + "...");
                var userManager = serviceProvider.GetService <UserManager <User> >();
                var user        = userManager.FindByEmailAsync(adminEmail).Result;
                userManager.AddToRoleAsync(user, "Admin").Wait();
                userManager.AddToRoleAsync(user, "Staff").Wait();
                userManager.AddToRoleAsync(user, "Moderator").Wait();
                userManager.AddToRoleAsync(user, "User").Wait();
                Console.WriteLine("Done.");

                applicationLifetime.StopApplication();
            }

            // assert that variable is set correctly
            if (EnvVarManager.GetOrThrow("EXTERNAL_URL").EndsWith('/') ||
                !EnvVarManager.GetOrThrow("EXTERNAL_URL").Contains("http"))
            {
                throw new Exception("EXTERNAL_URL must include protocol and must not end with /");
            }

            return(this);
        }