aboutsummaryrefslogtreecommitdiffstats
path: root/src/Startup.cs
blob: 8ad5b07dbe62993efea5af0d610647fe55b49e24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.Default);

			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 = AppCookies.Session.Name;
						options.Cookie.SameSite = SameSiteMode.Strict;
						options.Cookie.HttpOnly = true;
						options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
					});

			services.AddControllers()
					.AddJsonOptions(AppJsonSettings.Default);
		}

		// 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();
			});
		}
	}
}