From 4322330745d18bb28f7deee1a54ae3c748645d92 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 25 Apr 2021 00:10:17 +0200 Subject: Initial commit --- src/Startup.cs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Startup.cs (limited to 'src/Startup.cs') 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(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(); + }); + } + } +} -- cgit v1.3