2020-06-04 14:53:59 +00:00
|
|
|
using System;
|
2020-06-05 13:27:41 +00:00
|
|
|
using System.Text.Json.Serialization;
|
2020-06-04 14:53:59 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
namespace ScrumTaskboard
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
2020-06-04 08:01:54 +00:00
|
|
|
{
|
2020-06-04 14:53:59 +00:00
|
|
|
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
|
|
|
|
{
|
|
|
|
builder.AllowAnyOrigin()
|
|
|
|
.AllowAnyMethod()
|
|
|
|
.AllowAnyHeader();
|
2020-06-05 13:43:44 +00:00
|
|
|
}));
|
2020-06-05 13:27:41 +00:00
|
|
|
services.AddMvc().AddJsonOptions(o =>
|
|
|
|
{
|
|
|
|
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
|
|
o.JsonSerializerOptions.IgnoreNullValues = true;
|
|
|
|
});
|
2020-06-04 14:53:59 +00:00
|
|
|
services.AddScoped(serviceProvider => new TaskContext(
|
|
|
|
new DbContextOptionsBuilder<TaskContext>()
|
2020-06-05 13:43:44 +00:00
|
|
|
.UseNpgsql(GetConnectionString())
|
2020-06-04 14:53:59 +00:00
|
|
|
.Options));
|
|
|
|
services.AddControllers();
|
2020-06-04 14:55:52 +00:00
|
|
|
services.AddOpenApiDocument(
|
2020-06-04 07:17:27 +00:00
|
|
|
config =>
|
|
|
|
{
|
|
|
|
config.PostProcess = document =>
|
|
|
|
{
|
|
|
|
document.Info.Version = "v1";
|
|
|
|
document.Info.Title = "Scrum Taskboard API";
|
|
|
|
document.Info.Description = "A RESTful API for the Scrum Taskboard Backend";
|
|
|
|
document.Info.TermsOfService = "None";
|
|
|
|
};
|
2020-06-04 14:55:52 +00:00
|
|
|
});
|
2020-06-04 14:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
{
|
|
|
|
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
|
|
|
{
|
|
|
|
var context = serviceScope.ServiceProvider.GetRequiredService<TaskContext>();
|
|
|
|
context.Database.EnsureCreated();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
|
|
|
});
|
2020-06-22 12:36:59 +00:00
|
|
|
app.UseOpenApi(a => {
|
|
|
|
a.PostProcess = (document, _) => {
|
|
|
|
document.Schemes = new[] { NSwag.OpenApiSchema.Https, NSwag.OpenApiSchema.Http };
|
|
|
|
};
|
|
|
|
});
|
2020-06-04 14:55:52 +00:00
|
|
|
app.UseSwaggerUi3();
|
2020-06-04 14:53:59 +00:00
|
|
|
}
|
2020-06-05 13:43:44 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates DB Connection String based on ENV vars and default vars.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public string GetConnectionString()
|
|
|
|
{
|
|
|
|
string dbHost;
|
|
|
|
string dbPort;
|
|
|
|
string dbName;
|
|
|
|
string dbUser;
|
|
|
|
string dbPassword;
|
|
|
|
|
|
|
|
dbHost = Environment.GetEnvironmentVariable("DATABASE_HOST");
|
|
|
|
dbPort = Environment.GetEnvironmentVariable("DATABASE_PORT");
|
|
|
|
dbName = Environment.GetEnvironmentVariable("DATABASE_NAME");
|
|
|
|
dbUser = Environment.GetEnvironmentVariable("DATABASE_USER");
|
|
|
|
dbPassword = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");
|
|
|
|
|
|
|
|
if(dbHost == null || dbPort == null || dbName == null || dbUser == null || dbPassword == null)
|
|
|
|
{
|
|
|
|
dbHost = "nig.gl";
|
|
|
|
dbPort = "8543";
|
|
|
|
dbName = "taskboard";
|
|
|
|
dbUser = "scrum";
|
|
|
|
dbPassword = "c6gXud7YvBWp2sgxSgy4wRN";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $"Host={dbHost}; Port={dbPort}; Username={dbUser}; Database={dbName}; Password={dbPassword}";
|
|
|
|
}
|
2020-06-04 14:53:59 +00:00
|
|
|
}
|
|
|
|
}
|