Skip to content

Commit

Permalink
workaround for podman host-gateway timeout when selecting frontend ve…
Browse files Browse the repository at this point in the history
…rsion
  • Loading branch information
bjosttveit committed Jun 3, 2024
1 parent f68a61c commit a680417
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/Services/LocalFrontend/Implementation/LocalFrontendService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,52 @@
using LocalTest.Models;
using LocalTest.Services.LocalFrontend.Interface;
using Microsoft.Extensions.Options;
using static System.Linq.Enumerable;

namespace LocalTest.Services.LocalFrontend;

public class LocalFrontendService: ILocalFrontendService
public class LocalFrontendService : ILocalFrontendService
{
private readonly HttpClient _httpClient;
private static readonly Range PortRange = 8080..8090;
private readonly string _localFrontedBaseUrl;

public LocalFrontendService(IHttpClientFactory httpClientFactory, IOptions<LocalPlatformSettings> localPlatformSettings)
public LocalFrontendService(
IHttpClientFactory httpClientFactory,
IOptions<LocalPlatformSettings> localPlatformSettings
)
{
_httpClient = httpClientFactory.CreateClient();
_localFrontedBaseUrl = $"{localPlatformSettings.Value.LocalFrontendProtocol}://{localPlatformSettings.Value.LocalFrontendHostname}";
_httpClient.Timeout = TimeSpan.FromMilliseconds(500);
_localFrontedBaseUrl =
$"{localPlatformSettings.Value.LocalFrontendProtocol}://{localPlatformSettings.Value.LocalFrontendHostname}";
}

public async Task<List<LocalFrontendInfo>> GetLocalFrontendDevPorts()
{
var ports = new List<LocalFrontendInfo>();
for (int i = PortRange.Start.Value; i < PortRange.End.Value; i++)
var tasks = Range(PortRange.Start.Value, PortRange.End.Value).Select(port => TestFrontendDevPort(port));
var result = await Task.WhenAll(tasks);
return result.Where(x => x != null).Select(x => x!.Value).ToList();
}

private async Task<LocalFrontendInfo?> TestFrontendDevPort(int port)
{
try
{
try
var response = await _httpClient.GetAsync($"{_localFrontedBaseUrl}:{port.ToString()}/");
if (response.Headers.TryGetValues("X-Altinn-Frontend-Branch", out var values))
{
var response =
await _httpClient.GetAsync($"{_localFrontedBaseUrl}:{i.ToString()}/");
if (response.Headers.TryGetValues("X-Altinn-Frontend-Branch", out var values))
return new LocalFrontendInfo()
{

ports.Add(new LocalFrontendInfo()
{
Port = i.ToString(),
Branch = values?.First() ?? "Unknown"
});
}
}
catch(HttpRequestException)
{

Port = port.ToString(),
Branch = values?.First() ?? "Unknown"
};
}
}
return ports;
catch (TaskCanceledException) { }
catch (HttpRequestException) { }

return null;
}
}
}

0 comments on commit a680417

Please sign in to comment.