Class: Whisper::Server

Inherits:
Object
  • Object
show all
Includes:
Loggy
Defined in:
lib/whisper/server.rb

Constant Summary collapse

PRODUCTION_REFRESH_DELAY =

seconds

60

Instance Method Summary collapse

Constructor Details

#initialize(blog, mode, static_dir, x_accel_redirect) ⇒ Server

Returns a new instance of Server.



18
19
20
21
22
23
# File 'lib/whisper/server.rb', line 18

def initialize blog, mode, static_dir, x_accel_redirect
  @blog = blog
  @static_dir = static_dir
  @x_accel_redirect = x_accel_redirect
  @production = mode == :production
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/whisper/server.rb', line 25

def call env
  start = Time.now
  req = Rack::Request.new env
  res = Rack::Response.new

  deny = false
  path = CGI.unescape req.path_info

  ok = if req.scheme != "http"
    false
  elsif path == "/favicon.ico"
    handle_static "favicon.ico", res
  elsif path =~ /^\/static\/(.+)$/
    handle_static $1, res
  elsif(x = @blog.get_content_for path, req.params, req.request_method.downcase.intern)
    content, content_type = x # oh ruby!

    if req.request_method == "HEAD"
      res.content_length = content.size
      res.content_type = content_type
    else
      res.write content
      res.content_type = content_type
    end
    true
  else
    false
  end

  unless ok
    res.content_type = "text/plain"
    res.status = 404
    res.write "No such intarweb resource"
  end

  result = res.finish
  elapsed = (Time.now - start) * 1000000
  info "#{res.status} #{sprintf '%5dus', elapsed} #{sprintf '%5.1fk', res.length / 1024.to_f} #{req.request_method} #{req.path_info} #{deny ? '' : res.content_type}"
  result
end