Module: Bender::HttpListener

Defined in:
lib/http_listener.rb

Overview

Handles WEBrick and auto-mounting servlets

Author

Michael Heijmans ([email protected])

Copyright

Copyright © 2013-2015 Michael Heijmans

License

MIT

Class Method Summary collapse

Class Method Details

.start(bot) ⇒ Object

Starts the WEBrick server and mounts all the servlets


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
# File 'lib/http_listener.rb', line 14

def self.start(bot)
  @bot = bot

  if Bender.config.http_log_file == "!BENDER_LOGGER"
    logger = Bender.log
  else
    logger = MonoLogger.new(Bender.config.http_log_file, Bender.config.http_log_rotation)
    logger.level = Bender.config.http_log_level
  end

  begin
    Bender.log.info("starting http listener on port #{Bender.config.http_port}...")
    server = WEBrick::HTTPServer.new(
                                      :Port               => Bender.config.http_port,
                                      :MaxClients         => Bender.config.http_max_clients,
                                      :Logger             => logger,
                                      :BindAddress        => Bender.config.http_listen_address,
                                      :bot                => bot,
                                    )
    Bender.log.info "Starting servlets"
    Bender::HTTP.constants.each do |i|
      klass = "Bender::HTTP::#{i.to_s}".classify
      mountpoint = klass.mountpoint
      server.mount mountpoint, klass
      Bender.log.info "  Starting servlet: #{i.to_s} mounted at #{mountpoint}"
    end
    server.start

  rescue => e
    Bender.log.error "HTTP server caught execption"
    Bender.log.error e
    raise e
  end
end