diff --git a/RBLNews.Web/Models/AppSettingsModel.cs b/RBLNews.Shared/Models/AppSettingsModel.cs similarity index 69% rename from RBLNews.Web/Models/AppSettingsModel.cs rename to RBLNews.Shared/Models/AppSettingsModel.cs index 53e9c38..24d434a 100644 --- a/RBLNews.Web/Models/AppSettingsModel.cs +++ b/RBLNews.Shared/Models/AppSettingsModel.cs @@ -1,4 +1,4 @@ -namespace RBLNews.Web.Models; +namespace RBLNews.Shared.Models; public class AppSettingsModel { diff --git a/RBLNews.Shared/Services/Contracts/IConfigService.cs b/RBLNews.Shared/Services/Contracts/IConfigService.cs new file mode 100644 index 0000000..1a07ef1 --- /dev/null +++ b/RBLNews.Shared/Services/Contracts/IConfigService.cs @@ -0,0 +1,8 @@ +using RBLNews.Shared.Models; + +namespace RBLNews.Shared.Services.Contracts; + +public interface IConfigService +{ + AppSettingsModel AppSettings { get; } +} \ No newline at end of file diff --git a/RBLNews.Shared/Services/FeedDataService.cs b/RBLNews.Shared/Services/FeedDataService.cs index 4262b19..baa645b 100644 --- a/RBLNews.Shared/Services/FeedDataService.cs +++ b/RBLNews.Shared/Services/FeedDataService.cs @@ -2,6 +2,8 @@ using System.Net.Http.Json; using RBLFeederCommon.Models.RssFeed; +using RBLNews.Shared.Services.Contracts; + namespace RBLNews.Shared.Services { public interface IFeedDataService @@ -15,7 +17,7 @@ namespace RBLNews.Shared.Services public class FeedDataService : IFeedDataService { - private readonly EnvironmentVariablesService _environmentVariablesService; + private readonly IConfigService _configService; private static readonly HttpClient httpClient = new(); @@ -23,15 +25,14 @@ namespace RBLNews.Shared.Services public Action DataChanged { get; set; } - - public FeedDataService(EnvironmentVariablesService environmentVariablesService) + public FeedDataService(IConfigService configService) { - this._environmentVariablesService = environmentVariablesService; + this._configService = configService; } public async Task LoadFeeds() { - Feeds = await httpClient.GetFromJsonAsync($"{this._environmentVariablesService.RblApiUrl}/api/feeds") ?? new RssVM(); + Feeds = await httpClient.GetFromJsonAsync($"{this._configService.AppSettings.BackendApiUrl}/api/feeds") ?? new RssVM(); DataChanged.Invoke(); } } diff --git a/RBLNews.Web/Program.cs b/RBLNews.Web/Program.cs index dfa915d..119fa32 100644 --- a/RBLNews.Web/Program.cs +++ b/RBLNews.Web/Program.cs @@ -1,4 +1,6 @@ +using RBLNews.Shared.Models; using RBLNews.Shared.Services; +using RBLNews.Shared.Services.Contracts; using RBLNews.Web.Components; using RBLNews.Web.Services; @@ -9,10 +11,9 @@ builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddBlazorBootstrap(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); var app = builder.Build(); @@ -25,24 +26,11 @@ if (!app.Environment.IsDevelopment()) app.UseHsts(); } -using var serviceScope = app.Services.CreateScope(); -IServiceProvider services = serviceScope.ServiceProvider; +AppSettingsModel? appSettings = app.Configuration.GetSection("App").Get(); -var logger = LoggerFactory.Create(config => +if (string.IsNullOrEmpty(appSettings?.BackendApiUrl)) { - config.AddConsole(); -}).CreateLogger("Program"); - - -ConfigService configService = services.GetRequiredService(); -var a = configService.AppSettings; - -EnvironmentVariablesService environmentVariablesService = services.GetRequiredService(); -string? backendApiUrl = environmentVariablesService.RblApiUrl; - -if (string.IsNullOrEmpty(backendApiUrl)) -{ - throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{backendApiUrl}'"); + throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{appSettings?.BackendApiUrl}'"); } app.UseHttpsRedirection(); diff --git a/RBLNews.Web/Services/ConfigService.cs b/RBLNews.Web/Services/ConfigService.cs index 3cf60fd..edd0d47 100644 --- a/RBLNews.Web/Services/ConfigService.cs +++ b/RBLNews.Web/Services/ConfigService.cs @@ -1,18 +1,16 @@ -using RBLNews.Web.Models; +using RBLNews.Shared.Models; +using RBLNews.Shared.Services.Contracts; namespace RBLNews.Web.Services; -public class ConfigService +public class ConfigService : IConfigService { - private readonly IConfiguration _configuration; - - public AppSettingsModel AppSettings { get; private set; } + public AppSettingsModel AppSettings { get; } public ConfigService(IConfiguration configuration) { - this._configuration = configuration; - configuration.GetSection("App").Bind(this.AppSettings); - AppSettings = configuration.GetSection("App").Get(); + if(this.AppSettings == null) + throw new NullReferenceException("App settings not found"); } } \ No newline at end of file diff --git a/RBLNews.Web/appsettings.Development.json b/RBLNews.Web/appsettings.Development.json index af54d1e..7a608cd 100644 --- a/RBLNews.Web/appsettings.Development.json +++ b/RBLNews.Web/appsettings.Development.json @@ -6,6 +6,6 @@ } }, "App": { - "BackendApiUrl": "https://rbl.wohlleben.dev" + "BackendApiUrl": "https://localhost:5147" } } diff --git a/RBLNews.Web/appsettings.Test.json b/RBLNews.Web/appsettings.Test.json new file mode 100644 index 0000000..8ceaf0e --- /dev/null +++ b/RBLNews.Web/appsettings.Test.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "App": { + "BackendApiUrl": "https://rbl.wohlleben.dev" + } +} diff --git a/RBLNews.Web/appsettings.json b/RBLNews.Web/appsettings.json index 0c2c7dc..10f68b8 100644 --- a/RBLNews.Web/appsettings.json +++ b/RBLNews.Web/appsettings.json @@ -5,8 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "App": { - "BackendApiUrl": "https://rblnews.de" - } + "AllowedHosts": "*" }