Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (80 loc) · 2.93 KB
/
Copy pathProgram.cs
File metadata and controls
101 lines (80 loc) · 2.93 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
using CollaborativeBoard.Data;
using CollaborativeBoard.Hubs;
using CollaborativeBoard.Services;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var port = Environment.GetEnvironmentVariable("PORT");
if (!string.IsNullOrWhiteSpace(port))
{
builder.WebHost.UseUrls($"http://0.0.0.0:{port}");
}
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
var postgresConnectionString = builder.Configuration.GetConnectionString("Postgres");
if (string.IsNullOrWhiteSpace(postgresConnectionString))
{
throw new InvalidOperationException(
"Missing PostgreSQL connection string. Set ConnectionStrings__Postgres before starting the app.");
}
postgresConnectionString = NormalizePostgresConnectionString(postgresConnectionString);
var keysDirectory = Path.Combine(builder.Environment.ContentRootPath, "App_Data", "keys");
Directory.CreateDirectory(keysDirectory);
builder.Services.AddControllersWithViews();
builder.Services.AddSignalR();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(
postgresConnectionString,
npgsql => npgsql.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(keysDirectory))
.SetApplicationName("Boardspace");
builder.Services.AddScoped<IBoardService, BoardService>();
builder.Services.AddSingleton<BoardPresenceService>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.MigrateAsync();
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
if (app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Boards}/{action=Index}/{id?}");
app.MapControllers();
app.MapHub<BoardHub>("/hubs/boards");
app.Run();
static string NormalizePostgresConnectionString(string value)
{
if (!Uri.TryCreate(value, UriKind.Absolute, out var uri) ||
(uri.Scheme != "postgres" && uri.Scheme != "postgresql"))
{
return value;
}
var userInfo = uri.UserInfo.Split(':', 2);
var username = Uri.UnescapeDataString(userInfo.ElementAtOrDefault(0) ?? string.Empty);
var password = Uri.UnescapeDataString(userInfo.ElementAtOrDefault(1) ?? string.Empty);
var database = uri.AbsolutePath.TrimStart('/');
var builder = new Npgsql.NpgsqlConnectionStringBuilder
{
Host = uri.Host,
Port = uri.Port > 0 ? uri.Port : 5432,
Database = database,
Username = username,
Password = password,
SslMode = Npgsql.SslMode.Require
};
builder["GSS Encryption Mode"] = "Disable";
return builder.ConnectionString;
}