Added Comments

This commit is contained in:
Nicolai Ort 2020-07-07 13:13:15 +02:00
parent faafbbeb62
commit 110adac9ee
1 changed files with 42 additions and 18 deletions

View File

@ -16,27 +16,44 @@ namespace ScrumTaskboard
Configuration = configuration;
}
//Default getter for the configuration
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
/// <summary>
/// This method gets called by the runtime.
/// It's used to add services to the container.
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
//CORS (Cross Origin Resource Sharing) controls from where a service can be accessed
//Here we allow access from every origin by adding a policy called "AllowAll" that allows access form every origin with every method and any headers
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
//JSON parsing options control how JSON is parsed to get input or send output from/to the API endpoints
//Here we allow null values for parameters and add a Enum converter for the "ScrumPrio" Enum
services.AddMvc().AddJsonOptions(o =>
{
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
o.JsonSerializerOptions.IgnoreNullValues = true;
});
//Adds the PostgreSQL based database context to provide database communication
services.AddScoped(serviceProvider => new TaskContext(
new DbContextOptionsBuilder<TaskContext>()
.UseNpgsql(GetConnectionString())
.Options));
//Default Service call that adds all controllers (and thereby API endpoints) to the service
services.AddControllers();
//OpenAPI Document settings for autogenerating the OpenAPI v3 documentation
//Here we set the basic information for the documentation - mainly: version, title, description and TOS
services.AddOpenApiDocument(
config =>
{
@ -50,36 +67,47 @@ namespace ScrumTaskboard
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// <summary>
/// This method gets called by the runtime.
/// This method gets used to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//Ensure the existance of all tabels in the database (by creating them if they're missing)
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<TaskContext>();
context.Database.EnsureCreated();
}
//Checks is developer mode is enabled in the env and adds the developer only exception page for debugging if it is enabled
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//app.UseHttpsRedirection();
//Use the previously created "AllowAll" CORS policy
app.UseCors("AllowAll");
//Use the default ASP.NetCore routing
app.UseRouting();
app.UseAuthorization();
//Use the endpoints provided by the controllers and map their urls accordingly
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//Use the configured OpenAPI document config and add the url schemes for http and https.
app.UseOpenApi(a => {
a.PostProcess = (document, _) => {
document.Schemes = new[] { NSwag.OpenApiSchema.Https, NSwag.OpenApiSchema.Http };
};
});
//Use the SwaggerUi v3 to provide a SwaggerUI based API explorer
app.UseSwaggerUi3();
}
@ -89,18 +117,14 @@ namespace ScrumTaskboard
/// <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");
//Read the parameters for the connection string form env vars
string dbHost = Environment.GetEnvironmentVariable("DATABASE_HOST");
string dbPort = Environment.GetEnvironmentVariable("DATABASE_PORT");
string dbName = Environment.GetEnvironmentVariable("DATABASE_NAME");
string dbUser = Environment.GetEnvironmentVariable("DATABASE_USER");
string dbPassword = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");
//Check if any of the env vars were null and set the parameters for the connection string to default parameters of any of them were null
if(dbHost == null || dbPort == null || dbName == null || dbUser == null || dbPassword == null)
{
dbHost = "nig.gl";
@ -110,8 +134,8 @@ namespace ScrumTaskboard
dbPassword = "c6gXud7YvBWp2sgxSgy4wRN";
}
//Return the created connection String
return $"Host={dbHost}; Port={dbPort}; Username={dbUser}; Database={dbName}; Password={dbPassword}";
}
}
}
}