Class: Panoptimon::HTTP

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/panoptimon/http.rb

Instance Method Summary collapse

Methods included from Logger

#logger, logger

Constructor Details

#initialize(args = {}) ⇒ HTTP

Returns a new instance of HTTP.



10
11
12
13
14
15
# File 'lib/panoptimon/http.rb', line 10

def initialize (args={})
  @match = []
  @mount = []
  # TODO args[:config].http_port, ssl, etc
  @http = Thin::Server.new('0.0.0.0', 8080, self);
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/panoptimon/http.rb', line 25

def call (env)
  path = env['PATH_INFO']
  return favicon(env) if path == '/favicon.ico'
  # logger.debug { "#{path} => " + env.inspect }
  if    go = @match.find {|x| path =~ x[0]}
  elsif go = @mount.find {|x| path =~ %r{^#{x[0]}(/|$)}}
    env['SCRIPT_NAME'] = go[0]
    env['PATH_INFO'] = path.sub(%r{^#{go[0]}}, '')
  else
    return [404, {'Content-Type' => 'text/html'},
      '<html><head><title>Not Found</title></head>' +
      '<body><p>nope</p></body></html>']
  end
  env['rack.logger'] = logger
  begin
    return go[1].call(env)
  rescue => ex
    logger.error { "error: #{ex.message} #{ex.backtrace.join("\n  ")}" }
    return [500, {'Content-Type' => 'text/html'}, ['bah']]
  end
end

#favicon(env) ⇒ Object



47
48
49
50
51
52
# File 'lib/panoptimon/http.rb', line 47

def favicon(env)
  # TODO bundle / configure favicon?
  # NOTE why doesn't rack/thin support .to_path per spec?
  return [200, {'Content-Type' => 'image/x-icon'},
    Pathname.new('/tmp/favicon.ico').open]
end

#hostportObject



21
22
23
# File 'lib/panoptimon/http.rb', line 21

def hostport
  "#{@http.host}:#{@http.port}"
end

#match(regexp, app) ⇒ Object

regexp-match



55
56
57
58
# File 'lib/panoptimon/http.rb', line 55

def match (regexp, app)
  regexp = %r{^#{regexp}} if regexp.class == String
  @match.push([regexp, app])
end

#mount(point, app) ⇒ Object

path prefix



61
62
63
64
# File 'lib/panoptimon/http.rb', line 61

def mount (point, app)
  point.sub(%r{/*$}, '')
  @mount.push([point, app])
end

#startObject



17
18
19
# File 'lib/panoptimon/http.rb', line 17

def start
  @http.backend.start
end