Sha256: 7a64900b608b8d53c8ef6d63fe67c9aebd7883043296c13809136ff73aab2d92

Contents?: true

Size: 1.33 KB

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
smallcage-0.2.6 lib/smallcage/http_server.rb
smallcage-0.2.5 lib/smallcage/http_server.rb