using Sonex.Services.WebApi.EndPoints; using System.Net; using System.Net.Sockets; namespace Sonex.Services.WebApi.Helpers; public static class AppHelpers { public static string NormalizePath(string path) { if (string.IsNullOrWhiteSpace(path)) return path; return path .Replace('\\', Path.DirectorySeparatorChar) .Replace('/', Path.DirectorySeparatorChar); } public static void EnsureDirectoryExists(string dir) { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); } public static IEnumerable GetLocalHttpLinks(int port, string path = "") { if (!string.IsNullOrEmpty(path) && !path.StartsWith('/')) path = "/" + path; yield return $"http://localhost:{port}{path}"; foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip)) yield return $"http://{ip}:{port}{path}"; } } public static void ShowStartupInfo(WebApplication app, int port, string webRootDir, string releasesDir, SonexAppEndpoints.SonexClientVersion clientVersion) { Console.WriteLine("============================================================"); Console.WriteLine(" Sonex Services Updates"); Console.WriteLine("============================================================"); Console.WriteLine($" Environment : {app.Environment.EnvironmentName}"); Console.WriteLine($" Port : {port}"); Console.WriteLine($" WebRootDir : {Path.GetFullPath(webRootDir)}"); Console.WriteLine($" ReleasesDir : {Path.GetFullPath(releasesDir)}"); Console.WriteLine("============================================================"); Console.WriteLine(" Sonex Client Version"); Console.WriteLine("============================================================"); Console.WriteLine($" FileName : {clientVersion.FileName}"); Console.WriteLine($" Version : {clientVersion.Version}"); Console.WriteLine($" Url : {clientVersion.Url}"); Console.WriteLine($" Size : {clientVersion.Size}"); Console.WriteLine($" Sha256 : {clientVersion.Sha256}"); Console.WriteLine("============================================================"); Console.WriteLine(" Links"); Console.WriteLine("============================================================"); foreach (var url in GetLocalHttpLinks(port)) Console.WriteLine($" {url}"); foreach (var url in GetLocalHttpLinks(port, "/latest/")) Console.WriteLine($" {url}"); foreach (var url in GetLocalHttpLinks(port, "/latest.json")) Console.WriteLine($" {url}"); Console.WriteLine(); Console.WriteLine(" Logs endpoint:"); foreach (var url in GetLocalHttpLinks(port, "/api/client-errors")) Console.WriteLine($" {url}"); Console.WriteLine(); Console.WriteLine(" Listening:"); foreach (var url in app.Urls) Console.WriteLine($" {url}"); Console.WriteLine("============================================================"); } }