source: KernelRecordsMVC.Web/Program.cs@ fa0fbaf

main
Last change on this file since fa0fbaf was fa0fbaf, checked in by mmilevski <markomilevski3@…>, 4 weeks ago

Added major improvements.

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