From 8f98b6d11351c4f7b721c7f4ed86a8145b20786b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20A=2EP?= <53834183+Jossec101@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:08:08 +0100 Subject: [PATCH] feat(justfile): add context to drop-db command to specify ApplicationDbContext for clarity (#360) feat(Dockerfile, NodeGuard.csproj): upgrade .NET version from 7.0 to 8.0 for better performance and features feat(NodeGuard.csproj): update package versions to their latest stable versions for improved functionality and security feat(Program.cs): refactor code for better readability and maintainability feat(Program.cs): add NpgsqlDataSourceBuilder to enable dynamic JSON in PostgreSQL fix(NodeChannelSubscribeJobTests.cs): change Assert.ThrowsAsync to Assert.ThrowsAnyAsync to catch any type of exceptions feat(NodeGuard.Tests.csproj): upgrade project to .NET 8.0 for improved performance and features chore(NodeGuard.Tests.csproj): update all package references to their latest versions for better stability and new features --- justfile | 2 +- src/Dockerfile | 4 +- src/NodeGuard.csproj | 38 +++++++++---------- src/Program.cs | 36 ++++++++++++------ .../Jobs/NodeChannelSubscribeJobTests.cs | 2 +- test/NodeGuard.Tests/NodeGuard.Tests.csproj | 22 +++++------ 6 files changed, 59 insertions(+), 45 deletions(-) diff --git a/justfile b/justfile index d911a03f..6a3bedd3 100644 --- a/justfile +++ b/justfile @@ -1,7 +1,7 @@ set fallback := true drop-db: - cd src && dotnet ef database drop -f + cd src && dotnet ef database drop -f --context ApplicationDbContext add-license-cs: go install github.com/fbiville/headache/cmd/headache@latest headache --configuration ./configuration-cs.json diff --git a/src/Dockerfile b/src/Dockerfile index 4508810f..942c9a6b 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -1,11 +1,11 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY ["src/NodeGuard.csproj", "."] RUN dotnet restore "./NodeGuard.csproj" diff --git a/src/NodeGuard.csproj b/src/NodeGuard.csproj index 32680fdf..dc56a573 100644 --- a/src/NodeGuard.csproj +++ b/src/NodeGuard.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable enable aspnet-NodeGuard-66F1BF9B-21F9-40C4-8380-C86E94E1BCAC @@ -25,29 +25,29 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + - - + + - - - + + + - - - - - + + + + + @@ -56,7 +56,7 @@ - + diff --git a/src/Program.cs b/src/Program.cs index fb2b4dec..79479bdc 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -44,6 +44,7 @@ using NodeGuard.Helpers; using NodeGuard.Rpc; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Npgsql; using Quartz.AspNetCore; namespace NodeGuard @@ -57,7 +58,7 @@ public static void Main(string[] args) var builder = WebApplication.CreateBuilder(args); var jsonFormatter = new LowerCaseJsonFormatter(); - + Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) @@ -65,7 +66,7 @@ public static void Main(string[] args) .Enrich.With(new DatadogLogEnricher()) .WriteTo.Console(jsonFormatter) .CreateLogger(); - + builder.Host.UseSerilog(); //Add services to the container. @@ -105,7 +106,8 @@ public static void Main(string[] args) builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); - builder.Services.AddTransient(); + builder.Services + .AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); @@ -125,19 +127,26 @@ public static void Main(string[] args) builder.Services.AddTransient(); //DbContext + var dataSourceBuilder = new NpgsqlDataSourceBuilder(Constants.POSTGRES_CONNECTIONSTRING); + dataSourceBuilder.EnableDynamicJson(); + + var npgsqlDataSource = dataSourceBuilder.Build(); builder.Services.AddDbContext(options => { //options.EnableSensitiveDataLogging(); //options.EnableDetailedErrors(); - options.UseNpgsql(Constants.POSTGRES_CONNECTIONSTRING, options => + + + options.UseNpgsql(npgsqlDataSource, npgsqlDbContextOptionsBuilder => { - options.UseQuerySplittingBehavior(QuerySplittingBehavior + npgsqlDbContextOptionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior .SingleQuery); // Slower but integrity is ensured }); }, ServiceLifetime.Transient); - + //DataProtection - builder.Services.AddDbContext(options => options.UseNpgsql(Constants.POSTGRES_CONNECTIONSTRING)); + builder.Services.AddDbContext(options => + options.UseNpgsql(Constants.POSTGRES_CONNECTIONSTRING)); builder.Services.AddDataProtection() .PersistKeysToDbContext() .SetApplicationName("NodeGuard"); @@ -158,8 +167,9 @@ public static void Main(string[] args) // Setup a HTTP/2 endpoint without TLS. options.ListenAnyIP(50051, o => o.Protocols = HttpProtocols.Http2); - options.ListenAnyIP(int.Parse(Environment.GetEnvironmentVariable("HTTP1_LISTEN_PORT") ?? "80"), o => o.Protocols = - HttpProtocols.Http1); + options.ListenAnyIP(int.Parse(Environment.GetEnvironmentVariable("HTTP1_LISTEN_PORT") ?? "80"), o => + o.Protocols = + HttpProtocols.Http1); }); //DBContextFactory @@ -207,8 +217,12 @@ public static void Main(string[] args) q.AddTrigger(opts => { - opts.ForJob(nameof(SweepAllNodesWalletsJob)).WithIdentity($"{nameof(SweepAllNodesWalletsJob)}Trigger") - .StartNow().WithSimpleSchedule(scheduleBuilder => { scheduleBuilder.WithIntervalInMinutes(1).RepeatForever(); }); + opts.ForJob(nameof(SweepAllNodesWalletsJob)) + .WithIdentity($"{nameof(SweepAllNodesWalletsJob)}Trigger") + .StartNow().WithSimpleSchedule(scheduleBuilder => + { + scheduleBuilder.WithIntervalInMinutes(1).RepeatForever(); + }); }); //Monitor Withdrawals Job diff --git a/test/NodeGuard.Tests/Jobs/NodeChannelSubscribeJobTests.cs b/test/NodeGuard.Tests/Jobs/NodeChannelSubscribeJobTests.cs index 3bdb2488..b40e0242 100644 --- a/test/NodeGuard.Tests/Jobs/NodeChannelSubscribeJobTests.cs +++ b/test/NodeGuard.Tests/Jobs/NodeChannelSubscribeJobTests.cs @@ -67,7 +67,7 @@ public async Task NodeUpdateManagement_ThrowsException_WhenCloseAddressIsEmpty() }; // Act + Assert - Assert.ThrowsAsync(async () => await _nodeUpdateManager.NodeUpdateManagement(channelEventUpdate, new Node())); + await Assert.ThrowsAnyAsync(async () => await _nodeUpdateManager.NodeUpdateManagement(channelEventUpdate, new Node())); } [Fact] diff --git a/test/NodeGuard.Tests/NodeGuard.Tests.csproj b/test/NodeGuard.Tests/NodeGuard.Tests.csproj index 823bae4c..5f764148 100644 --- a/test/NodeGuard.Tests/NodeGuard.Tests.csproj +++ b/test/NodeGuard.Tests/NodeGuard.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable @@ -12,20 +12,20 @@ - - - - + + + + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -33,7 +33,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - +