diff --git a/HrynCo.NotificationService.Api/AppSettings.cs b/HrynCo.NotificationService.Api/AppSettings.cs new file mode 100644 index 0000000..a6d15cd --- /dev/null +++ b/HrynCo.NotificationService.Api/AppSettings.cs @@ -0,0 +1,8 @@ +namespace HrynCo.NotificationService.Api; + +public sealed class AppSettings +{ + public const string SectionName = "App"; + + public string ConnectionString { get; init; } = string.Empty; +} \ No newline at end of file diff --git a/HrynCo.NotificationService.Api/Program.cs b/HrynCo.NotificationService.Api/Program.cs index 2798628..95b1753 100644 --- a/HrynCo.NotificationService.Api/Program.cs +++ b/HrynCo.NotificationService.Api/Program.cs @@ -1,3 +1,4 @@ +using HrynCo.NotificationService.Api; using HrynCo.NotificationService.DAL.EF; using HrynCo.NotificationService.Services; using Serilog; @@ -7,13 +8,14 @@ var builder = WebApplication.CreateBuilder(args); builder.Host.UseSerilog((context, lc) => lc.ReadFrom.Configuration(context.Configuration)); +var appSettings = builder.Configuration + .GetSection(AppSettings.SectionName) + .Get() ?? throw new InvalidOperationException("App settings are not configured."); + +builder.Services.AddSingleton(appSettings); builder.Services.AddOpenApi(); builder.Services.AddControllers(); - -string connectionString = builder.Configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string 'Default' is not configured."); - -builder.Services.AddNotificationDataAccess(connectionString); +builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationServices(); var app = builder.Build(); diff --git a/HrynCo.NotificationService.Api/appsettings.json b/HrynCo.NotificationService.Api/appsettings.json index 253cb2d..c78a6b6 100644 --- a/HrynCo.NotificationService.Api/appsettings.json +++ b/HrynCo.NotificationService.Api/appsettings.json @@ -1,6 +1,6 @@ { - "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" + "App": { + "ConnectionString": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" }, "Serilog": { "MinimumLevel": { diff --git a/HrynCo.NotificationService.Worker/AppSettings.cs b/HrynCo.NotificationService.Worker/AppSettings.cs new file mode 100644 index 0000000..c8cd459 --- /dev/null +++ b/HrynCo.NotificationService.Worker/AppSettings.cs @@ -0,0 +1,8 @@ +namespace HrynCo.NotificationService.Worker; + +public sealed class AppSettings +{ + public const string SectionName = "App"; + + public string ConnectionString { get; init; } = string.Empty; +} \ No newline at end of file diff --git a/HrynCo.NotificationService.Worker/Program.cs b/HrynCo.NotificationService.Worker/Program.cs index bb7026a..bebce7a 100644 --- a/HrynCo.NotificationService.Worker/Program.cs +++ b/HrynCo.NotificationService.Worker/Program.cs @@ -8,10 +8,12 @@ var builder = Host.CreateApplicationBuilder(args); builder.Services.AddSerilog(lc => lc.ReadFrom.Configuration(builder.Configuration)); -string connectionString = builder.Configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string 'Default' is not configured."); +var appSettings = builder.Configuration + .GetSection(AppSettings.SectionName) + .Get() ?? throw new InvalidOperationException("App settings are not configured."); -builder.Services.AddNotificationDataAccess(connectionString); +builder.Services.AddSingleton(appSettings); +builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationServices(); builder.Services.AddHostedService(); diff --git a/HrynCo.NotificationService.Worker/appsettings.json b/HrynCo.NotificationService.Worker/appsettings.json index 117dbc3..67e3bf3 100644 --- a/HrynCo.NotificationService.Worker/appsettings.json +++ b/HrynCo.NotificationService.Worker/appsettings.json @@ -1,6 +1,6 @@ { - "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" + "App": { + "ConnectionString": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" }, "Serilog": { "MinimumLevel": {