Class: Raps::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/raps/server.rb

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/raps/server.rb', line 5

def self.call(env)
  root = Rails.root.join('public')
  path = Rack::Utils.unescape(env['PATH_INFO'])
  file = root.to_s + path

  status = 404
  headers = { 'Content-Type' => 'text/plain' }
  content = nil

  if File.exist?(file)
    status = 200
    mime = MimeMagic.by_path(file)
    headers = { 'Content-Type' => mime ? mime.type : 'application/octet-stream' }
    content = mime && mime.mediatype == 'text' ? File.read(file) : File.binread(file)
  else
    content = "file not found #{file}"
  end

  [ status, headers, [content] ]
end