示例#1
0
        private void InitializeRouteManagers()
        {
            DynamicRoutes = new DynamicRouteManager();
            StaticRoutes  = new StaticRouteManager();
            ContentRoutes = new ContentRouteManager();

            _ContentRouteProcessor = new ContentRouteProcessor(ContentRoutes);
            OptionsRoute           = null;
        }
        /// <summary>
        /// Creates a new instance of the Watson Webserver.
        /// </summary>
        /// <param name="ip">IP address on which to listen.</param>
        /// <param name="port">TCP port on which to listen.</param>
        /// <param name="ssl">Specify whether or not SSL should be used (HTTPS).</param>
        /// <param name="defaultRequestHandler">Method used when a request is received and no routes are defined.  Commonly used as the 404 handler when routes are used.</param>
        public Server(string ip, int port, bool ssl, Func <HttpRequest, HttpResponse> defaultRequestHandler, bool debug)
        {
            if (String.IsNullOrEmpty(ip))
            {
                ip = "*";
            }
            if (port < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (defaultRequestHandler == null)
            {
                throw new ArgumentNullException(nameof(defaultRequestHandler));
            }

            Http         = new HttpListener();
            ListenerIp   = ip;
            ListenerPort = port;
            ListenerSsl  = ssl;
            Logging      = new LoggingManager(debug);
            if (debug)
            {
                DebugRestRequests  = true;
                DebugRestResponses = true;
            }

            DynamicRoutes    = new DynamicRouteManager(Logging, debug);
            StaticRoutes     = new StaticRouteManager(Logging, debug);
            ContentRoutes    = new ContentRouteManager(Logging, debug);
            ContentProcessor = new ContentRouteProcessor(Logging, debug, ContentRoutes);
            DefaultRoute     = defaultRequestHandler;
            OptionsRoute     = null;

            Console.Write("Starting Watson Webserver at ");
            if (ListenerSsl)
            {
                Console.WriteLine("https://" + ListenerIp + ":" + ListenerPort);
            }
            else
            {
                Console.WriteLine("http://" + ListenerIp + ":" + ListenerPort);
            }

            TokenSource = new CancellationTokenSource();
            Token       = TokenSource.Token;
            Task.Run(() => StartServer(Token), Token);
        }