Sha256: 1cf8561c80b8b4d2cefb3f01ec85b5a54fbd4800d7828eac1d0a54a6904abfa6

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

module Hyla
  module Commands
    class Serve < Command

      def self.process(args, options)
        include WEBrick

        my_opts = {}

        destination = options[:destination]  if self.check_mandatory_option?('-d / --destination', options[:destination])

        my_opts[:Port] = options[:port]
        my_opts[:BindAddress] = options[:host]
        my_opts[:baseurl] = options[:baseurl]
        my_opts[:MimeTypes] = self.mime_types
        my_opts[:DoNotReverseLookupmy_opts] = true
        my_opts[:StartCallback] = start_callback(options[:detach])
        my_opts[:AccessLog] = []
        my_opts[:Logger] = Log::new([], Log::WARN)

        # recreate NondisclosureName under utf-8 circumstance
        fh_option = WEBrick::Config::FileHandler
        fh_option[:NondisclosureName] = ['.ht*','~*']
        # Option added to allow to navigate into the directories
        fh_option[:FancyIndexing] = true

        #s = HTTPServer.new(webrick_options(my_opts))
        s = HTTPServer.new(my_opts)

        s.mount(my_opts[:baseurl],HTTPServlet::FileHandler, destination, fh_option)

        Hyla.logger.info "Server address:", "http://#{s.config[:BindAddress]}:#{s.config[:Port]}"

        if options[:detach] # detach the server
          pid = Process.fork { s.start }
          Process.detach(pid)
          Hyla.logger.info "Server detached with pid '#{pid}'.", "Run `kill -9 #{pid}' to stop the server."
        else # create a new server thread, then join it with current terminal
          t = Thread.new { s.start }
          trap("INT") { s.shutdown }
          t.join()
        end
      end


      def self.start_callback(detached)
        unless detached
          Proc.new { Hyla.logger.info "Server running...", "press ctrl-c to stop." }
        end
      end

      def self.mime_types
        mime_types_file = File.expand_path('../../../data/mime.types', File.dirname(__FILE__))
        WEBrick::HTTPUtils::load_mime_types(mime_types_file)
      end

    end # class
  end # module Commands
end # module Hyla

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hyla-1.0.7.pre.7 lib/hyla/commands/serve.rb
hyla-1.0.7.pre.6 lib/hyla/commands/serve.rb
hyla-1.0.7.pre.5 lib/hyla/commands/serve.rb
hyla-1.0.7.pre.3 lib/hyla/commands/serve.rb
hyla-1.0.7.pre.2 lib/hyla/commands/serve.rb
hyla-1.0.7.pre.1 lib/hyla/commands/serve.rb
hyla-1.0.6 lib/hyla/commands/serve.rb
hyla-1.0.5 lib/hyla/commands/serve.rb
hyla-1.0.5.pre.1 lib/hyla/commands/serve.rb
hyla-1.0.4 lib/hyla/commands/serve.rb