Skip to content

Commit

Permalink
Fix docker linux issue where ip address was not correctly identified.
Browse files Browse the repository at this point in the history
  • Loading branch information
pm7y committed May 19, 2022
1 parent 92c3276 commit a52d9db
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class ValidateAllSubscriptionsCommandHandler : IRequestHandler<ValidateAl
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<ValidateAllSubscriptionsCommandHandler> _logger;
private readonly SimulatorSettings _simulatorSettings;
private readonly ValidationIpAddress _validationIpAddress;
private readonly ValidationIpAddressProvider _validationIpAddress;

public ValidateAllSubscriptionsCommandHandler(ILogger<ValidateAllSubscriptionsCommandHandler> logger,
IHttpClientFactory httpClientFactory,
SimulatorSettings simulatorSettings,
ValidationIpAddress validationIpAddress)
ValidationIpAddressProvider validationIpAddress)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
Expand Down
29 changes: 0 additions & 29 deletions src/AzureEventGridSimulator/Infrastructure/ValidationIpAddress.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace AzureEventGridSimulator.Infrastructure;

public class ValidationIpAddressProvider
{
private static string _ipAddress;
private static readonly object _lock = new();

public string Create()
{
return (NetworkInterface.GetAllNetworkInterfaces()
.SelectMany(o => o.GetIPProperties().DnsAddresses)
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork &&
!ip.ToString().StartsWith("172") &&
!IPAddress.IsLoopback(ip)) ?? IPAddress.Loopback).ToString();
}

public override string ToString()
{
lock (_lock)
{
if (string.IsNullOrWhiteSpace(_ipAddress))
{
_ipAddress = Create();
}
}

return _ipAddress;
}

public static implicit operator string(ValidationIpAddressProvider d)
{
return d.ToString();
}
}
63 changes: 39 additions & 24 deletions src/AzureEventGridSimulator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,50 @@ await host.StartAsync(token)

private static async Task OnApplicationStarted(IApplicationBuilder app, IHostApplicationLifetime lifetime)
{
Log.Information("It's Alive !");
try
{
Log.Verbose("Started");

var simulatorSettings = (SimulatorSettings)app.ApplicationServices.GetService(typeof(SimulatorSettings));
var simulatorSettings = app.ApplicationServices.GetService<SimulatorSettings>();

if (simulatorSettings is null)
{
Log.Fatal("Settings are not found. The application will now exit");
lifetime.StopApplication();
return;
}
if (simulatorSettings is null)
{
Log.Fatal("Settings are not found. The application will now exit");
lifetime.StopApplication();
return;
}

if (!simulatorSettings.Topics.Any())
{
Log.Fatal("There are no configured topics. The application will now exit");
lifetime.StopApplication();
return;
}
if (!simulatorSettings.Topics.Any())
{
Log.Fatal("There are no configured topics. The application will now exit");
lifetime.StopApplication();
return;
}

if (simulatorSettings.Topics.All(o => o.Disabled))
{
Log.Fatal("All of the configured topics are disabled. The application will now exit");
lifetime.StopApplication();
return;
}
if (simulatorSettings.Topics.All(o => o.Disabled))
{
Log.Fatal("All of the configured topics are disabled. The application will now exit");
lifetime.StopApplication();
return;
}

var mediator = app.ApplicationServices.GetService<IMediator>();

if (mediator is null)
{
Log.Fatal("Required component was not found. The application will now exit");
lifetime.StopApplication();
return;
}

if (app.ApplicationServices.GetService(typeof(IMediator)) is IMediator mediator)
{
await mediator.Send(new ValidateAllSubscriptionsCommand());

Log.Information("It's alive !");
}
catch (Exception e)
{
Log.Fatal(e, "It died !");
lifetime.StopApplication();
}
}

Expand Down Expand Up @@ -160,12 +176,11 @@ private static WebApplicationBuilder ConfigureWebHost(string[] args, IConfigurat
builder.Host.ConfigureServices(services =>
{
services.AddSimulatorSettings(configuration);
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddHttpClient();
services.AddScoped<SasKeyValidator>();
services.AddSingleton<ValidationIpAddress>();
services.AddSingleton<ValidationIpAddressProvider>();
services.AddControllers(options => { options.EnableEndpointRouting = false; })
.AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; });
Expand Down

0 comments on commit a52d9db

Please sign in to comment.