Added simple env var import

This commit is contained in:
Niggl1999 2020-06-04 09:54:10 +02:00
parent 7ffc5962b7
commit 5badaa819a

View File

@ -1,69 +1,76 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace ScrumTaskboard namespace ScrumTaskboard
{ {
public class Startup public class Startup
{ {
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
Configuration = configuration; Configuration = configuration;
} }
public IConfiguration Configuration { get; } public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddCors(o => o.AddPolicy("AllowAll", builder => // Get all the env vars for the DB connection
{ string dbHost = Environment.GetEnvironmentVariable("DATABASE_HOST");
builder.AllowAnyOrigin() string dbPort = Environment.GetEnvironmentVariable("DATABASE_PORT");
.AllowAnyMethod() string dbName = Environment.GetEnvironmentVariable("DATABASE_NAME");
.AllowAnyHeader(); string dbUser = Environment.GetEnvironmentVariable("DATABASE_USER");
})); string dbPassword = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");
services.AddScoped(serviceProvider => new TaskContext(
new DbContextOptionsBuilder<TaskContext>() services.AddCors(o => o.AddPolicy("AllowAll", builder =>
.UseNpgsql("Host=nig.gl; Port=8543; Username=scrum; Database=taskboard; Password=c6gXud7YvBWp2sgxSgy4wRN") {
.Options)); builder.AllowAnyOrigin()
services.AddControllers(); .AllowAnyMethod()
} .AllowAnyHeader();
}));
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. services.AddScoped(serviceProvider => new TaskContext(
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) new DbContextOptionsBuilder<TaskContext>()
{ .UseNpgsql($"Host={dbHost}; Port={dbPort}; Username={dbUser}; Database={dbName}; Password={dbPassword}")
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) .Options));
{ services.AddControllers();
var context = serviceScope.ServiceProvider.GetRequiredService<TaskContext>(); }
context.Database.EnsureCreated();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment()) {
{ using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
app.UseDeveloperExceptionPage(); {
} var context = serviceScope.ServiceProvider.GetRequiredService<TaskContext>();
context.Database.EnsureCreated();
//app.UseHttpsRedirection(); }
app.UseCors("AllowAll");
if (env.IsDevelopment())
app.UseRouting(); {
app.UseDeveloperExceptionPage();
app.UseAuthorization(); }
app.UseEndpoints(endpoints => //app.UseHttpsRedirection();
{ app.UseCors("AllowAll");
endpoints.MapControllers();
}); app.UseRouting();
}
} app.UseAuthorization();
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}