class Raamen::FileServer

Constants

MIME_TYPES

Public Class Methods

new(root) click to toggle source
# File lib/raamen/static.rb, line 32
def initialize(root)
  @root = root
end

Public Instance Methods

call(env) click to toggle source
# File lib/raamen/static.rb, line 36
def call(env)
  req = Rack::Request.new(env)
  res = Rack::Response.new
  file_path = File.join(
          Dir.pwd,
    req.path
  )

  if File.exist?(file_path)
    extension = File.extname(file_path)
    content_type = MIME_TYPES[extension]
    file_content = File.read(file_path)
    res["Content-Type"] = content_type
    res.write(file_content)
  else
    res.status = 404
    res.write("File not found")
  end

  res
end