aboutsummaryrefslogtreecommitdiffstats
path: root/src/Startup.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Startup.cs')
-rw-r--r--src/Startup.cs74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/Startup.cs b/src/Startup.cs
new file mode 100644
index 0000000..a3a7009
--- /dev/null
+++ b/src/Startup.cs
@@ -0,0 +1,74 @@
+using System;
+using System.IO;
+using IOL.WebApi.Template.Data.Database;
+using IOL.WebApi.Template.Data.Static;
+using IOL.Helpers;
+using Microsoft.AspNetCore.Authentication.Cookies;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.DataProtection;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Http;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+namespace IOL.WebApi.Template
+{
+ public class Startup
+ {
+ public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) {
+ Configuration = configuration;
+ WebHostEnvironment = webHostEnvironment;
+ }
+
+ private IWebHostEnvironment WebHostEnvironment { get; }
+ private IConfiguration Configuration { get; }
+
+ // This method gets called by the runtime. Use this method to add services to the container.
+ public void ConfigureServices(IServiceCollection services) {
+ services.AddDataProtection()
+ .PersistKeysToFileSystem(new DirectoryInfo(AppPaths.DataProtectionKeys.HostPath));
+
+ services.Configure(AppJsonSettings.Value);
+
+ services.AddDbContext<AppDbContext>(options => {
+ options.UseNpgsql("Server={DB_HOST};Port={DB_PORT};Database={DB_NAME};User Id={DB_USER};Password={DB_PASSWORD}".UnicornFormatWithEnvironment(Configuration),
+ builder => {
+ builder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
+ builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), default);
+ })
+ .UseSnakeCaseNamingConvention();
+ if (WebHostEnvironment.IsDevelopment())
+ options.EnableSensitiveDataLogging();
+ });
+
+ services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
+ .AddCookie(options => {
+ options.Cookie.Name = "";
+ options.Cookie.SameSite = SameSiteMode.Strict;
+ options.Cookie.HttpOnly = true;
+ options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
+ });
+
+ services.AddControllers()
+ .AddJsonOptions(AppJsonSettings.Value);
+ }
+
+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+ public void Configure(IApplicationBuilder app) {
+ if (WebHostEnvironment.IsDevelopment()) {
+ app.UseDeveloperExceptionPage();
+ }
+
+ app.UseStaticFiles();
+ app.UseRouting();
+ app.UseAuthentication();
+ app.UseAuthorization();
+ app.UseStatusCodePages();
+ app.UseEndpoints(endpoints => {
+ endpoints.MapControllers();
+ });
+ }
+ }
+}