forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.cs
More file actions
280 lines (253 loc) · 11.3 KB
/
Copy pathBuilder.cs
File metadata and controls
280 lines (253 loc) · 11.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using Microsoft.AspNetCore.Authentication.BearerToken;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Npgsql;
using Serilog;
using static NpgsqlRestClient.Config;
namespace NpgsqlRestClient;
public static class Builder
{
public static WebApplicationBuilder Instance { get; private set; } = default!;
public static bool LogToConsole { get; private set; } = false;
public static bool LogToFile { get; private set; } = false;
public static Serilog.ILogger? Logger { get; private set; } = null;
public static bool UseHttpsRedirection { get; private set; } = false;
public static bool UseHsts { get; private set; } = false;
public static void BuildInstance()
{
var staticFilesCfg = Cfg.GetSection("StaticFiles");
string? webRootPath = staticFilesCfg is not null && GetConfigBool("Enabled", staticFilesCfg) is true ? GetConfigStr("RootPath", staticFilesCfg) : null;
var options = new WebApplicationOptions()
{
ApplicationName = GetConfigStr("ApplicationName") ?? Path.GetFileName(Environment.CurrentDirectory),
WebRootPath = webRootPath,
EnvironmentName = GetConfigStr("EnvironmentName") ?? "Production",
};
Instance = WebApplication.CreateEmptyBuilder(options);
Instance.WebHost.UseKestrelCore();
var kestrelConfig = Cfg.GetSection("Kestrel");
Instance.WebHost.ConfigureKestrel((context, options) =>
{
options.Configure(Cfg.GetSection("Kestrel"));
});
var urls = GetConfigStr("Urls");
if (urls is not null)
{
Instance.WebHost.UseUrls(urls.Split(';'));
}
var ssqlCfg = Cfg.GetSection("Ssl");
if (ssqlCfg.Exists() is true && GetConfigBool("Enabled", ssqlCfg) is true)
{
Instance.WebHost.UseKestrelHttpsConfiguration();
UseHttpsRedirection = GetConfigBool("UseHttpsRedirection", ssqlCfg);
UseHsts = GetConfigBool("UseHsts", ssqlCfg);
}
}
public static WebApplication Build() => Instance.Build();
public static void BuildLogger()
{
var logCfg = Cfg.GetSection("Log");
if (logCfg.Exists() is false)
{
LogToConsole = false;
LogToFile = false;
return;
}
Logger = null;
LogToConsole = GetConfigBool("ToConsole", logCfg);
LogToFile = GetConfigBool("ToFile", logCfg);
var filePath = GetConfigStr("FilePath", logCfg);
if (LogToConsole is true || LogToFile is true)
{
var loggerConfig = new LoggerConfiguration().MinimumLevel.Verbose();
foreach (var level in logCfg.GetSection("MinimalLevels").GetChildren())
{
var key = level.Key;
var value = GetEnum<Serilog.Events.LogEventLevel?>(level.Value);
if (value is not null && key is not null)
{
loggerConfig.MinimumLevel.Override(key, value.Value);
}
}
string outputTemplate = GetConfigStr("OutputTemplate", logCfg) ?? "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}";
if (LogToConsole is true)
{
loggerConfig = loggerConfig.WriteTo.Console(
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
outputTemplate: outputTemplate,
theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code);
}
if (LogToFile is true)
{
loggerConfig = loggerConfig.WriteTo.File(
path: filePath ?? "logs/log.txt",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: GetConfigInt("FileSizeLimitBytes", logCfg) ?? 30000000,
retainedFileCountLimit: GetConfigInt("RetainedFileCountLimit", logCfg) ?? 30,
rollOnFileSizeLimit: GetConfigBool("RollOnFileSizeLimit", logCfg, defaultVal: true),
outputTemplate: outputTemplate);
}
var serilog = loggerConfig.CreateLogger();
Logger = serilog.ForContext<Program>();
Instance.Host.UseSerilog(serilog);
Logger?.Information("----> Starting with configuration(s): {0}", Cfg.Providers);
}
}
public static void BuildAuthentication()
{
var authCfg = Cfg.GetSection("Auth");
if (authCfg.Exists() is false)
{
return;
}
var cookieAuth = GetConfigBool("CookieAuth", authCfg);
var bearerTokenAuth = GetConfigBool("BearerTokenAuth", authCfg);
if (cookieAuth is true || bearerTokenAuth is true)
{
var cookieScheme = GetConfigStr("CookieAuthScheme", authCfg) ?? CookieAuthenticationDefaults.AuthenticationScheme;
var tokenScheme = GetConfigStr("BearerTokenAuthScheme", authCfg) ?? BearerTokenDefaults.AuthenticationScheme;
string defaultScheme = (cookieAuth, bearerTokenAuth) switch
{
(true, true) => string.Concat(cookieScheme, "_and_", tokenScheme),
(true, false) => cookieScheme,
(false, true) => tokenScheme,
_ => throw new NotImplementedException(),
};
var auth = Instance.Services.AddAuthentication(defaultScheme);
if (cookieAuth is true)
{
var days = GetConfigInt("CookieExpireDays", authCfg) ?? 14;
auth.AddCookie(cookieScheme, options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(days);
var name = GetConfigStr("CookieName", authCfg);
if (string.IsNullOrEmpty(name) is false)
{
options.Cookie.Name = GetConfigStr("CookieName", authCfg);
}
options.Cookie.Path = GetConfigStr("CookiePath", authCfg);
options.Cookie.Domain = GetConfigStr("CookieDomain", authCfg);
options.Cookie.MaxAge = GetConfigBool("CookieMultiSessions", authCfg) is true ? TimeSpan.FromDays(days) : null;
options.Cookie.HttpOnly = GetConfigBool("CookieHttpOnly", authCfg) is true;
});
Logger?.Information("Using Cookie Authentication with scheme {0}. Cookie expires in {1} days.", cookieScheme, days);
}
if (bearerTokenAuth is true)
{
var hours = GetConfigInt("BearerTokenExpireHours", authCfg) ?? 1;
var days = GetConfigInt("BearerRefreshTokenExpireDays", authCfg) ?? 14;
auth.AddBearerToken(tokenScheme, options =>
{
options.BearerTokenExpiration = TimeSpan.FromHours(hours);
options.RefreshTokenExpiration = TimeSpan.FromDays(days);
});
Logger?.Information("Using Bearer Token Authentication with scheme {0}. Token expires in {1} hours and refresh token expires in {2} days.", tokenScheme, hours, days);
}
if (cookieAuth is true && bearerTokenAuth is true)
{
auth.AddPolicyScheme(defaultScheme, defaultScheme, options =>
{
// runs on each request
options.ForwardDefaultSelector = context =>
{
// filter by auth type
string? authorization = context.Request.Headers[HeaderNames.Authorization];
if (string.IsNullOrEmpty(authorization) is false && authorization.StartsWith("Bearer "))
{
return tokenScheme;
}
// otherwise always check for cookie auth
return cookieScheme;
};
});
}
if (cookieAuth || bearerTokenAuth)
{
ExternalAuthConfig.Build(authCfg);
}
}
}
public static void BuildCors()
{
var corsCfg = Cfg.GetSection("Cors");
if (corsCfg.Exists() is false || GetConfigBool("Enabled", corsCfg) is false)
{
return;
}
var allowedOrigins = GetConfigEnumerable("AllowedOrigins", corsCfg)?.ToArray() ?? [];
var allowedMethods = GetConfigEnumerable("AllowedMethods", corsCfg)?.ToArray() ?? [];
var allowedHeaders = GetConfigEnumerable("AllowedHeaders", corsCfg)?.ToArray() ?? [];
Instance.Services.AddCors(options => options.AddDefaultPolicy(policy =>
{
if (allowedOrigins.Contains("*"))
{
policy.AllowAnyOrigin();
Logger?.Information("Allowed any origins.");
}
else
{
policy.WithOrigins(allowedOrigins);
Logger?.Information("Allowed origins: {0}", allowedOrigins);
}
if (allowedMethods.Contains("*"))
{
policy.AllowAnyMethod();
Logger?.Information("Allowed any methods.");
}
else
{
policy.WithMethods(allowedMethods);
Logger?.Information("Allowed methods: {0}", allowedMethods);
}
if (allowedHeaders.Contains("*"))
{
policy.AllowAnyHeader();
Logger?.Information("Allowed any headers.");
}
else
{
policy.WithHeaders(allowedHeaders);
Logger?.Information("Allowed headers: {0}", allowedHeaders);
}
policy.AllowCredentials();
}));
}
public static string? BuildConnectionString()
{
string? connectionString;
string? connectionName = GetConfigStr("ConnectionName", NpgsqlRestCfg);
if (connectionName is not null)
{
connectionString = Cfg.GetConnectionString(connectionName);
}
else
{
var section = Cfg.GetSection("ConnectionStrings");
connectionString = section.GetChildren().FirstOrDefault()?.Value;
}
if (string.IsNullOrEmpty(connectionString?.Trim()) is true)
{
Logger?.Fatal("ERROR: Connection string could not be initialized! Check your configuration and try again.");
return null;
}
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString);
if (GetConfigBool("SetApplicationNameInConnection", NpgsqlRestCfg) is true)
{
connectionStringBuilder.ApplicationName = Instance.Environment.ApplicationName;
}
connectionString = connectionStringBuilder.ConnectionString;
connectionStringBuilder.Remove("password");
Logger?.Information(messageTemplate: "Using connection: {0}", connectionStringBuilder.ConnectionString);
return connectionString;
}
public static Dictionary<string, StringValues> GetCustomHeaders()
{
var result = new Dictionary<string, StringValues>();
foreach(var section in NpgsqlRestCfg.GetSection("CustomRequestHeaders").GetChildren())
{
result.Add(section.Key, section.Value);
}
return result;
}
}