@@ -23,7 +23,7 @@ public static class Builder
2323 public static void BuildInstance ( )
2424 {
2525 var staticFilesCfg = Cfg . GetSection ( "StaticFiles" ) ;
26- string ? webRootPath = staticFilesCfg is not null && GetConfigBool ( "Enabled" , staticFilesCfg ) is true ? GetConfigStr ( "RootPath" , staticFilesCfg ) : null ;
26+ string ? webRootPath = staticFilesCfg is not null && GetConfigBool ( "Enabled" , staticFilesCfg ) is true ? GetConfigStr ( "RootPath" , staticFilesCfg ) ?? "wwwroot" : null ;
2727
2828 var options = new WebApplicationOptions ( )
2929 {
@@ -45,13 +45,25 @@ public static void BuildInstance()
4545 {
4646 Instance . WebHost . UseUrls ( urls . Split ( ';' ) ) ;
4747 }
48+ else
49+ {
50+ Instance . WebHost . UseUrls ( "http://localhost:5000" , "http://localhost:5001" ) ;
51+ }
4852
4953 var ssqlCfg = Cfg . GetSection ( "Ssl" ) ;
50- if ( ssqlCfg . Exists ( ) is true && GetConfigBool ( "Enabled" , ssqlCfg ) is true )
54+ if ( ssqlCfg . Exists ( ) is true )
55+ {
56+ if ( GetConfigBool ( "Enabled" , ssqlCfg ) is true )
57+ {
58+ Instance . WebHost . UseKestrelHttpsConfiguration ( ) ;
59+ UseHttpsRedirection = GetConfigBool ( "UseHttpsRedirection" , ssqlCfg ) ;
60+ UseHsts = GetConfigBool ( "UseHsts" , ssqlCfg ) ;
61+ }
62+ }
63+ else
5164 {
52- Instance . WebHost . UseKestrelHttpsConfiguration ( ) ;
53- UseHttpsRedirection = GetConfigBool ( "UseHttpsRedirection" , ssqlCfg ) ;
54- UseHsts = GetConfigBool ( "UseHsts" , ssqlCfg ) ;
65+ UseHttpsRedirection = true ;
66+ UseHsts = true ;
5567 }
5668 }
5769
@@ -60,31 +72,34 @@ public static void BuildInstance()
6072 public static void BuildLogger ( )
6173 {
6274 var logCfg = Cfg . GetSection ( "Log" ) ;
63- if ( logCfg . Exists ( ) is false )
64- {
65- LogToConsole = false ;
66- LogToFile = false ;
67- return ;
68- }
69-
7075 Logger = null ;
71- LogToConsole = GetConfigBool ( "ToConsole" , logCfg ) ;
76+ LogToConsole = GetConfigBool ( "ToConsole" , logCfg , true ) ;
7277 LogToFile = GetConfigBool ( "ToFile" , logCfg ) ;
73- var filePath = GetConfigStr ( "FilePath" , logCfg ) ;
78+ var filePath = GetConfigStr ( "FilePath" , logCfg ) ?? "logs/log.txt" ;
7479
7580 if ( LogToConsole is true || LogToFile is true )
7681 {
7782 var loggerConfig = new LoggerConfiguration ( ) . MinimumLevel . Verbose ( ) ;
78- foreach ( var level in logCfg . GetSection ( "MinimalLevels" ) . GetChildren ( ) )
83+ var levels = logCfg ? . GetSection ( "MinimalLevels" ) ? . GetChildren ( ) ? . ToList ( ) ;
84+ if ( levels is null )
85+ {
86+ loggerConfig . MinimumLevel . Override ( "System" , Serilog . Events . LogEventLevel . Warning ) ;
87+ loggerConfig . MinimumLevel . Override ( "Microsoft" , Serilog . Events . LogEventLevel . Warning ) ;
88+ }
89+ else
7990 {
80- var key = level . Key ;
81- var value = GetEnum < Serilog . Events . LogEventLevel ? > ( level . Value ) ;
82- if ( value is not null && key is not null )
91+ foreach ( var level in logCfg ? . GetSection ( "MinimalLevels" ) ? . GetChildren ( ) ?? [ ] )
8392 {
84- loggerConfig . MinimumLevel . Override ( key , value . Value ) ;
93+ var key = level . Key ;
94+ var value = GetEnum < Serilog . Events . LogEventLevel ? > ( level . Value ) ;
95+ if ( value is not null && key is not null )
96+ {
97+ loggerConfig . MinimumLevel . Override ( key , value . Value ) ;
98+ }
8599 }
86100 }
87- string outputTemplate = GetConfigStr ( "OutputTemplate" , logCfg ) ?? "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}" ;
101+ string outputTemplate = GetConfigStr ( "OutputTemplate" , logCfg ) ??
102+ "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}" ;
88103 if ( LogToConsole is true )
89104 {
90105 loggerConfig = loggerConfig . WriteTo . Console (
@@ -112,12 +127,15 @@ public static void BuildLogger()
112127 public static void BuildAuthentication ( )
113128 {
114129 var authCfg = Cfg . GetSection ( "Auth" ) ;
115- if ( authCfg . Exists ( ) is false )
130+ bool cookieAuth = false ;
131+ bool bearerTokenAuth = false ;
132+ if ( authCfg . Exists ( ) is true )
116133 {
117- return ;
134+ cookieAuth = GetConfigBool ( "CookieAuth" , authCfg ) ;
135+ bearerTokenAuth = GetConfigBool ( "BearerTokenAuth" , authCfg ) ;
136+
118137 }
119- var cookieAuth = GetConfigBool ( "CookieAuth" , authCfg ) ;
120- var bearerTokenAuth = GetConfigBool ( "BearerTokenAuth" , authCfg ) ;
138+
121139 if ( cookieAuth is true || bearerTokenAuth is true )
122140 {
123141 var cookieScheme = GetConfigStr ( "CookieAuthScheme" , authCfg ) ?? CookieAuthenticationDefaults . AuthenticationScheme ;
@@ -144,8 +162,8 @@ public static void BuildAuthentication()
144162 }
145163 options . Cookie . Path = GetConfigStr ( "CookiePath" , authCfg ) ;
146164 options . Cookie . Domain = GetConfigStr ( "CookieDomain" , authCfg ) ;
147- options . Cookie . MaxAge = GetConfigBool ( "CookieMultiSessions" , authCfg ) is true ? TimeSpan . FromDays ( days ) : null ;
148- options . Cookie . HttpOnly = GetConfigBool ( "CookieHttpOnly" , authCfg ) is true ;
165+ options . Cookie . MaxAge = GetConfigBool ( "CookieMultiSessions" , authCfg , true ) is true ? TimeSpan . FromDays ( days ) : null ;
166+ options . Cookie . HttpOnly = GetConfigBool ( "CookieHttpOnly" , authCfg , true ) is true ;
149167 } ) ;
150168 Logger ? . Information ( "Using Cookie Authentication with scheme {0}. Cookie expires in {1} days." , cookieScheme , days ) ;
151169 }
@@ -194,9 +212,9 @@ public static void BuildCors()
194212 return ;
195213 }
196214
197- var allowedOrigins = GetConfigEnumerable ( "AllowedOrigins" , corsCfg ) ? . ToArray ( ) ?? [ ] ;
198- var allowedMethods = GetConfigEnumerable ( "AllowedMethods" , corsCfg ) ? . ToArray ( ) ?? [ ] ;
199- var allowedHeaders = GetConfigEnumerable ( "AllowedHeaders" , corsCfg ) ? . ToArray ( ) ?? [ ] ;
215+ var allowedOrigins = GetConfigEnumerable ( "AllowedOrigins" , corsCfg ) ? . ToArray ( ) ?? [ "*" ] ;
216+ var allowedMethods = GetConfigEnumerable ( "AllowedMethods" , corsCfg ) ? . ToArray ( ) ?? [ "*" ] ;
217+ var allowedHeaders = GetConfigEnumerable ( "AllowedHeaders" , corsCfg ) ? . ToArray ( ) ?? [ "*" ] ;
200218
201219 Instance . Services . AddCors ( options => options . AddDefaultPolicy ( policy =>
202220 {
@@ -252,7 +270,7 @@ public static void BuildCors()
252270 }
253271
254272 var connectionStringBuilder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
255- if ( GetConfigBool ( "SetApplicationNameInConnection" , ConnectionSettingsCfg ) is true )
273+ if ( GetConfigBool ( "SetApplicationNameInConnection" , ConnectionSettingsCfg , true ) is true )
256274 {
257275 connectionStringBuilder . ApplicationName = Instance . Environment . ApplicationName ;
258276 }
@@ -261,39 +279,39 @@ public static void BuildCors()
261279 {
262280 var whenMissing = GetConfigBool ( "UseEnvironmentConnectionWhenMissing" , ConnectionSettingsCfg ) ;
263281
264- var hostEnvVar = GetConfigStr ( "HostEnvVar" , ConnectionSettingsCfg ) ;
282+ var hostEnvVar = GetConfigStr ( "HostEnvVar" , ConnectionSettingsCfg ) ?? "PGHOST" ;
265283 if ( string . IsNullOrEmpty ( hostEnvVar ) is false && string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( hostEnvVar ) ) is false )
266284 {
267285 if ( whenMissing is true || string . IsNullOrEmpty ( connectionStringBuilder . Host ) )
268286 {
269287 connectionStringBuilder . Host = Environment . GetEnvironmentVariable ( hostEnvVar ) ;
270288 }
271289 }
272- var portEnvVar = GetConfigStr ( "PortEnvVar" , ConnectionSettingsCfg ) ;
290+ var portEnvVar = GetConfigStr ( "PortEnvVar" , ConnectionSettingsCfg ) ?? "PGPORT" ;
273291 if ( string . IsNullOrEmpty ( portEnvVar ) is false && int . TryParse ( Environment . GetEnvironmentVariable ( portEnvVar ) , out int port ) is true )
274292 {
275293 if ( whenMissing is true || connectionStringBuilder . Port != port )
276294 {
277295 connectionStringBuilder . Port = port ;
278296 }
279297 }
280- var dbEnvVar = GetConfigStr ( "DatabaseEnvVar" , ConnectionSettingsCfg ) ;
298+ var dbEnvVar = GetConfigStr ( "DatabaseEnvVar" , ConnectionSettingsCfg ) ?? "PGDATABASE" ;
281299 if ( string . IsNullOrEmpty ( dbEnvVar ) is false && string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( dbEnvVar ) ) is false )
282300 {
283301 if ( whenMissing is true || string . IsNullOrEmpty ( connectionStringBuilder . Database ) )
284302 {
285303 connectionStringBuilder . Database = Environment . GetEnvironmentVariable ( dbEnvVar ) ;
286304 }
287305 }
288- var userEnvVar = GetConfigStr ( "UserEnvVar" , ConnectionSettingsCfg ) ;
306+ var userEnvVar = GetConfigStr ( "UserEnvVar" , ConnectionSettingsCfg ) ?? "PGUSER" ;
289307 if ( string . IsNullOrEmpty ( userEnvVar ) is false && string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( userEnvVar ) ) is false )
290308 {
291309 if ( whenMissing is true || string . IsNullOrEmpty ( connectionStringBuilder . Username ) )
292310 {
293311 connectionStringBuilder . Username = Environment . GetEnvironmentVariable ( userEnvVar ) ;
294312 }
295313 }
296- var passEnvVar = GetConfigStr ( "PasswordEnvVar" , ConnectionSettingsCfg ) ;
314+ var passEnvVar = GetConfigStr ( "PasswordEnvVar" , ConnectionSettingsCfg ) ?? "PGPASSWORD" ;
297315 if ( string . IsNullOrEmpty ( passEnvVar ) is false && string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( passEnvVar ) ) is false )
298316 {
299317 if ( whenMissing is true || string . IsNullOrEmpty ( connectionStringBuilder . Password ) )
0 commit comments