Sha256: 80d6b815590a1e1102a9ebf0b426f20c1f5b798ebbe81b9c9b1fe3d7d9d0d773

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require 'webrick'

module SmallCage
  class HTTPServer

    def initialize(document_root, port)
      # logger = WEBrick::Log.new(nil, 1)
      @server = WEBrick::HTTPServer.new({
        :DocumentRoot => document_root,
        :Port => port,
        :AccessLog => []
      })

      WEBrick::HTTPServlet::FileHandler.remove_handler('cgi')
      WEBrick::HTTPServlet::FileHandler.remove_handler('rhtml')

      @server.mount('/_smc/update_uri', UpdateUriServlet)
      @server.mount('/_smc/auto', AutoServlet)
    end

    def start
      @server.start
    end

    def shutdown
      @server.shutdown
    end

    def updated_uri=(uri)
      UpdateUriServlet.uri = uri
    end

    def reload
      UpdateUriServlet.uri = ':reload'
    end
  end


  class UpdateUriServlet < WEBrick::HTTPServlet::AbstractServlet
    @@uri = '/index.html'
    @@update_time = ''

    def do_GET(req, res)
      res['content-type'] = 'text/plain'
      res.body = @@uri + "\n" + @@update_time
    end

    def self.uri=(uri)
      @@uri = uri
      update_time
    end

    def self.update_time
      @@update_time = Time.now.to_s
    end
  end

  class AutoServlet < WEBrick::HTTPServlet::AbstractServlet
    def do_GET(req, res)
      res['content-type'] = 'text/html'
      html = File.dirname(__FILE__) + '/resources/auto.html'
      res.body = File.read(html)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smallcage-0.3.2 lib/smallcage/http_server.rb
smallcage-0.3.1 lib/smallcage/http_server.rb
smallcage-0.3.0 lib/smallcage/http_server.rb
smallcage-0.2.9 lib/smallcage/http_server.rb
smallcage-0.2.8 lib/smallcage/http_server.rb
smallcage-0.2.7 lib/smallcage/http_server.rb