aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/http_server.rb
blob: 7bd4467491fabf581b97f30d003696d8739d421a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# encoding: UTF-8
#
# Bleuurrgggggh!  Bleurrrrrgghh!
#
require 'mauve/auth_bytemark'
require 'mauve/web_interface'
require 'mauve/mauve_thread'
require 'digest/sha1'
require 'log4r'
require 'thin'
#
# Needed for Lenny version of thin converted by apt-ruby, for some reason.
#
require 'thin_parser'
require 'rack'
require 'rack-flash'
require 'rack/handler/webrick'

################################################################################
#
# Bodge up thin logging.
# 
module Thin
  module Logging
    
    def log(m=nil)
      # return if Logging.silent?
      logger = Log4r::Logger.new "Mauve::HTTPServer"
      logger.info(m || yield)
    end
    
    def debug(m=nil)
      # return unless Logging.debug?
      logger = Log4r::Logger.new "Mauve::HTTPServer"
      logger.debug(m || yield)
    end
    
    def trace(m=nil)
      return unless Logging.trace?
      logger = Log4r::Logger.new "Mauve::HTTPServer"
      logger.debug(m || yield)
    end
    
    def log_error(e=$!)
      logger = Log4r::Logger.new "Mauve::HTTPServer"
      logger.error(e)
      logger.debug(e.backtrace.join("\n")) 
    end

  end
end

################################################################################
# 
# More logging hacks for Rack
#
# @see http://stackoverflow.com/questions/2239240/use-rackcommonlogger-in-sinatra
#
class RackErrorsProxy

  def initialize(l); @logger = l; end

  def write(msg)
    case msg
      when String then @logger.info(msg.chomp)
      when Array then @logger.info(msg.join("\n"))
      else
        @logger.error(msg.inspect)
    end
  end
  
  alias_method :<<, :write
  alias_method :puts, :write

  def flush; end
end



################################################################################
module Mauve

  # 
  # API to control the web server
  #
  class HTTPServer < MauveThread

    include Singleton

    attr_accessor :port, :ip, :document_root, :base_url
    attr_accessor :session_secret 
    
    def initialize
      super
      @port = 1288
      @ip = "127.0.0.1"
      @document_root = "/usr/share/mauvealert"
      @session_secret = "%x" % rand(2**100)
      @server_name = nil
    end
   
    def main_loop
      # 
      # Sessions are kept for 8 days.
      #
      @server = ::Thin::Server.new(@ip, @port, Rack::Session::Cookie.new(WebInterface.new, {:key => "mauvealert", :secret => @session_secret, :expire_after => 691200}), :signals => false)
      @server.start
    end

    def base_url
      @base_url ||= "http://"+Server.instance.hostname
    end
    
    def stop
      @server.stop if @server
      super
    end
  end    
end