8000 IUtilService into abstractions ; AppConfig object for easy configurat… · devel0/example-webapp-with-auth@baf5f4a · GitHub
[go: up one dir, main page]

Skip to content

Commit baf5f4a

Browse files
committed
IUtilService into abstractions ; AppConfig object for easy configuration access ; AppConfig.SetValue to change configuration at runtime
1 parent 12e3085 commit baf5f4a

29 files changed

+533
-251
lines changed

readme.md

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
| misc | misc scripts ( restore scripts exec permissions, db dia gen script ) |
110110
| src | sources |
111111
| src/backend | c# backend |
112+
| src/backend/abstractions | c# backend abstraction services |
112113
| src/backend/db-context | c# database context |
113114
| src/backend/db-migrations-psql | c# ef core psql migrations |
114115
| src/backend/test | c# backend integration test |
@@ -155,19 +156,55 @@ DB_CONN_STRING="Host=localhost; Database=ExampleWebApp; Username=example_webapp_
155156
JWTKEY="$(openssl rand -hex 32)"
156157
```
157158

159+
optionally set unit test conn string
160+
161+
```sh
162+
UNIT_TEST_DB_CONN_STRING="Host=localhost; Database=ExampleWebAppTest; Username=example_webapp_user; Password=$(cat ~/security/devel/ExampleWebApp/postgres-user)"
163+
```
164+
158165
- set development user secrets
159166

160167
```sh
161168
cd src/backend/webapi
162169
dotnet user-secrets init
163-
dotnet user-secrets set "SeedUsers:Admin:Email" "$SEED_ADMIN_EMAIL"
164-
dotnet user-secrets set "SeedUsers:Admin:Password" "$SEED_ADMIN_PASS"
165-
dotnet user-secrets set "DbProvider" "$DB_PROVIDER"
166-
dotnet user-secrets set "ConnectionStrings:Main" "$DB_CONN_STRING"
167-
dotnet user-secrets set "JwtSettings:Key" "$JWTKEY"
170+
171+
dotnet user-secrets set "AppConfig:Auth:Jwt:Key" "$JWTKEY"
172+
173+
dotnet user-secrets set "AppConfig:Database:Seed:Users:0:Username" "admin"
174+
dotnet user-secrets set "AppConfig:Database:Seed:Users:0:Email" "$SEED_ADMIN_EMAIL"
175+
dotnet user-secrets set "AppConfig:Database:Seed:Users:0:Password" "$SEED_ADMIN_PASS"
176+
dotnet user-secrets set "AppConfig:Database:Seed:Users:0:Roles:0" "admin"
177+
178+
dotnet user-secrets set "AppConfig:Database:ConnectionName" "Development"
179+
dotnet user-secrets set "AppConfig:Database:Connections:0:Name" "Development"
180+
dotnet user-secrets set "AppConfig:Database:Connections:0:ConnectionString" "$DB_CONN_STRING"
181+
dotnet user-secrets set "AppConfig:Database:Connections:0:Provider" "$DB_PROVIDER"
182+
183+
# optionally configure also unit test db
184+
dotnet user-secrets set "AppConfig:Database:Connections:1:Name" "UnitTest"
185+
dotnet user-secrets set "AppConfig:Database:Connections:1:ConnectionString" "$UNIT_TEST_DB_CONN_STRING"
186+
dotnet user-secrets set "AppConfig:Database:Connections:1:Provider" "$DB_PROVIDER"
187+
168188
cd ..
169189
```
170190

191+
- example result of `dotnet user-secrets list`
192+
193+
```sh
194+
AppConfig:Database:Seed:Users:0:Username = admin
195+
AppConfig:Database:Seed:Users:0:Roles:0 = admin
196+
AppConfig:Database:Seed:Users:0:Password = ADMINPASS
197+
AppConfig:Database:Seed:Users:0:Email = ADMIN@EMAIL.COM
198+
AppConfig:Database:Connections:1:Provider = Postgres
199+
AppConfig:Database:Connections:1:Name = UnitTest
200+
AppConfig:Database:Connections:1:ConnectionString = Host=localhost; Database=ExampleWebAppTest; Username=example_webapp_user; Password=DBPASS
201+
AppConfig:Database:Connections:0:Provider = Postgres
202+
AppConfig:Database:Connections:0:Name = Development
203+
AppConfig:Database:Connections:0:ConnectionString = Host=localhost; Database=ExampleWebApp; Username=example_webapp_user; Password=DBPASS
204+
AppConfig:Database:ConnectionName = Development
205+
AppConfig:Auth:Jwt:Key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
206+
```
207+
171208
### configuration parameters for mail server
172209

173210
- to be able to use the reset password feature configure also the smtp server
@@ -396,7 +433,7 @@ dotnet user-secrets set "ConnectionStrings:UnitTest" "$TEST_DB_CONN_STRING"
396433
cd ../../..
397434
```
398435

399-
- to run tests
436+
- to run tests ( requires about 1 min to complete )
400437

401438
```sh
402439
dotnet test
@@ -410,29 +447,30 @@ dotnet test --filter=TEST
410447

411448
#### configuration parameters
412449

413-
| param name | description | example |
414-
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
415-
| AppServerName | Used to build [app url][10] for the reset password link. | "dev-webapp-test.searchathing.local" |
416-
| DbProvider | Used to [inject db provider service][12]. | "Postgres" |
417-
| ConnectionStrings:Main | Used to build application [db context datasource][11]. | "Host=localhost; Database=ExampleWebApp; Username=postgres; Password=somepass" |
418-
| IsUnitTest | Used to build [unit test application datasource][11] in unit test mode. Will be set to `true` from the [test factory][13]. | false |
419-
| ConnectionStrings:UnitTest | Need to be set in order to run unit tests. Warning: database referred by this conn string will be dropped during tests. | "Host=localhost; Database=ExampleWebAppTest; Username=postgres; Password=somepass" |
420-
| JwtSettings:Key | Symmetric key for JWT signature generation. | (results from `openssl rand -hex 32` command) |
421-
| JwtSettings:Issuer | Issuer of the JWT access token. | "https://www.example.com" |
422-
| JwtSettings:Audience | Audience of the JWT access token. | "https://www.example.com/app" |
423-
| JwtSettings:AccessTokenDurationSeconds | JWT access token duration (seconds) | 300 |
424-
| JwtSettings:RefreshTokenDurationSeconds | JWT refresh token duration (seconds) | 1200 |
425-
| JwtSettings:ClockSkewSeconds | JWT access token clock skew (seconds) | 0 |
426-
| SeedUsers:Admin:UserName | Default seeded admin username | admin |
427-
| SeedUsers:Admin:Password | Default seeded admin password | SomePass1! |
428-
| SeedUsers:Admin:Email | Default seeded admin email | admin@some.com |
429-
| EmailServer:Username | Email server config used in reset password ( account username ) | server@some.com |
430-
| EmailServer:Password | Email server config used in reset password ( account password ) | |
431-
| EmailServer:SmtpServerName | Email server config used in reset password ( account smtp server ) | smtp@some.com |
432-
| EmailServer:SmtpServerPort | Email server config used in reset password ( account smtp port ) | 587 |
433-
| EmailServer:Security | Email server config used in reset password ( account protocol security ) | "Tls" |
434-
| EmailServer:FromDisplayName | Email server config used in reset password ( account displayname of the sender ) | "Server" |
435-
| DbSchemaSnakeCase | if true generates db schema with snake case mode ( useful for Postgres ) | |
450+
| param name | description | example |
451+
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
452+
| Server:HostName | Used to build [app url][10] for the reset password link. | "dev-webapp-test.searchathing.local" |
453+
| Database:SchemaSnakeCase | if true generates db schema with snake case mode ( useful for Postgres ) | |
454+
| Database:ConnectionName | Connection to user. | "Development" |
455+
| Database:Connections:IDX:Provider | Used to [inject db provider service][12]. | "Postgres" |
456+
| Database:Connections:IDX:ConnectionString | Used to build application [db context datasource][11]. | "Host=localhost; Database=ExampleWebApp; Username=postgres; Password=somepass" |
457+
| Database:Seed:Users:IDX:UserName | Default seeded user username | admin |
458+
| Database:Seed:Users:IDX:Password | Default seeded user password | SomePass1! |
459+
| Database:Seed:Users:IDX:Email | Default seeded user email | admin@some.com |
460+
| Database:Seed:Users:IDX:Roles | Default seeded user roles | admin |
461+
| Auth:JwtSettings:Key | Symmetric key for JWT signature generation. | (results from `openssl rand -hex 32` command) |
462+
| Auth:JwtSettings:Issuer | Issuer of the JWT access token. | "https://www.example.com" |
463+
| Auth:JwtSettings:Audience | Audience of the JWT access token. | "https://www.example.com/app" |
464+
| Auth:JwtSettings:AccessTokenDuration | JWT access token duration (TimeSpan) | "00:00:30" |
465+
| Auth:JwtSettings:RefreshTokenDuration | JWT refresh token duration (TimeSpan) | "7.00:00:00" |
466+
| Auth:JwtSettings:ClockSkew | JWT access token clock skew (TimeSpan) | "00:00:00" |
467+
| EmailServer:Username | Email server config used in reset password ( account username ) | server@some.com |
468+
| EmailServer:Password | Email server config used in reset password ( account password ) | |
469+
| EmailServer:SmtpServer | Email server config used in reset password ( account smtp server ) | smtp@some.com |
470+
| EmailServer:SmtpServerPort | Email server config used in reset password ( account smtp port ) | 587 |
471+
| EmailServer:Security | Email server config used in reset password ( account protocol security ) | "Tls" |
472+
| EmailServer:FromDisplayName | Email server config used in reset password ( account displayname of the sender ) | "Server" |
473+
| IsUnitTest | Used to build [unit test application datasource][11] in unit test mode. Will be set to `true` from the [test factory][13]. | false |
436474

437475
The configuration is setup through [SetupAppSettings][14] method in order to evaluate:
438476
- `appsettings.json`
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExampleWebApp.Backend.WebApi;
1+
namespace ExampleWebApp.Backend.Abstractions;
22

33
public interface IUtilService
44
{
@@ -7,4 +7,9 @@ public interface IUtilService
77

88
JsonSerializerOptions JavaSerializerSettings { get; }
99

10+
/// <summary>
11+
/// retrieve <see cref="Types.AppConfig"/> object from configuration
12+
/// </summary>
13+
AppConfig AppConfig();
14+
1015
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ExampleWebApp.Backend.Abstractions;
2+
3+
public static partial class Constants
4+
{
5+
6+
public const string CONFIG_KEY_AppConfig = "AppConfig";
7+
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace ExampleWebApp.Backend.Abstractions;
4+
5+
public class ReflectionHelper<T>
6+
{
7+
public static string GetPath<TProperty>(Expression<Func<T, TProperty>> expr)
8+
{
9+
var name = expr.Parameters[0].Name;
10+
11+
return expr.Body.ToString().Replace($"{name} => {name}", typeof(T).Name);
12+
}
13+
}
14+
15+
public static class Extensions
16+
{
17+
18+
/// <summary>
19+
/// retrieve <see cref="Types.AppConfig"/> from configuration
20+
/// </summary>
21+
public static AppConfig AppConfig(this IConfiguration configuration)
22+
{
23+
var appConfig = configuration.GetSection(CONFIG_KEY_AppConfig).Get<AppConfig>();
24+
25+
if (appConfig is null)
26+
throw new Exception($"Can't load {nameof(Types.AppConfig)} object from appsettings.json");
27+
28+
return appConfig;
29+
}
30+
31+
/// <summary>
32+
/// change a value in <see cref="Types.AppConfig"/> reflecting to the configuration section
33+
/// </summary>
34+
public static void SetValue<P, T>(this AppConfig appConfig,
35+
IConfiguration configuration, Expression<Func<AppConfig, P>> path, T value) =>
36+
Types.AppConfig.SetValue(configuration, path, value);
37+
38+
}

0 commit comments

Comments
 (0)
0