Class: Raw::WebrickAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/raw/adapter/webrick.rb,
lib/raw/adapter/webrick/vcr.rb

Overview

A Webrick Adapter for Nitro. Webrick is a pure Ruby web server included in the default Ruby distribution. The Webrick Adapter is the prefered adapter in development/debug environments. It is also extremely easy to setup.

However, for live/production environments, you should prefer a more performant adapter like Mongrel or FCGI. Mongrel is the suggested adapter for production applications.

Defined Under Namespace

Classes: Swallow

Instance Method Summary collapse

Methods inherited from Adapter

#initialize

Constructor Details

This class inherits a constructor from Raw::Adapter

Instance Method Details

#setup(server) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/raw/adapter/webrick/vcr.rb', line 7

def setup(server)
  if $record_session_filename
    require "raw/adapter/webrick/vcr"
    vcr_record($record_session_filename)
  end
  
  if $playback_session_filename
    require "raw/adapter/webrick/vcr"
    vcr_playback($playback_session_filename)
  end
end

#start(app) ⇒ Object

Start the adapter.



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
# File 'lib/raw/adapter/webrick.rb', line 27

def start(app)
  super
  
  if RUBY_PLATFORM !~ /mswin32/
    wblog = WEBrick::BasicLog::new("/dev/null")
  elsif File.exist? "log"
    wblog = WEBrick::BasicLog::new("log/access.log")
  else
    wblog = STDERR
  end

  webrick_options = app.options.dup
  
  require "webrick/https" if webrick_options[:SSLEnable]

  webrick_options.update(
    :BindAddress => app.address, 
    :Port => app.port, 
    :DocumentRoot => app.public_dir,
    :Logger => Swallow,
    :AccessLog => [
      [wblog, WEBrick::AccessLog::COMMON_LOG_FORMAT],
      [wblog, WEBrick::AccessLog::REFERER_LOG_FORMAT]
    ]
  )

  trap("INT") { stop }
  
  @webrick = WEBrick::HTTPServer.new(webrick_options)
  @webrick.mount("/", WebrickHandler, app)
  @webrick.start
end

#stopObject

Stop the adapter.



62
63
64
65
# File 'lib/raw/adapter/webrick.rb', line 62

def stop
  super
  @webrick.shutdown
end

#vcr_playback(filename = "session.yaml") ⇒ Object

Playback a recorded session. Typically used for testing.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/raw/adapter/webrick/vcr.rb', line 51

def vcr_playback(filename = "session.yaml")
  info "Playing back application server session from '#{filename}'."
  
  $playback_session = YAML.load_file(filename)
  $playback_exception_count = 0
      
  WEBrick::HTTPServer.class_eval do
    def start(&block)
      run(nil)
    end
    
    def run(sock)
      while true
        delta, req, res = $playback_session.shift
        
        if delta
          sleep(delta)
          begin
            service(req, res)
          rescue Object => ex
            $playback_exception_count += 1
            p "---", ex                
          end
        else
          return
        end
      end
    end
  end
  
  at_exit do
    puts "\n\n"
    puts "Playback raised #$playback_exception_count exceptions.\n"
    puts "\n"
  end
end

#vcr_record(filename = "session.yaml") ⇒ Object

Enables session recording. The recorded data can be used for automatic app testing by means of the playback mode.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/raw/adapter/webrick/vcr.rb', line 22

def vcr_record(filename = "session.yaml")
  info "Recording application server session to '#{filename}'."
  
  require "facets/core/file/self/create"

  $record_session = []
  $last_record_time = Time.now

  Nitro::WebrickHandler.class_eval do   
    def do_GET(req, res)
      record_context(req, res)
      handle(req, res)
    end
    alias_method :do_POST, :do_GET

    def record_context(req, res)
      delta = Time.now - $last_record_time
      $last_record_time = Time.now
      $record_session << [delta, req, res]
    end          
  end

  at_exit do
    File.create(filename, YAML.dump($record_session))
  end
end