Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews()
            .AddNewtonsoftJson(options =>
                               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                               );
            string connectionString = "";

            if (_currentEnvironment.IsDevelopment())
            {
                connectionString = Configuration.GetConnectionString("DefaultConnection");
            }
            else
            {
                connectionString = ConnectionUri.Convert(Environment.GetEnvironmentVariable("DATABASE_URL"));
            }
            services.AddAuthentication("Bearer")
            .AddJwtBearer("Bearer", config =>
            {
                config.Authority = Configuration.GetValue <string>("JwtBearer:Authority");
                config.Audience  = Configuration.GetValue <string>("JwtBearer:Audience");
            });
            services.AddDbContext <PizzaDbContext>(config =>
            {
                //config.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(typeof(Startup).Assembly.FullName));
                config.UseNpgsql(connectionString, sql => sql.MigrationsAssembly(typeof(Startup).Assembly.FullName));
            });
            services.AddControllers();
        }
Exemplo n.º 2
0
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()

                                               .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                                               .AddJsonFile("appsettings.json")
                                               .Build();

            //optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
            {
                optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
            }
            else
            {
                optionsBuilder.UseNpgsql(ConnectionUri.Convert(Environment.GetEnvironmentVariable("DATABASE_URL")));
            }
        }
Exemplo n.º 3
0
        public static int Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
                         .MinimumLevel.Override("System", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         // uncomment to write to Azure diagnostics stream
                         //.WriteTo.File(
                         //    @"D:\home\LogFiles\Application\identityserver.txt",
                         //    fileSizeLimitBytes: 1_000_000,
                         //    rollOnFileSizeLimit: true,
                         //    shared: true,
                         //    flushToDiskInterval: TimeSpan.FromSeconds(1))
                         .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
                         .CreateLogger();

            try
            {
                //docker compose setting

                /*var host = CreateHostBuilder(args).Build();
                 * Log.Information("Seeding database...");
                 * var config = host.Services.GetRequiredService<IConfiguration>();
                 * var connectionString = config.GetConnectionString("DefaultConnection");
                 * //string connectionString = ConnectionUri.Convert(Environment.GetEnvironmentVariable("DATABASE_URL")) ;
                 * SeedData.EnsureSeedData(connectionString);
                 * Log.Information("Done seeding database.");*/

                var seed = args.Contains("/seed");
                if (seed)
                {
                    args = args.Except(new[] { "/seed" }).ToArray();
                }

                var host = CreateHostBuilder(args).Build();

                if (seed)
                {
                    Log.Information("Seeding database...");
                    var    config           = host.Services.GetRequiredService <IConfiguration>();
                    var    env              = host.Services.GetRequiredService <IWebHostEnvironment>();
                    string connectionString = "";
                    if (env.IsDevelopment())
                    {
                        connectionString = config.GetConnectionString("DefaultConnection");
                    }
                    else
                    {
                        connectionString = ConnectionUri.Convert(Environment.GetEnvironmentVariable("DATABASE_URL"));
                    }
                    SeedData.EnsureSeedData(connectionString);
                    Log.Information("Done seeding database.");
                    return(0);
                }

                Log.Information("Starting host...");
                host.Run();
                return(0);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly.");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }