Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use redis in web api #527

Merged
merged 22 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docker-compose-no-webapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ services:
interval: 5s
timeout: 20s
retries: 5


dialogporten-redis:
image: redis:7.0-alpine
restart: always
ports:
- "6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5

dialogporten-service:
build:
context: .
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ services:
depends_on:
dialogporten-postgres:
condition: service_healthy

dialogporten-redis:
condition: service_healthy
environment:
- Infrastructure:Redis:ConnectionString=dialogporten-redis:6379
- Infrastructure:DialogDbConnectionString=${DB_CONNECTION_STRING}
- Serilog__WriteTo__0__Name=Console
- Serilog__MinimumLevel__Default=Debug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Altinn.ApiClients.Maskinporten" Version="9.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.2" />
<PackageReference Include="Altinn.Authorization.ABAC" Version="0.0.8" />
<PackageReference Include="Bogus" Version="35.4.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
Expand All @@ -24,13 +25,15 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Digdir.Domain.Dialogporten.Application\Digdir.Domain.Dialogporten.Application.csproj" />
<ProjectReference Include="..\Digdir.Library.Entity.EntityFrameworkCore\Digdir.Library.Entity.EntityFrameworkCore.csproj" />
<ProjectReference
Include="..\Digdir.Domain.Dialogporten.Application\Digdir.Domain.Dialogporten.Application.csproj" />
<ProjectReference
Include="..\Digdir.Library.Entity.EntityFrameworkCore\Digdir.Library.Entity.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Digdir.Domain.Dialogporten.Application.Integration.Tests" />
<InternalsVisibleTo Include="Digdir.Domain.Dialogporten.Infrastructure.Unit.Tests" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Digdir.Domain.Dialogporten.Infrastructure.Altinn.Events;
using Digdir.Domain.Dialogporten.Infrastructure.Altinn.OrganizationRegistry;
using Digdir.Domain.Dialogporten.Infrastructure.Altinn.ResourceRegistry;
using StackExchange.Redis;

namespace Digdir.Domain.Dialogporten.Infrastructure;

Expand All @@ -51,11 +52,31 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
.ValidateOnStart();

var thisAssembly = Assembly.GetExecutingAssembly();

services
// Framework
.AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true)
.AddDistributedMemoryCache()
.AddDbContext<DialogDbContext>((services, options) =>
.AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true);

var infrastructureSettings = infrastructureConfigurationSection.Get<InfrastructureSettings>()
?? throw new InvalidOperationException("Failed to get Redis settings. Infrastructure settings must not be null.");

if (infrastructureSettings.Redis.Enabled == true)
{
services.AddStackExchangeRedisCache(options =>
{
var infrastructureSettings = infrastructureConfigurationSection.Get<InfrastructureSettings>()
?? throw new InvalidOperationException("Failed to get Redis connection string. Infrastructure settings must not be null.");
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
var connectionString = infrastructureSettings.Redis.ConnectionString;
options.Configuration = connectionString;
options.InstanceName = "Redis";
});
}
else
{
services.AddDistributedMemoryCache();
}

services.AddDbContext<DialogDbContext>((services, options) =>
{
var connectionString = services.GetRequiredService<IOptions<InfrastructureSettings>>()
.Value.DialogDbConnectionString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed class InfrastructureSettings
public const string ConfigurationSectionName = "Infrastructure";

public required string DialogDbConnectionString { get; init; }
public required RedisSettings Redis { get; init; }
public required AltinnPlatformSettings Altinn { get; init; }
public required AltinnCdnPlatformSettings AltinnCdn { get; init; }
public required MaskinportenSettings Maskinporten { get; init; }
Expand All @@ -26,12 +27,19 @@ public sealed class AltinnCdnPlatformSettings
public required Uri BaseUri { get; init; }
}

public sealed class RedisSettings
{
public required bool? Enabled { get; init; }
public required string ConnectionString { get; init; }
}

internal sealed class InfrastructureSettingsValidator : AbstractValidator<InfrastructureSettings>
{
public InfrastructureSettingsValidator(
IValidator<AltinnPlatformSettings> altinnPlatformSettingsValidator,
IValidator<AltinnCdnPlatformSettings> altinnCdnPlatformSettingsValidator,
IValidator<MaskinportenSettings> maskinportenSettingsValidator)
IValidator<MaskinportenSettings> maskinportenSettingsValidator,
IValidator<RedisSettings> redisSettingsValidator)
{
RuleFor(x => x.DialogDbConnectionString)
.NotEmpty();
Expand All @@ -47,6 +55,10 @@ public InfrastructureSettingsValidator(
RuleFor(x => x.Maskinporten)
.NotEmpty()
.SetValidator(maskinportenSettingsValidator);

RuleFor(x => x.Redis)
.NotEmpty()
.SetValidator(redisSettingsValidator);
}
}

Expand Down Expand Up @@ -76,3 +88,12 @@ public MaskinportenSettingsValidator()
RuleFor(x => x.EncodedJwk).NotEmpty();
}
}

internal sealed class RedisSettingsValidator : AbstractValidator<RedisSettings>
{
public RedisSettingsValidator()
{
RuleFor(x => x.Enabled).Must(x => x is false or true);
RuleFor(x => x.ConnectionString).NotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.4"/>
<PackageReference Include="FastEndpoints.Swagger" Version="5.22.0"/>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="7.1.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1"/>
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
<PackageReference Include="Azure.Identity" Version="1.10.4" />
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="FastEndpoints.Swagger" Version="5.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="7.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Digdir.Domain.Dialogporten.Application\Digdir.Domain.Dialogporten.Application.csproj"/>
<ProjectReference Include="..\Digdir.Domain.Dialogporten.Infrastructure\Digdir.Domain.Dialogporten.Infrastructure.csproj"/>
<ProjectReference
Include="..\Digdir.Domain.Dialogporten.Application\Digdir.Domain.Dialogporten.Application.csproj" />
<ProjectReference
Include="..\Digdir.Domain.Dialogporten.Infrastructure\Digdir.Domain.Dialogporten.Infrastructure.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
}
},
"Infrastructure": {
"Redis": {
"Enabled": true,
"ConnectionString": "localhost:6379"
},
"DialogDbConnectionString": "TODO: Add to local secrets",
// Settings from appsettings.json, environment variables or other configuration providers.
// The first three are always mandatory for all client definitions types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
}
},
"Infrastructure": {
"Redis":{
"Enabled": true,
"ConnectionString": "TODO: Add to local secrets"
},
"DialogDbConnectionString": "TODO: Add to local secrets",
// Settings from appsettings.json, environment variables or other configuration providers.
// The first three are always mandatory for all client definitions types
Expand Down
4 changes: 4 additions & 0 deletions src/Digdir.Domain.Dialogporten.WebApi/appsettings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
}
},
"Infrastructure": {
"Redis":{
"Enabled": true,
"ConnectionString": "TODO: Add to local secrets"
},
"DialogDbConnectionString": "TODO: Add to local secrets",
// Settings from appsettings.json, environment variables or other configuration providers.
// The first three are always mandatory for all client definitions types
Expand Down