Class: Rascut::Httpd

Inherits:
Object
  • Object
show all
Defined in:
lib/rascut/httpd.rb

Defined Under Namespace

Classes: FileOnly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Httpd

Returns a new instance of Httpd.



42
43
44
45
# File 'lib/rascut/httpd.rb', line 42

def initialize(command)
  @command = command
  @threads = []
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



46
47
48
# File 'lib/rascut/httpd.rb', line 46

def command
  @command
end

Instance Method Details

#configObject



86
87
88
# File 'lib/rascut/httpd.rb', line 86

def config
  command.config
end

#loggerObject



90
91
92
# File 'lib/rascut/httpd.rb', line 90

def logger
  config.logger
end

#reload!Object



94
95
96
97
98
# File 'lib/rascut/httpd.rb', line 94

def reload!
  while t = @threads.pop
    t.run
  end
end

#startObject



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
# File 'lib/rascut/httpd.rb', line 48

def start
  swf_path = command.root.to_s
  logger.debug "swf_path: #{swf_path}"
  vendor = Pathname.new(__FILE__).parent.parent.parent.join('vendor')
  logger.debug "vendor_path: #{vendor}"
  reload = reload_handler
  index = index_handler

  #app = Rack::FixedBuilder.new do
  #  use Rack::ShowExceptions
  #  map('/reload') { run reload }
  #  map('/swf/') { run Rack::File.new(swf_path) }
  #  map('/') { run index }
  #end

  urls = []
  urls.concat(config_url_mapping) if config[:mapping]

  urls.concat([
    ['/js/swfobject.js', Rack::ShowExceptions.new(Httpd::FileOnly.new(vendor.join('js/swfobject.js').to_s))],
    ['/swf', Rack::ShowExceptions.new(Rack::File.new(swf_path))],
    ['/reload', Rack::ShowExceptions.new(reload_handler)],
    ['/proxy', Rack::ShowExceptions.new(proxy_handler)],
    ['/', Rack::ShowExceptions.new(index_handler)]
  ])
  logger.debug 'url mappings: ' + urls.map{|u| u.first}.inspect
  app = Rack::URLMap.new(urls)
  port = config[:port] || 3001
  host = config[:bind_address] || '0.0.0.0'

  _args = [app, {:Host => host, :Port => port}]
  server_handler = detect_server
  Thread.new(_args) do |args|
    server_handler.run *args
  end
  logger.info "Start #{server_handler} http://#{host}:#{port}/"
end