-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
165 lines (132 loc) · 5.05 KB
/
Program.cs
File metadata and controls
165 lines (132 loc) · 5.05 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Net;
using bloom.Models;
using bloom.Services;
using bloom.Data;
using bloom.Repositories;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.Options;
using Pomelo.EntityFrameworkCore.MySql.Internal;
using Microsoft.AspNetCore.DataProtection;
var builder = WebApplication.CreateBuilder(args);
// Get ConnectionString
var build_environmment = builder.Environment.EnvironmentName;
var ConnectionString = build_environmment == "Production"
? builder.Configuration.GetConnectionString("ProductionConnection")
: builder.Configuration.GetConnectionString("DefaultConnection");
Console.WriteLine($"ConnectionString: {ConnectionString}");
// ============ Add services to the container. ============
if (build_environmment == "Development")
{
builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// builder.WebHost.ConfigureKestrel(options =>
// {
// options.ListenAnyIP(8080); // HTTP
// options.ListenAnyIP(2443, listenOptions => listenOptions.UseHttps()); // HTTPS optional
// });
}
// Add DB Context
builder.Services.AddDbContext<BloomDbContext>(options =>
options.UseMySql(ConnectionString,
new MySqlServerVersion(new Version(11, 7, 2)),
mySqlOptions => mySqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null
)));
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo("/var/dpkeys"))
.SetApplicationName("BloomServer")
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));
// Add identity
builder.Services.AddIdentity<Account, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<BloomDbContext>()
.AddDefaultTokenProviders();
// =========== Add Custom Services ===========
builder.Services.AddScoped<IAccountService, AccountService>();
builder.Services.AddScoped<IRobotService, RobotService>();
// Add RobotSession Services and Repositories
builder.Services.AddSingleton<IRobotStateRepository, InMemoryRobotStateRepository>();
builder.Services.AddScoped<IRobotSessionRepository, RobotSessionRepository>();
builder.Services.AddScoped<ISessionCodeService, SessionCodeService>();
builder.Services.AddScoped<IRobotSessionService, RobotSessionService>();
builder.Services.AddScoped<IRobotStateService, RobotStateService>();
// Add MVC model
builder.Services.AddControllersWithViews();
// Add Cookie Auth
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = builder.Configuration.GetValue<string>("LoginPath");
options.LogoutPath = builder.Configuration.GetValue<string>("LogoutPath");
options.Cookie.HttpOnly = true;
//TODO: development comment lul
//options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
//options.Cookie.SameSite = SameSiteMode.Strict;
options.Cookie.Name = "bloom_cookie";
});
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// Enable CORS for development
// TODO: add production check
builder.Services.AddCors(options => {
options.AddDefaultPolicy(policy => {
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// authorization policies
builder.Services.AddAuthorization(options =>
{
// options.AddPolicy(
// //only Admins can create accounts
// "CanCreateAccount", policy => policy.RequireRole("Admin", "Facilitator"));
});
var app = builder.Build();
// ============ Configure the HTTP request pipeline. ============
if (app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseHsts();
}
// app.UseHttpsRedirection();
app.UseCors();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
using (var scope = app.Services.CreateScope())
{
// var db = scope.ServiceProvider.GetRequiredService<BloomDbContext>();
// db.Database.Migrate();
// var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
// await BloomDbContext.SeedRolesAsync(roleManager);
}
// app.MapControllerRoute(
// name: "default",
// pattern: "{controller}/{action=Index}/{id?}");
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();