diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index f095493994a5b4c950542864524b7f21e5199dd5..b15b91cef5d34e74460756ba52fd1a1da80d42c0 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -31,25 +31,35 @@ namespace Tsi1.Api { public class Startup { - public Startup(IConfiguration configuration) + public Startup(IConfiguration configuration, IWebHostEnvironment env) { Configuration = configuration; + _env = env; } public IConfiguration Configuration { get; } + private readonly IWebHostEnvironment _env; // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddControllers(); + string postgreSqlSection; + string mongoDbSection; + if (_env.IsProduction()) + { + postgreSqlSection = "PostgreSqlCloud"; + mongoDbSection = "Tsi1DatabaseSettingsCloud"; + } + else + { + postgreSqlSection = "PostgreSql"; + mongoDbSection = "Tsi1DatabaseSettings"; + } + services.AddControllers(); + services.AddSignalR(); - var isElasticCloud = bool.Parse(Configuration.GetSection("IsElasticCloud").Value); - - var postgreSqlSection = isElasticCloud ? "PostgreSqlCloud" : "PostgreSql"; - var mongoDbSection = isElasticCloud ? "Tsi1DatabaseSettingsCloud" : "Tsi1DatabaseSettings"; - var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>(); services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString(postgreSqlSection))); @@ -167,12 +177,16 @@ namespace Tsi1.Api } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Tsi1Context context) { + if (env.IsProduction()) + { + context.Database.Migrate(); + } + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - } app.UseSwagger(); diff --git a/Tsi1.Api/Tsi1.Api/appsettings.json b/Tsi1.Api/Tsi1.Api/appsettings.json index 48f2b50a243cb8e2510b4b13a1c36ce23d33e726..aeffabd1acc0a4a5296e376e85c35a1114d2770e 100644 --- a/Tsi1.Api/Tsi1.Api/appsettings.json +++ b/Tsi1.Api/Tsi1.Api/appsettings.json @@ -1,5 +1,4 @@ { - "IsElasticCloud": true, "ConnectionStrings": { "PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111", "PostgreSqlCloud": "Host=postgres;Database=tsi1;Username=postgres;Password=postgres" diff --git a/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs b/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs index bbddd4ce1d5a1f114681fa7689fd907640977df4..781545a9ae9113a925b95bb034759a2049025a5c 100644 --- a/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs +++ b/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs @@ -1,10 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; using System.IO; -using System.Text; namespace Tsi1.DataLayer { @@ -17,10 +14,7 @@ namespace Tsi1.DataLayer .AddJsonFile(@Directory.GetCurrentDirectory() + "/../Tsi1.Api/appsettings.json") .Build(); - var isElasticCloud = bool.Parse(configuration.GetSection("IsElasticCloud").Value); - var databaseSection = isElasticCloud ? "PostgreSqlCloud" : "PostgreSql"; - - var connectionString = configuration.GetConnectionString(databaseSection); + var connectionString = configuration.GetConnectionString("PostgreSql"); var builder = new DbContextOptionsBuilder<Tsi1Context>(); builder.UseNpgsql(connectionString);