Class: FileServer

Inherits:
Object show all
Defined in:
lib/static_asset_server.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/static_asset_server.rb', line 29

def call(env)
  res = Rack::Response.new
  file_name = requested_file_name(env)

  if File.exists?(file_name)
    serve(file_name, res)
  else
    res.status = 404
    res.write("File not found")
  end
  res
end

#requested_file_name(env) ⇒ Object



52
53
54
55
56
57
# File 'lib/static_asset_server.rb', line 52

def requested_file_name(env)
  req = Rack::Request.new(env)
  path = req.path
  dir = Gazebo::ROOT
  File.join(dir, path)
end

#serve(file_name, res) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/static_asset_server.rb', line 42

def serve(file_name, res)
  extension = File.extname(file_name)
  extension = '.json' if extension == '.map'
  content_type = Rack::Mime::MIME_TYPES[extension]

  res['Content-Type'] = content_type
  file = File.read(file_name)
  res.write(file)
end