-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (98 loc) · 2.63 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (98 loc) · 2.63 KB
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
110
111
112
113
114
115
116
117
118
using System.Globalization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Storefront.Data;
using Storefront.Services;
using Storefront.Settings;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddUserSecrets<Program>();
// Add services to the container.
builder.Services.AddRazorPages(options =>
{
options.Conventions
.AuthorizeFolder("/Admin");
options.Conventions
.AllowAnonymousToPage(
"/Admin/Login");
});
builder.Services.AddHttpClient();
var culture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
var environment = builder.Environment;
if (environment.IsDevelopment())
{
builder.Services.AddDbContext<ShopContext>(
options =>
{
options.UseSqlite(
builder.Configuration
.GetConnectionString(
"ShopDbConnection"));
});
}
else
{
var rawUrl =
Environment.GetEnvironmentVariable(
"DATABASE_URL");
var uri = new Uri(rawUrl);
var userInfo = uri.UserInfo.Split(':');
var connectionString =
$@"Host={uri.Host};Port={uri.Port};
Username={userInfo[0]};Password={userInfo[1]};
Database={uri.AbsolutePath.TrimStart('/')};
SSL Mode=Disable;Trust Server Certificate=true";
builder.Services.AddDbContext<ShopContext>(
options =>
options.UseNpgsql(
connectionString,
npgsqlOptions =>
{
npgsqlOptions
.EnableRetryOnFailure();
}));
}
builder.Services
.AddAuthentication(
CookieAuthenticationDefaults
.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath =
"/Admin/Login";
});
builder.Services.AddScoped<BasketService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddSession();
builder.Services.Configure<
EmailSettings>(
builder.Configuration
.GetSection("Email"));
builder.Services.AddScoped<
EmailService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
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.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets();
app.MapRazorPages()
.WithStaticAssets();
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider
.GetRequiredService<ShopContext>();
context.Database.Migrate();
SeedData.Initialize(context);
}
app.Run();