-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add demo user with sample data for demo server
- Loading branch information
1 parent
ddb39f2
commit 13ca7c3
Showing
23 changed files
with
821 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
source/Gnomeshade.WebApi/Configuration/StartupFilters/DemoUserBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2021 Valters Melnalksnis | ||
// Licensed under the GNU Affero General Public License v3.0 or later. | ||
// See LICENSE.txt file in the project root for full license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Gnomeshade.Data.Repositories; | ||
using Gnomeshade.WebApi.Client; | ||
using Gnomeshade.WebApi.Services; | ||
|
||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
using NodaTime; | ||
|
||
namespace Gnomeshade.WebApi.Configuration.StartupFilters; | ||
|
||
internal sealed class DemoUserBackgroundService : BackgroundService | ||
{ | ||
private readonly IHostApplicationLifetime _lifetime; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly Lazy<HttpMessageHandler> _handler; | ||
|
||
public DemoUserBackgroundService(IHostApplicationLifetime lifetime, IServiceProvider serviceProvider, Lazy<HttpMessageHandler>? handler = null) | ||
{ | ||
_handler = handler ?? new(() => new SocketsHttpHandler()); | ||
_lifetime = lifetime; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
if (!await WaitForAppStartup(stoppingToken)) | ||
{ | ||
return; | ||
} | ||
|
||
using var scope = _serviceProvider.CreateScope(); | ||
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); | ||
if (configuration.GetChildren().Any(section => section.Key is "GNOMESHADE_DEMO")) | ||
{ | ||
await CreateDemoUser(scope.ServiceProvider); | ||
} | ||
} | ||
|
||
private async Task<bool> WaitForAppStartup(CancellationToken stoppingToken) | ||
{ | ||
var startedSource = new TaskCompletionSource(); | ||
var cancelledSource = new TaskCompletionSource(); | ||
|
||
await using var reg1 = _lifetime.ApplicationStarted.Register(() => startedSource.SetResult()); | ||
await using var reg2 = stoppingToken.Register(() => cancelledSource.SetResult()); | ||
|
||
var completedTask = await Task.WhenAny(startedSource.Task, cancelledSource.Task); | ||
|
||
// If the completed tasks was the "app started" task, return true, otherwise false | ||
return completedTask == startedSource.Task; | ||
} | ||
|
||
private async Task CreateDemoUser(IServiceProvider services) | ||
{ | ||
var repository = services.GetRequiredService<CounterpartyRepository>(); | ||
var counterparties = await repository.GetAllAsync(); | ||
|
||
if (counterparties.SingleOrDefault(counterparty => counterparty.NormalizedName is "DEMO") is not null) | ||
{ | ||
return; | ||
} | ||
|
||
var registrationService = services.GetRequiredService<UserRegistrationService>(); | ||
|
||
var registerResult = await registrationService.RegisterUser("demo", "Demo", "Demo1!"); | ||
if (!registerResult.Succeeded) | ||
{ | ||
throw new("Failed to register demo user"); | ||
} | ||
|
||
// The default address is not provided by the feature, so need to set it manually | ||
var serverAddress = services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>()?.Addresses.FirstOrDefault() ?? "http://localhost:5000/"; | ||
var baseAddress = new UriBuilder(serverAddress) { Host = "localhost", Path = "/api/" }.Uri; | ||
|
||
var clock = services.GetRequiredService<IClock>(); | ||
var dateTimeZoneProvider = services.GetRequiredService<IDateTimeZoneProvider>(); | ||
|
||
// todo This probably needs to be reworked by not using an HttpClient to call the API from the API | ||
var client = new GnomeshadeClient( | ||
new(new TokenDelegatingHandler(new(clock), new(dateTimeZoneProvider), new NullOidcClient()) | ||
{ | ||
InnerHandler = _handler.Value, | ||
}) { BaseAddress = baseAddress }, | ||
new(dateTimeZoneProvider)); | ||
|
||
var loginResult = await client.LogInAsync(new() { Username = "demo", Password = "Demo1!" }); | ||
if (loginResult is FailedLogin) | ||
{ | ||
throw new("Failed to log in with demo user"); | ||
Check warning on line 104 in source/Gnomeshade.WebApi/Configuration/StartupFilters/DemoUserBackgroundService.cs
|
||
} | ||
|
||
var service = new DemoUserService(client, clock, dateTimeZoneProvider); | ||
await service.GenerateData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"Gnomeshade.WebApi": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"dotnetRunMessages": "true" | ||
} | ||
} | ||
"profiles": { | ||
"Gnomeshade.WebApi": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"GNOMESHADE_DEMO": "true" | ||
}, | ||
"dotnetRunMessages": "true" | ||
} | ||
} | ||
} |
Oops, something went wrong.