Skip to content

Commit

Permalink
Merge branch 'test'
Browse files Browse the repository at this point in the history
  • Loading branch information
Paahn committed Apr 15, 2024
2 parents 614261a + f9ed58d commit 32251f3
Show file tree
Hide file tree
Showing 131 changed files with 1,800 additions and 5,705 deletions.
2 changes: 1 addition & 1 deletion backend/pidp-backend.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "endorsement-reminder", "ser
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "endorsement-reminder.tests", "services.endorsement-reminder.tests\endorsement-reminder.tests.csproj", "{1251BD7F-30CB-4333-87D3-A96912F86361}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "update-opid", "tools.update-opid\update-opid.csproj", "{631ECF6D-6E61-49EF-933B-B9423A068032}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "do-work", "tools.do-work\do-work.csproj", "{631ECF6D-6E61-49EF-933B-B9423A068032}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions backend/tools.do-work/DoWorkService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace DoWork;

using Microsoft.EntityFrameworkCore;

using Pidp.Data;
using Pidp.Infrastructure.HttpClients.Keycloak;

public class DoWorkService : IDoWorkService
{
private readonly IKeycloakAdministrationClient keycloakClient;
private readonly PidpDbContext context;

public DoWorkService(IKeycloakAdministrationClient keycloakClient, PidpDbContext context)
{
this.keycloakClient = keycloakClient;
this.context = context;
}

public async Task DoWorkAsync()
{
var userId = await this.context.Parties
.Where(party => party.Id == 1001)
.Select(party => party.Credentials.First().UserId)
.SingleOrDefaultAsync();
await this.keycloakClient.GetUser(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
namespace UpdateOpId;
namespace DoWork;

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Diagnostics;

internal sealed class HostedServiceWrapper : IHostedService
{
private readonly IUpdateOpIdService service;
private readonly IDoWorkService service;
private readonly IHostApplicationLifetime appLifetime;
private readonly ILogger logger;
private int? exitCode;

public HostedServiceWrapper(
IUpdateOpIdService service,
IDoWorkService service,
IHostApplicationLifetime appLifetime,
ILogger<HostedServiceWrapper> logger)
{
Expand All @@ -24,9 +25,11 @@ public Task StartAsync(CancellationToken cancellationToken)
{
this.appLifetime.ApplicationStarted.Register(async () =>
{
var sw = Stopwatch.StartNew();
try
{
await this.service.UpdateOpIdAsync();
Console.WriteLine(">>>> Starting Work.");
await this.service.DoWorkAsync();
this.exitCode = 0;
}
catch (Exception ex)
Expand All @@ -36,6 +39,8 @@ public Task StartAsync(CancellationToken cancellationToken)
}
finally
{
sw.Stop();
Console.WriteLine($">>>> Work Stopped. Total Run Time: {(sw.ElapsedMilliseconds > 1000 ? sw.ElapsedMilliseconds / 1000 : sw.ElapsedMilliseconds + "m")}s.");
this.appLifetime.StopApplication();
}
});
Expand Down
9 changes: 9 additions & 0 deletions backend/tools.do-work/IDoWorkService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DoWork;

public interface IDoWorkService
{
/// <summary>
/// A service with DI that can be updated to perform manual tasks.
/// </summary>
public Task DoWorkAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@
using NodaTime;
using System.Reflection;

using UpdateOpId;
using DoWork;
using Pidp;
using Pidp.Data;
using Pidp.Infrastructure.HttpClients;
using Pidp.Infrastructure.HttpClients.Keycloak;


await Host.CreateDefaultBuilder(args)
.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
.ConfigureServices((hostContext, services) =>
{
var config = InitializeConfiguration(services);
services.AddHttpClient<IAccessTokenClient, AccessTokenClient>();
services.AddHttpClientWithBaseAddress<IKeycloakAdministrationClient, KeycloakAdministrationClient>(config.Keycloak.AdministrationUrl)
.WithBearerToken(new KeycloakAdministrationClientCredentials
{
Address = config.Keycloak.TokenUrl,
ClientId = config.Keycloak.AdministrationClientId,
ClientSecret = config.Keycloak.AdministrationClientSecret
});
services
.AddHttpClients(config)
.AddSingleton<IClock>(SystemClock.Instance)
.AddMediatR(opt => opt.RegisterServicesFromAssemblyContaining<Startup>())
.AddTransient<IUpdateOpIdService, UpdateOpIdService>()
.AddTransient<IDoWorkService, DoWorkService>()
.AddHostedService<HostedServiceWrapper>()
.AddDbContext<PidpDbContext>(options => options
.UseNpgsql(config.ConnectionStrings.PidpDatabase, npg => npg.UseNodaTime())
Expand Down
5 changes: 5 additions & 0 deletions backend/tools.do-work/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Do-Work Tool

This tool is intended as a standalone app to run ad-hoc scripts and migrations using the same Dependancy Injection as the main webapi project.
Add whatever additional services you may need to the DI in `Program.cs`, and update `appsettings.json` with the needed environment variables/screts.
Be sure not to commit any secrets when doing so.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>UpdateOpId</RootNamespace>
<RootNamespace>DoWork</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>5c2dc965-00b4-4531-9ff0-9b37193ead9b</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\webapi\pidp.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Nanoid" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
12 changes: 0 additions & 12 deletions backend/tools.update-opid/IUpdateOpIdService.cs

This file was deleted.

44 changes: 0 additions & 44 deletions backend/tools.update-opid/UpdateOpIdService.cs

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 32251f3

Please sign in to comment.