Ejemplo n.º 1
0
        public void Serve(int port, string rootPath, string defaultFile, bool enableDirectoryListing, bool enableLogging, string username, string password)
        {
            string baseAddress = string.Format(Strings.url_format, port);
            bool   valid       = true;

            if (!_networkService.PortAvailable(port))
            {
                valid = false;
                _consoleWriter.WriteLine(string.Format(Strings.port_in_use, port));
            }
            if (!_fileService.DirectoryExists(rootPath))
            {
                valid = false;
                _consoleWriter.WriteLine(string.Format(Strings.web_server_root_invalid, rootPath));
            }

            if (string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password) ||
                !string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
            {
                valid = false;
                _consoleWriter.WriteLine(Strings.fill_in_username_and_password);
            }

            if (!valid)
            {
                _consoleWriter.WriteLine(Strings.press_any_key);
                _consoleWriter.ReadLine();
            }
            else
            {
                using (_webServer.CreateServer(baseAddress, enableDirectoryListing, rootPath, defaultFile, enableLogging, username, password))
                {
                    _consoleWriter.WriteLine(string.Format(Strings.listening_at_port, port));

                    if (!string.IsNullOrEmpty(defaultFile))
                    {
                        _consoleWriter.WriteLine(string.Format(Strings.default_file, defaultFile));
                    }

                    if (enableDirectoryListing)
                    {
                        _consoleWriter.WriteLine(Strings.directory_listing_enabled);
                    }
                    else
                    {
                        _consoleWriter.WriteLine(Strings.directory_listing_disabled);
                    }

                    if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
                    {
                        _consoleWriter.WriteLine(Strings.basic_auth_enabled);
                    }

                    if (enableLogging)
                    {
                        _consoleWriter.WriteLine(Strings.logging_enabled);
                    }
                    else
                    {
                        _consoleWriter.WriteLine(Strings.logging_disabled);
                    }

                    _consoleWriter.WriteLine(string.Format(Strings.web_server_root, rootPath));
                    _consoleWriter.WriteLine(Strings.press_any_key);
                    _consoleWriter.ReadLine();
                }
            }
        }