using GCLocalServerRewrite.common; using GCLocalServerRewrite.server; using SQLitePCL; using Swan; using Swan.Logging; namespace GCLocalServerRewrite; internal class Program { private static void Main(string[] args) { Batteries_V2.Init(); InitializeLogging(); LogConfigValues(); var urlPrefixes = args.Length > 0 ? new List(args) : new List { "http://+:80", "https://+:443" }; using (var cts = new CancellationTokenSource()) { Task.WaitAll( RunWebServerAsync(urlPrefixes, cts.Token), Task.CompletedTask, WaitForUserBreakAsync(cts.Cancel)); } // Clean up "Bye".Info(nameof(Program)); Terminal.Flush(); Console.WriteLine("Press any key to exit."); WaitForKeypress(); } private static void InitializeLogging() { if (!Directory.Exists(PathHelper.LogRootPath)) { Directory.CreateDirectory(PathHelper.LogRootPath); } Logger.RegisterLogger(new FileLogger(Path.Combine(PathHelper.LogRootPath, Configs.LOG_BASE_NAME), true)); } /// /// Create and run a web server. /// /// /// private static async Task RunWebServerAsync(IEnumerable urlPrefixes, CancellationToken cancellationToken) { using var server = Server.CreateWebServer(urlPrefixes); await server.RunAsync(cancellationToken).ConfigureAwait(false); } /// /// Prompt the user to press any key; /// when a key is next pressed, call the specified action to cancel operations. /// /// Cancel Action to call private static async Task WaitForUserBreakAsync(Action cancel) { // Be sure to run in parallel. await Task.Yield(); "Press any key to stop the web server.".Info(nameof(Program)); WaitForKeypress(); "Stopping...".Info(nameof(Program)); cancel(); } /// /// Clear the console input buffer and wait for a keypress /// private static void WaitForKeypress() { while (Console.KeyAvailable) { Console.ReadKey(true); } Console.ReadKey(true); } private static void LogConfigValues() { var paths = $"Paths: {nameof(PathHelper.HtmlRootPath)}: {PathHelper.HtmlRootPath}\n" + $"{nameof(PathHelper.LogRootPath)}: {PathHelper.LogRootPath}\n" + $"{nameof(PathHelper.DataBaseRootPath)}: {PathHelper.DataBaseRootPath}\n" + $"{nameof(PathHelper.ConfigFilePath)}: {PathHelper.ConfigFilePath}\n" + $"{nameof(PathHelper.CertRootPath)}: {PathHelper.CertRootPath}\n"; paths.Info(); var configs = "Config values: \n" + $"{Configs.SETTINGS.Stringify()}"; configs.Info(); } }