-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (94 loc) · 3.02 KB
/
Program.cs
File metadata and controls
104 lines (94 loc) · 3.02 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
using System.Security.Claims;
using HvZBot.discordBotService;
using HvZBot.utils;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
var root = Directory.GetCurrentDirectory();
var dotEnv = Path.Combine(root, "secrets.env");
DotEnv.Load(dotEnv);
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddLogging(configure =>
{
configure.AddFilter("Microsoft", LogLevel.Warning);
configure.AddConsole(consoleOptions =>
{
consoleOptions.LogToStandardErrorThreshold = LogLevel.Warning;
});
});
builder.Services.AddHostedService<Bot>();
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "Discord";
})
.AddCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
})
.AddDiscord(options =>
{
options.ClientId = Environment.GetEnvironmentVariable("ClientId")!;
options.ClientSecret = Environment.GetEnvironmentVariable("ClientSecret")!;
options.Scope.Add("identify");
options.Scope.Add("guilds");
options.SaveTokens = true;
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "username");
});
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddCors(options => //disable in prod?
{
options.AddPolicy("AllowLocalhost",
policy =>
{
policy.WithOrigins("http://localhost:3000", "http://localhost:5173", "https://localhost:44417", "https://localhost:7235")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
options.AddPolicy("AllowHvZLive",
policy =>
{
policy.WithOrigins("https://hvzbot.live")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
builder.Services.AddHttpClient("DiscordApi", client =>
{
client.BaseAddress = new Uri("https://discord.com/api/v10");
});
builder.Services.AddScoped<DiscordApiService>();
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors(app.Environment.IsDevelopment() ? "AllowLocalhost" : "AllowHvZLive");
app.UseAuthentication();
app.UseSession();
app.UseSessionRevalidation();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();