Class: UU::OS::Util::EmbeddedApplicationServer

Inherits:
EmbeddedHTTPServer show all
Defined in:
uu_os_connection-server-2.3.1/lib/uu/os/util/embedded_application_server.rb

Overview

Simple application server based on EmbeddedHTTPServer to be used mainly for testing purposes.

Examples:

Basic usage:

require 'uu/os/util/embedded_application_server'

server = UU::OS::Util::EmbeddedApplicationServer.new('path/to/application')
server.start
...
server.shutdown

Constant Summary

Instance Attribute Summary

Attributes inherited from EmbeddedHTTPServer

#base_url, #context, #host, #port

Instance Method Summary (collapse)

Methods inherited from EmbeddedHTTPServer

#register_servlet, #shutdown, #status, #stop

Constructor Details

- (EmbeddedApplicationServer) initialize(app_dir = Dir.pwd, options = {})

Creates new instance of server.

Parameters:

  • app_dir (String) (defaults to: Dir.pwd)

    Root directory of application (directory which contains configuration file config.ru, defaults to current working directory).

  • options (Hash) (defaults to: {})

    See UU::OS::Util::EmbeddedHttpServer#initialize.

Options Hash (options):

  • :server_cfg (String, #read, Hash)

    Additional configuration to be passed to application server (this configuration is merged with one given via SERVER_CFG environment variable). Possible values are:

    • String - Path to configuration file in properties format.

    • #read - Instance of existing IO (e.g. File) pointing to data in properties format.

    • Hash - Configuration in plain key-value format.



32
33
34
35
36
37
38
39
40
41
42
# File 'uu_os_connection-server-2.3.1/lib/uu/os/util/embedded_application_server.rb', line 32

def initialize(app_dir = Dir.pwd, options = {})
  app_dir, options = Dir.pwd, app_dir if app_dir.kind_of?(Hash)
  ENV['RACK_ENV'] = 'test' if ENV['RACK_ENV'].nil?
  super(options)
  @app_dir = File.expand_path(app_dir)
  @options = options.dup
  @options[:config] ||= './config.ru'
  @options[:server] ||= 'embedded'
  @options[:server_instance] = @server
  Rack::Handler.register('embedded', EmbeddedHandler)
end

Instance Method Details

- (void) start

This method returns an undefined value.

Starts server.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'uu_os_connection-server-2.3.1/lib/uu/os/util/embedded_application_server.rb', line 46

def start
  @thread = Thread.new {
    Thread.current.abort_on_exception = true
    Thread.current[:BASE_URL] = base_url
    Thread.current[:SERVER_CFG] = @options[:server_cfg]
    Dir.chdir(@app_dir)
    Rack::Server.start(@options)
  }
  while @server.status != :Running
    Thread.pass
    unless @thread.alive?
      @thread.join
      raise
    end
  end
  return
end