Class: Minescope::Rack::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/minescope/rack/application.rb

Instance Method Summary collapse

Constructor Details

#initialize(world_dir) ⇒ Application

Returns a new instance of Application.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/minescope/rack/application.rb', line 8

def initialize(world_dir)
  @app = ::Rack::Builder.new do |builder|
    map "/" do
      static_path = File.join(File.dirname(__FILE__), "../static")
      run ::Rack::File.new(static_path)
    end
    map "/world" do
      run WorldData.new(world_dir)
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/minescope/rack/application.rb', line 20

def call(env)
  if env['PATH_INFO'] == '/'
    script_name = env['SCRIPT_NAME']
    scheme = env['rack.url_scheme']
    host = env['SERVER_NAME']
    port = ":#{env['SERVER_PORT']}"
    if scheme == 'http' and port == ':80'
      port = ''
    elsif scheme == 'https' and port == ':443'
      port = ''
    end
    host_port = env['HTTP_HOST'] || "#{host}#{port}"
    index_url = "#{scheme}://#{host_port}#{script_name}/index.html"
    [302, {'Content-Type' => 'text/plain', 'Location' => index_url}, ["302 Found"]]
  else
    @app.call(env)
  end
end