Skip to content
Snippets Groups Projects
Commit a7b43428 authored by Lucca Santangelo's avatar Lucca Santangelo
Browse files

enviroment configuration

parent 79d0017a
No related branches found
No related tags found
1 merge request!26Develop
Pipeline #10318 passed
......@@ -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();
......
{
"IsElasticCloud": true,
"ConnectionStrings": {
"PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111",
"PostgreSqlCloud": "Host=postgres;Database=tsi1;Username=postgres;Password=postgres"
......
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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment