This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
109 lines (98 loc) · 3.8 KB
/
Program.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Serilog;
using shipping_service.Models;
using shipping_service.Persistence.Database;
using shipping_service.Repositories;
using shipping_service.Services;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Configure services
builder.Configuration.AddJsonFile("appsettings.json");
string dbConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
bool autoApplyMigrations = builder.Configuration.GetValue<bool>("AutomaticallyApplyMigrations");
bool addSeedData = builder.Configuration.GetValue<bool>("AddSeedDataIfDBEmpty");
bool detailedDbLogging = builder.Configuration.GetValue<bool>("DetailedDbLogging");
double authenticationCookieLifetimeDays = builder.Configuration.GetValue<double>("AuthenticationCookieLifetimeDays");
builder.Services.AddDbContext<DatabaseContext>(option =>
{
option.UseNpgsql(dbConnectionString);
if (detailedDbLogging)
{
// enable logging for debugging
option.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.LogTo(Console.WriteLine);
}
});
builder.Services.AddScoped<ICourierRepository, CourierRepository>();
builder.Services.AddScoped<IShipmentRepository, ShipmentRepository>();
builder.Services.AddScoped<IShipmentService, ShipmentService>();
builder.Services.AddScoped<ICourierService, CourierService>();
builder.Services.AddScoped<IPostMachineRepository, PostMachineRepository>();
builder.Services.AddScoped<IPostMachineService, PostMachineService>();
string generatorInterface = builder.Configuration.GetValue<string>("GeneratorInterface");
if (generatorInterface == "CodeGenerator")
{
builder.Services.AddScoped<ICodeGenerator, CodeGenerator>();
}
else
{
builder.Services.AddScoped<ICodeGenerator, CodeGeneratorByDigit>();
}
builder.Services.AddScoped<ISenderRepository, SenderRepository>();
builder.Services.AddScoped<IShipmentService, ShipmentService>();
builder.Services.AddScoped<IAccountService, AccountService>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(authenticationCookieLifetimeDays);
options.Cookie.Name = "auth";
options.Cookie.SameSite = SameSiteMode.Strict;
});
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddHttpContextAccessor();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<TokenProvider>();
builder.Services.AddControllers();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.File(@"C:logs\logs.txt")
.CreateLogger();
builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console()
.WriteTo.File(@"C:\Logs\logs.txt", rollingInterval: RollingInterval.Day));
//Build
WebApplication app = builder.Build();
if (autoApplyMigrations)
{
Console.WriteLine("Creating the DB and applying DB migrations");
// create DB with all migrations applied on startup
using IServiceScope serviceScope = app.Services.CreateScope();
DatabaseContext context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
context.Database.Migrate();
}
if (addSeedData)
{
Console.WriteLine("Adding seed data to DB");
SeedData.PopulateIfEmpty(app);
}
//Configure
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapControllers();
});
app.Run();