Class: Baron::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, &block) ⇒ Server

Returns a new instance of Server.



33
34
35
36
37
# File 'lib/baron.rb', line 33

def initialize config = {}, &block
  @config = config.is_a?(Config) ? config : Config.new(config)
  @config.instance_eval(&block) if block_given?
  @blog_engine = Baron::BlogEngine.new(@config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/baron.rb', line 31

def config
  @config
end

#siteObject (readonly)

Returns the value of attribute site.



31
32
33
# File 'lib/baron.rb', line 31

def site
  @site
end

Instance Method Details

#call(env) ⇒ Object



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/baron.rb', line 39

def call env
  @request = Rack::Request.new env
  return [400, {}, []] unless @request.get?

  @response = Rack::Response.new
  
  path, ext = @request.path_info.split('.')
  extension = ext.to_s.empty? ? '.html' : ".#{ext}"

  redirected_url, status = @blog_engine.process_redirects(@request.path_info)

  if status
    @response.status = status
    @response['Location'] = redirected_url
  else
    baron_response = @blog_engine.process_request(@request.path_info, env)
    @response.body = [baron_response[:body]]
    @response.status = baron_response[:status]      
    @response['Content-Length'] = baron_response[:body].bytesize.to_s unless baron_response[:body].empty?
    @response['Content-Type'] = Rack::Mime.mime_type(extension)
    @response['Cache-Control'] = (Baron.env == 'production') ? "public, max-age=#{@config[:cache]}" : "no-cache, must-revalidate"
    @response['ETag'] = %("#{Digest::SHA1.hexdigest(baron_response[:body])}")
  end
  
  @response.finish
end