| [08aefc6] | 1 | using Microsoft.EntityFrameworkCore;
|
|---|
| [fa0fbaf] | 2 | using Npgsql;
|
|---|
| 3 | using Npgsql.NameTranslation;
|
|---|
| 4 | using KernelRecordsMVC.Domain.Enums;
|
|---|
| 5 | using KernelRecordsMVC.Infrastructure.Data;
|
|---|
| [1dcdb2c] | 6 | using Microsoft.AspNetCore.Identity;
|
|---|
| 7 | using KernelRecordsMVC.Models;
|
|---|
| [08aefc6] | 8 |
|
|---|
| 9 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| 10 |
|
|---|
| [fa0fbaf] | 11 | // ==========================================
|
|---|
| 12 | // AUTHENTICATION
|
|---|
| 13 | // ==========================================
|
|---|
| 14 |
|
|---|
| 15 | builder.Services
|
|---|
| 16 | .AddAuthentication("Cookies")
|
|---|
| 17 | .AddCookie("Cookies", options =>
|
|---|
| 18 | {
|
|---|
| 19 | options.LoginPath = "/Account/Login";
|
|---|
| 20 | options.AccessDeniedPath = "/Account/Login";
|
|---|
| 21 | options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
|---|
| 22 | options.SlidingExpiration = true;
|
|---|
| 23 | });
|
|---|
| 24 |
|
|---|
| 25 | builder.Services.AddAuthorization();
|
|---|
| 26 |
|
|---|
| [08aefc6] | 27 | builder.Services.AddControllersWithViews();
|
|---|
| 28 |
|
|---|
| [1dcdb2c] | 29 | builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
|
|---|
| 30 |
|
|---|
| [fa0fbaf] | 31 | // ==========================================
|
|---|
| 32 | // DATABASE
|
|---|
| 33 | // ==========================================
|
|---|
| 34 |
|
|---|
| 35 | var dataSourceBuilder = new NpgsqlDataSourceBuilder(
|
|---|
| 36 | builder.Configuration.GetConnectionString("KernelRecords"));
|
|---|
| 37 |
|
|---|
| 38 | var nameTranslator = new NpgsqlNullNameTranslator();
|
|---|
| 39 |
|
|---|
| 40 | dataSourceBuilder.MapEnum<AdminType>(
|
|---|
| 41 | "project.admin_type",
|
|---|
| 42 | nameTranslator);
|
|---|
| 43 |
|
|---|
| 44 | dataSourceBuilder.MapEnum<ProductFormat>(
|
|---|
| 45 | "project.product_format",
|
|---|
| 46 | nameTranslator);
|
|---|
| 47 |
|
|---|
| 48 | dataSourceBuilder.MapEnum<PaymentMethodType>(
|
|---|
| 49 | "project.payment_method_type",
|
|---|
| 50 | nameTranslator);
|
|---|
| 51 |
|
|---|
| 52 | dataSourceBuilder.MapEnum<OrderStatusType>(
|
|---|
| 53 | "project.order_status_type",
|
|---|
| 54 | nameTranslator);
|
|---|
| 55 |
|
|---|
| 56 | dataSourceBuilder.MapEnum<ModificationType>(
|
|---|
| 57 | "project.modification_type",
|
|---|
| 58 | nameTranslator);
|
|---|
| 59 |
|
|---|
| 60 | dataSourceBuilder.MapEnum<ArtistReleaseType>(
|
|---|
| 61 | "project.artist_release_type",
|
|---|
| 62 | nameTranslator);
|
|---|
| 63 |
|
|---|
| 64 | var dataSource = dataSourceBuilder.Build();
|
|---|
| 65 |
|
|---|
| [08aefc6] | 66 | builder.Services.AddDbContext<KernelRecordsContext>(options =>
|
|---|
| [fa0fbaf] | 67 | options.UseNpgsql(dataSource));
|
|---|
| [08aefc6] | 68 |
|
|---|
| [fa0fbaf] | 69 | // ==========================================
|
|---|
| 70 | // SESSION
|
|---|
| 71 | // ==========================================
|
|---|
| 72 |
|
|---|
| 73 | builder.Services.AddSession(options =>
|
|---|
| 74 | {
|
|---|
| 75 | options.IdleTimeout = TimeSpan.FromHours(2);
|
|---|
| 76 | options.Cookie.HttpOnly = true;
|
|---|
| 77 | options.Cookie.IsEssential = true;
|
|---|
| 78 | });
|
|---|
| [08aefc6] | 79 |
|
|---|
| 80 | var app = builder.Build();
|
|---|
| 81 |
|
|---|
| [fa0fbaf] | 82 | // ==========================================
|
|---|
| 83 | // MIDDLEWARE
|
|---|
| 84 | // ==========================================
|
|---|
| 85 |
|
|---|
| [08aefc6] | 86 | if (!app.Environment.IsDevelopment())
|
|---|
| 87 | {
|
|---|
| 88 | app.UseExceptionHandler("/Home/Error");
|
|---|
| 89 | app.UseHsts();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | app.UseHttpsRedirection();
|
|---|
| [fa0fbaf] | 93 |
|
|---|
| [08aefc6] | 94 | app.UseStaticFiles();
|
|---|
| 95 |
|
|---|
| 96 | app.UseRouting();
|
|---|
| 97 |
|
|---|
| 98 | app.UseSession();
|
|---|
| 99 |
|
|---|
| [fa0fbaf] | 100 | app.UseAuthentication();
|
|---|
| 101 |
|
|---|
| [08aefc6] | 102 | app.UseAuthorization();
|
|---|
| 103 |
|
|---|
| 104 | app.MapControllerRoute(
|
|---|
| 105 | name: "default",
|
|---|
| 106 | pattern: "{controller=Home}/{action=Index}/{id?}");
|
|---|
| 107 |
|
|---|
| [fa0fbaf] | 108 | app.Run();
|
|---|