-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
executable file
·88 lines (75 loc) · 3.05 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
using Microsoft.Extensions.FileProviders;
using OAuthServer;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
var logConfiguration = new LoggerConfiguration()
.WriteTo.Console();
builder.Host.UseSerilog();
var stateTokenCache = new MemCacher<RequestState>();
var fetchTokenCache = new MemCacher<FetchToken>();
var accessTokenCache = new MemCacher<AccessToken>();
var httpClient = new HttpClient(
// Pr docs, we refresh every 15 min to ensure DNS TTL is re-applied
new SocketsHttpHandler() { PooledConnectionLifetime = TimeSpan.FromMinutes(15) }
);
var appconfig = ConfigurationLoader.LoadApplicationConfiguration();
var renderers = ConfigurationLoader.LoadRenderers(appconfig);
var services = ConfigurationLoader.LoadServices(appconfig)
.ToDictionary(x => x.Id);
var storage = string.IsNullOrWhiteSpace(appconfig.StorageString)
? null
: new StorageProvider(appconfig.StorageString);
var appContext = new ApplicationContext(
appconfig,
services,
httpClient,
stateTokenCache,
fetchTokenCache,
accessTokenCache,
renderers,
storage
);
if (!string.IsNullOrWhiteSpace(appconfig.SeqLogUrl))
logConfiguration = logConfiguration.WriteTo.Seq(appconfig.SeqLogUrl, apiKey: appconfig.SeqLogApiKey);
Log.Logger = logConfiguration
.CreateLogger();
var app = builder.Build();
// Support LetsEncrypt
var le_path = Path.Combine(Directory.GetCurrentDirectory(), @".well-known");
if (Directory.Exists(le_path))
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(le_path),
RequestPath = new PathString("/.well-known"),
ServeUnknownFileTypes = true // serve extensionless file
});
}
/*
// TODO: Implement this if it is still being used
('/token-state', TokenStateHandler),
*/
app.MapGet("/login", ctx => OAuthServer.Handler.StartLogin.Handle(ctx, appContext));
app.MapGet("/logged-in", ctx => OAuthServer.Handler.CompleteLogin.Handle(ctx, appContext));
app.MapGet("/privacy-policy", ctx => OAuthServer.Handler.PrivacyPolicy.Handle(ctx, appContext));
app.MapGet("/fetch", ctx => OAuthServer.Handler.Fetch.Handle(ctx, appContext));
app.MapGet("/cli-token", ctx => OAuthServer.Handler.CliToken.Handle(ctx, appContext));
app.MapPost("/cli-token-login", ctx => OAuthServer.Handler.CliTokenLogin.Handle(ctx, appContext));
app.MapGet("/refresh", ctx => OAuthServer.Handler.Refresh.Handle(ctx, appContext));
app.MapPost("/refresh", ctx => OAuthServer.Handler.Refresh.Handle(ctx, appContext));
app.MapGet("/revoke", ctx => OAuthServer.Handler.StartRevoke.Handle(ctx, appContext));
app.MapPost("/revoked", ctx => OAuthServer.Handler.CompleteRevoke.Handle(ctx, appContext));
app.MapGet("/", ctx => OAuthServer.Handler.Index.Handle(ctx, appContext));
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot")),
RequestPath = new PathString("")
});
try
{
app.Run();
}
finally
{
Log.CloseAndFlush();
}