Exemple #1
0
        static void Main(string[] args)
        {
            HttpServer server = new HttpServer(5000);

            server.Start();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += LogUnhandledException;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.Name           = "Entry";

            Config = args.Length > 0 ?
                     ServerConfig.ReadFile(args[0]) :
                     ServerConfig.ReadFile("server.json");

            Environment.SetEnvironmentVariable("ServerLogFolder", Config.serverSettings.logFolder);
            GlobalContext.Properties["ServerName"] = Config.serverInfo.name;
            GlobalContext.Properties["ServerType"] = Config.serverInfo.type.ToString();

            XmlConfigurator.ConfigureAndWatch(new FileInfo(Config.serverSettings.log4netConfig));

            using (Resources = new Resources(Config.serverSettings.resourceFolder))
                using (Database = new Database(
                           Config.dbInfo.host,
                           Config.dbInfo.port,
                           Config.dbInfo.auth,
                           Config.dbInfo.index,
                           Resources))
                {
                    RequestHandlers.Initialize(Resources);

                    Config.serverInfo.instanceId = Guid.NewGuid().ToString();
                    ISManager = new ISManager(Database, Config);
                    ISManager.Run();
                    ChatManager   = new ChatManager(ISManager);
                    ISControl     = new ISControl(ISManager);
                    LegendSweeper = new LegendSweeper(Database);
                    LegendSweeper.Run();

                    Console.CancelKeyPress += delegate
                    {
                        Shutdown.Set();
                    };

                    var port = Config.serverInfo.port;
                    using (var server = new HttpServer($"http://{Config.serverInfo.bindAddress}:{port}/"))
                    {
                        foreach (var uri in RequestHandlers.Get.Keys)
                        {
                            server.GET(uri).Subscribe(Response);
                        }

                        foreach (var uri in RequestHandlers.Post.Keys)
                        {
                            server.POST(uri).Subscribe(Response);
                        }

                        Log.Info("Listening at port " + port + "...");
                        Shutdown.WaitOne();
                    }

                    Log.Info("Terminating...");
                    ISManager.Dispose();
                }
        }
 public static HttpServer getHttpServer()
 {
     if (httpServer == null)
         httpServer = new HttpServer();
     return httpServer;
 }
Exemple #4
0
        static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Initializing Server App... ");

            int    portNum;
            string port = null;
            string path = null;

            // Getting args
            if (args.Length > 1)
            {
                if (args[0] == "--port")
                {
                    port = args[1];
                }
                if (args[0] == "--path")
                {
                    path = args[1];
                }
            }

            if (args.Length > 3)
            {
                if (args[2] == "--port")
                {
                    port = args[3];
                }
                if (args[2] == "--path")
                {
                    path = args[3];
                }
            }

            // Validating Port
            if (!int.TryParse(port, out portNum))
            {
                if (port != null)
                {
                    Console.WriteLine("ERROR: Port parameter provided is not a integer number.");
                    return(1);
                }
                portNum = 8081;
            }

            // Validating Path
            if (path != null)
            {
                if (!Directory.Exists(path))
                {
                    Console.WriteLine("The path \"{0}\" does not exist.", path);
                    return(1);
                }
            }
            else
            {
                path = Directory.GetCurrentDirectory();
            }

            // Notifying App initialization
            Console.WriteLine("Server App initialization Complete!");
            Console.WriteLine($"Using PORT : {portNum}");
            Console.WriteLine($"Using PATH : {path}");
            Console.WriteLine();

            //--------------- Listen --------------//

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            //string cmd;

            Console.WriteLine($"Listening on port {portNum}...");

            var httpServer = new HttpServer(portNum);

            httpServer.StateChanged += (sender, e) =>
            {
                var st = e as StateChangedEventArgs;

                Console.Write("State Changed: ");
                Console.Write(st.PreviousState.ToString());
                Console.Write(" -> ");
                Console.Write(st.CurrentState.ToString());
                Console.WriteLine();
            };


            httpServer.Start();

            //bool breaker = true;

            //while (breaker)
            //{
            //    cmd = Console.ReadLine();
            //    if (cmd.ToLower() == "uptime")
            //    {
            //        Console.WriteLine("Time since Server started: " + stopWatch.Elapsed);
            //    }
            //    else if (cmd.ToLower() == "break")
            //    { breaker = false; }
            //};

            Console.ReadKey();

            httpServer.Stop();
            stopWatch.Stop();

            return(0);
        }