Class: Toto::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



323
324
325
326
327
# File 'lib/toto.rb', line 323

def initialize config = {}, &blk
  @config = config.is_a?(Config) ? config : Config.new(config)
  @config.instance_eval(&blk) if block_given?
  @site = Toto::Site.new(@config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



321
322
323
# File 'lib/toto.rb', line 321

def config
  @config
end

#siteObject (readonly)

Returns the value of attribute site.



321
322
323
# File 'lib/toto.rb', line 321

def site
  @site
end

Instance Method Details

#call(env) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/toto.rb', line 329

def call env
  @request  = Rack::Request.new env
  @response = Rack::Response.new

  return [400, {}, []] unless @request.get?

  path, mime = @request.path_info.split('.')
  route = (path || '/').split('/').reject {|i| i.empty? }

  response = @site.go(route, env, *(mime ? mime : []))

  @response.body = [response[:body]]
  @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty?
  @response['Content-Type']   = Rack::Mime.mime_type(".#{response[:type]}")

  # Set http cache headers
  @response['Cache-Control'] = if Toto.env == 'production'
    "public, max-age=#{@config[:cache]}"
  else
    "no-cache, must-revalidate"
  end

  @response['ETag'] = %("#{Digest::SHA1.hexdigest(response[:body])}")

  @response.status = response[:status]
  @response.finish
end