Class: Lepus::Web::App

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

Class Method Summary collapse

Class Method Details

.buildObject



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
# File 'lib/lepus/web/app.rb', line 6

def self.build
  root = Web.assets_path

  Rack::Builder.new do
    use Rack::Static,
      urls: ["/assets", "/sw.js"],
      root: root.to_s

    map "/api" do
      run Lepus::Web::API.new
    end

    run lambda { |env|
      req = Rack::Request.new(env)
      path = req.path_info

      if path == "/" || path == "/index.html"
        [200, {"content-type" => "text/html"}, [Web.render_index(env)]]
      else
        file_path = root.join(path.sub(%r{^/}, ""))
        if File.file?(file_path)
          [200, {"content-type" => Web.mime_for(file_path)}, [File.binread(file_path)]]
        else
          [200, {"content-type" => "text/html"}, [Web.render_index(env)]]
        end
      end
    }
  end
end