From 110adac9eef88644febfa82db52a728d1618b357 Mon Sep 17 00:00:00 2001 From: Niggl Date: Tue, 7 Jul 2020 13:13:15 +0200 Subject: [PATCH 1/5] Added Comments --- ScrumTaskboard/Startup.cs | 60 +++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/ScrumTaskboard/Startup.cs b/ScrumTaskboard/Startup.cs index 3b8f09d..0643167 100644 --- a/ScrumTaskboard/Startup.cs +++ b/ScrumTaskboard/Startup.cs @@ -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. + /// + /// This method gets called by the runtime. + /// It's used to add services to the container. + /// + /// 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() .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. + /// + /// This method gets called by the runtime. + /// This method gets used to configure the HTTP request pipeline. + /// + /// + /// 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().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService(); 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 /// 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}"; - } + } } } From 0a4baf6c167fefd2cb13f44604267c2d980d62ad Mon Sep 17 00:00:00 2001 From: Niggl Date: Tue, 7 Jul 2020 13:14:10 +0200 Subject: [PATCH 2/5] Added ReDoc --- ScrumTaskboard/Startup.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ScrumTaskboard/Startup.cs b/ScrumTaskboard/Startup.cs index 0643167..0cc602a 100644 --- a/ScrumTaskboard/Startup.cs +++ b/ScrumTaskboard/Startup.cs @@ -107,8 +107,14 @@ namespace ScrumTaskboard }; }); - //Use the SwaggerUi v3 to provide a SwaggerUI based API explorer + //Use the SwaggerUi v3 to provide a SwaggerUI based API explorer at "/swagger" app.UseSwaggerUi3(); + + //Use Redoc to provide a ReDoc based API explorer at "/redoc" + app.UseReDoc(options => + { + options.Path = "/redoc"; + }); } /// From c8cacd434a04c9a64bd8c4cf8ddbffe06a136e75 Mon Sep 17 00:00:00 2001 From: Niggl Date: Tue, 7 Jul 2020 13:20:10 +0200 Subject: [PATCH 3/5] Added Demo Server to the endpoints --- ScrumTaskboard/Startup.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ScrumTaskboard/Startup.cs b/ScrumTaskboard/Startup.cs index 0cc602a..0bce047 100644 --- a/ScrumTaskboard/Startup.cs +++ b/ScrumTaskboard/Startup.cs @@ -104,6 +104,7 @@ namespace ScrumTaskboard app.UseOpenApi(a => { a.PostProcess = (document, _) => { document.Schemes = new[] { NSwag.OpenApiSchema.Https, NSwag.OpenApiSchema.Http }; + document.Servers.Add(new NSwag.OpenApiServer { Description = "Demo Server", Url = "https://taskboard.dev.nig.gl/" }); }; }); From 23632d10f8b17a5904f382e73fdab22ec6316800 Mon Sep 17 00:00:00 2001 From: Niggl Date: Thu, 9 Jul 2020 18:23:17 +0200 Subject: [PATCH 4/5] Specified some comments in startup.cs --- ScrumTaskboard/Startup.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ScrumTaskboard/Startup.cs b/ScrumTaskboard/Startup.cs index 0bce047..47cc728 100644 --- a/ScrumTaskboard/Startup.cs +++ b/ScrumTaskboard/Startup.cs @@ -11,6 +11,10 @@ namespace ScrumTaskboard { public class Startup { + /// + /// Default constructor that "just" sets the configuration (needed by default for the ASP.NetCore Webservice) + /// + /// configuration for the new Startup object public Startup(IConfiguration configuration) { Configuration = configuration; @@ -119,9 +123,9 @@ namespace ScrumTaskboard } /// - /// Creates DB Connection String based on ENV vars and default vars. + /// Creates DB connection string based on ENV vars and default vars. /// - /// + /// DB connection string public string GetConnectionString() { //Read the parameters for the connection string form env vars From 94101093c950f19e0d679a89682c3293a6c2da8d Mon Sep 17 00:00:00 2001 From: Niggl Date: Thu, 9 Jul 2020 18:23:41 +0200 Subject: [PATCH 5/5] Specified URL for the SwaggerUI --- ScrumTaskboard/Startup.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ScrumTaskboard/Startup.cs b/ScrumTaskboard/Startup.cs index 47cc728..edf53ec 100644 --- a/ScrumTaskboard/Startup.cs +++ b/ScrumTaskboard/Startup.cs @@ -113,7 +113,10 @@ namespace ScrumTaskboard }); //Use the SwaggerUi v3 to provide a SwaggerUI based API explorer at "/swagger" - app.UseSwaggerUi3(); + app.UseSwaggerUi3(options => + { + options.Path = "/swagger"; + }); //Use Redoc to provide a ReDoc based API explorer at "/redoc" app.UseReDoc(options =>