Class: TotoBongo::Server

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

Overview

The HTTP server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



504
505
506
507
508
# File 'lib/toto-bongo.rb', line 504

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



502
503
504
# File 'lib/toto-bongo.rb', line 502

def config
  @config
end

#siteObject (readonly)

Returns the value of attribute site.



502
503
504
# File 'lib/toto-bongo.rb', line 502

def site
  @site
end

Instance Method Details

#call(env) ⇒ Object

This is the entry point of the request On each request, this is the first method that gets called



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/toto-bongo.rb', line 515

def call env
  TotoBongo::logger.debug("***************REQUEST BEGIN*************")
  @request  = Rack::Request.new env
  @response = Rack::Response.new
  return [400, {}, []] unless @request.get?


  #puts "Request path info is: #{@request.path_info}"

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

  response = @site.go(route, env, *(mime ? mime : []))
  
  TotoBongo.logger.debug("Line 524 response class = #{response.class}")
  TotoBongo.logger.debug("Line 526 @esponse class = #{@response.class}")
  TotoBongo.logger.debug("Line 526 @response.body class = #{@response.body.class}")
  TotoBongo.logger.debug("Line 525 response = #{response}")
  TotoBongo.logger.debug("Line 527 @response = #{@response}")
  
  @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 TotoBongo.env == 'production'
    "public, max-age=#{@config[:cache]}"
  else
    "no-cache, must-revalidate"
  end

  @response['ETag'] = %("#{Digest::SHA1.hexdigest(response[:body])}")
  TotoBongo::logger.debug("****************REQUEST END******************")

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