Class: FnordMetric::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/web/web.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Web

Returns a new instance of Web.



3
4
5
6
7
8
9
10
11
# File 'lib/fnordmetric/web/web.rb', line 3

def initialize(opts)
  @opts = opts

  @opts[:server] ||= "thin"
  @opts[:host]   ||= "0.0.0.0"
  @opts[:port]   ||= "4242"

  FnordMetric.register(self)
end

Instance Method Details

#initializedObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fnordmetric/web/web.rb', line 13

def initialized
  server = @opts[:server].downcase

  middleware_stack = @opts[:use] || []

  websocket = FnordMetric::WebSocket.new

  webapp = if FnordMetric.options[:http_websocket_only]
    lambda { |env| [204, {}, [""]] }
  else
    FnordMetric::App.new(@opts)
  end

  dispatch  = Rack::Builder.app do
    use Rack::CommonLogger
    use Rack::ShowExceptions

    map "/stream" do
      run websocket
    end

    map "/" do
      middleware_stack.each do |middleware| 
        use(*middleware[0..1], &middleware[2])
      end

      run webapp
    end

  end

  unless ["thin", "hatetepe"].include? server
    raise "Need an EventMachine webserver, but #{server} isn't"
  end

  host = @opts[:host] 
  port = @opts[:port]

  Rack::Server.start(
    :app => dispatch,
    :server => server,
    :Host => host, 
    :Port => port
  ) && FnordMetric.log("listening on http://#{host}:#{port}")
end