read backend api url from environment variables
All checks were successful
Restart Docker Container on Production / build (pull_request) Successful in 3m35s

This commit is contained in:
iTob 2024-09-23 12:46:09 +02:00
parent b8781215ae
commit 5076367282
6 changed files with 51 additions and 11 deletions

View File

@ -26,5 +26,5 @@ jobs:
docker pull git.wohlleben.dev/itob/rbl-news-webapp:latest docker pull git.wohlleben.dev/itob/rbl-news-webapp:latest
docker stop rbl-news-webapp docker stop rbl-news-webapp
docker rm rbl-news-webapp docker rm rbl-news-webapp
docker run --init -d --name rbl-news-webapp -p 8000:8080 --restart=always git.wohlleben.dev/itob/rbl-news-webapp:latest docker run --init -d --name rbl-news-webapp -p 8000:8080 -e RblApiUrl=${{ vars.RBL_PROD_ENV_API_URL }} --restart=always git.wohlleben.dev/itob/rbl-news-webapp:latest
docker system prune -af docker system prune -af

View File

@ -40,5 +40,5 @@ jobs:
docker pull git.wohlleben.dev/itob/rbl-news-webapp:latest docker pull git.wohlleben.dev/itob/rbl-news-webapp:latest
docker stop rbl-news-webapp docker stop rbl-news-webapp
docker rm rbl-news-webapp docker rm rbl-news-webapp
docker run --init -d --name rbl-news-webapp -p 8000:8080 --restart=always git.wohlleben.dev/itob/rbl-news-webapp:latest docker run --init -d --name rbl-news-webapp -p 8000:8080 -e RblApiUrl=${{ vars.RBL_TEST_ENV_API_URL }} --restart=always git.wohlleben.dev/itob/rbl-news-webapp:latest
docker system prune -af docker system prune -af

View File

@ -31,7 +31,20 @@ namespace RBLNews.Maui
builder.Logging.AddDebug(); builder.Logging.AddDebug();
#endif #endif
return builder.Build(); MauiApp app = builder.Build();
using var serviceScope = app.Services.CreateScope();
var services = serviceScope.ServiceProvider;
var environmentVariablesService = services.GetRequiredService<EnvironmentVariablesService>();
string? backendApiUrl = environmentVariablesService.RblApiUrl;
if (string.IsNullOrEmpty(backendApiUrl))
{
throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{backendApiUrl}'");
}
return app;
} }
} }
} }

View File

@ -0,0 +1,6 @@
namespace RBLNews.Shared.Services;
public class EnvironmentVariablesService
{
public string? RblApiUrl => Environment.GetEnvironmentVariable("RblApiUrl");
}

View File

@ -15,15 +15,23 @@ namespace RBLNews.Shared.Services
public class FeedDataService : IFeedDataService public class FeedDataService : IFeedDataService
{ {
private readonly EnvironmentVariablesService _environmentVariablesService;
private static readonly HttpClient httpClient = new(); private static readonly HttpClient httpClient = new();
public RssVM Feeds { get; private set; } public RssVM Feeds { get; private set; }
public Action DataChanged { get; set; } public Action DataChanged { get; set; }
public FeedDataService(EnvironmentVariablesService environmentVariablesService)
{
this._environmentVariablesService = environmentVariablesService;
}
public async Task LoadFeeds() public async Task LoadFeeds()
{ {
Feeds = await httpClient.GetFromJsonAsync<RssVM>("https://rblnews.de/api/feeds") ?? new RssVM(); Feeds = await httpClient.GetFromJsonAsync<RssVM>($"{this._environmentVariablesService.RblApiUrl}/api/feeds") ?? new RssVM();
DataChanged.Invoke(); DataChanged.Invoke();
} }
} }

View File

@ -10,6 +10,7 @@ builder.Services.AddBlazorBootstrap();
builder.Services.AddSingleton<IFeedDataService, FeedDataService>(); builder.Services.AddSingleton<IFeedDataService, FeedDataService>();
builder.Services.AddSingleton<AppLifecycleService>(); builder.Services.AddSingleton<AppLifecycleService>();
builder.Services.AddSingleton<EnvironmentVariablesService>();
var app = builder.Build(); var app = builder.Build();
@ -17,10 +18,22 @@ var app = builder.Build();
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }
using var serviceScope = app.Services.CreateScope();
IServiceProvider services = serviceScope.ServiceProvider;
EnvironmentVariablesService environmentVariablesService = services.GetRequiredService<EnvironmentVariablesService>();
string? backendApiUrl = environmentVariablesService.RblApiUrl;
if (string.IsNullOrEmpty(backendApiUrl))
{
throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{backendApiUrl}'");
}
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
@ -28,6 +41,6 @@ app.UseAntiforgery();
app.MapRazorComponents<App>() app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode() .AddInteractiveServerRenderMode()
.AddAdditionalAssemblies(typeof(RBLNews.Shared._Imports).Assembly); .AddAdditionalAssemblies(typeof( RBLNews.Shared._Imports ).Assembly);
app.Run(); app.Run();