示例#1
0
        static void Main()
        {
            Console.ResetColor();
            // Set window title
            var assembly = Assembly.GetExecutingAssembly().GetName();

            Console.Title = $"{assembly.Name} v{assembly.Version}";

            Log.Info(DEBUG ? "Starting server in DEBUG mode" : "Starting server");
            Log.Info("Loading configurations...");
            try
            {
                Config = new AppConfig("config.json");
            }
            catch (ConfigException e)
            {
                Log.Fatal($"{e.GetType().Name}: {e.Message}", e, false);
                Terminate(14001);
            }
            catch (Exception e)
            {
                Log.Fatal($"Unexpected error: {e.GetType().Name}: " + e.Message, e, true);
                Terminate(1);
            }
            // Set logging level
            Log.Config($"Setting log level to '{Config["appSettings"]["logLevel"]}'");
            Log.LogLevel = Level.GetLevel(Config["appSettings"]["logLevel"].Value <string>());
            // Toggle highlighting
            Log.Config($"Setting '{nameof(Log.UseConsoleHighlighting)}' to {Config["appSettings"]["useConsoleColors"].ToString().ToLower()}");
            Log.UseConsoleHighlighting = Config["appSettings"]["useConsoleColors"].Value <bool>();

            // Assign the reload event to OnConfigReload
            Config.Reload += OnConfigReload;

            // Create the HTTPListener
            Log.Config("Creating listener...");
            string[] addresses = Config["serverSettings"]["serverAddresses"].ToObject <string[]>();
            addresses = addresses.Length == 0 ? new string[] { GetLocalIP(), "localhost" } : addresses;
            listener  = new Listener(addresses);
            Log.Info("Listening on: " + string.Join(", ", addresses));

            // Get custom queues
            JSONQueue = listener.GetCustomQueue(x =>
                                                x.Request.ContentType == "application/json" ||
                                                (x.Request.ContentType == "application/octet-stream" &&
                                                 x.Request.Cookies["session"] != null)
                                                );
            ResourceQueue = listener.GetCustomQueue(x =>
                                                    x.Request.AcceptTypes != null &&
                                                    !x.Request.AcceptTypes.Contains("text/html")
                                                    );

            // Call the rest of the setup
            Setup();

            // Start listening
            Log.Config("Starting listener...");
            listener.Start();

            // Create exiter thread that releases the exit mutex when enter is pressed
            new Thread(() =>
            {
                Console.ReadLine();
                ExitLock.Release();
            })
            {
                Name = "Program Exiter"
            }.Start();

            // Wait until the exit mutex is released, then terminate
            ExitLock.Wait();
            Terminate(ExitCode);
        }
 /// <summary>
 /// Initializes a new instance of <see cref="LockExiter"/>.
 /// </summary>
 /// <param name="exitLockMethod">The delegate to invoke if a call to <paramref name="verifyLockMethod"/> returns true.</param>
 /// <param name="verifyLockMethod">The delegate indicating whether <paramref name="exitLockMethod"/> is allowed to be invoked.</param>
 public LockExiter(ExitLock exitLockMethod, VerifyLock verifyLockMethod)
 {
   _exitLock = exitLockMethod;
   _verifyLock = verifyLockMethod;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="LockExiter"/>.
 /// </summary>
 /// <param name="exitLockMethod">The delegate to invoke if a call to <paramref name="verifyLockMethod"/> returns true.</param>
 /// <param name="verifyLockMethod">The delegate indicating whether <paramref name="exitLockMethod"/> is allowed to be invoked.</param>
 public LockExiter(ExitLock exitLockMethod, VerifyLock verifyLockMethod)
 {
     _exitLock   = exitLockMethod;
     _verifyLock = verifyLockMethod;
 }