Class: Volt::Server

Inherits:
Object show all
Defined in:
lib/volt/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = nil, app = nil) ⇒ Server

You can also optionally pass in a prebooted app



26
27
28
29
30
31
32
33
# File 'lib/volt/server.rb', line 26

def initialize(root_path = nil, app = nil)
  @root_path = root_path || Dir.pwd
  @volt_app = app

  @app_path = File.expand_path(File.join(@root_path, 'app'))

  display_welcome
end

Instance Attribute Details

#app_pathObject (readonly)

Returns the value of attribute app_path.



23
24
25
# File 'lib/volt/server.rb', line 23

def app_path
  @app_path
end

#listenerObject (readonly)

Returns the value of attribute listener.



23
24
25
# File 'lib/volt/server.rb', line 23

def listener
  @listener
end

Instance Method Details

#appObject

App returns the main rack app. In development it will use ForkingServer, which forks the app and processes responses in a child process, that is killed when code changes and reforked. (This provides simple fast code reloading)



50
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
# File 'lib/volt/server.rb', line 50

def app
  # Setup the rack server and adaptor
  RackServerAdaptor.load

  app = Rack::Builder.new

  # Handle websocket connections
  app.use WebsocketHandler

  can_fork = Process.respond_to?(:fork)

  unless can_fork
    Volt.logger.warn('Code reloading in Volt currently depends on `fork`.  Your environment does not support `fork`.  We\'re working on adding more reloading strategies.  For now though you\'ll need to restart the server manually on changes, which sucks.  Feel free to complain to the devs, we really let you down here. :-)')
  end

  # Only run ForkingServer if fork is supported in this env.
  # NO_FORKING can be used to specify that you don't want to use the forking
  # server.
  if !can_fork || Volt.env.production? || Volt.env.test? || ENV['NO_FORKING']
    # In production/test, we boot the app and run the server
    #
    # Sometimes the app is already booted, so we can skip if it is
    boot_volt unless @volt_app

    # Setup the dispatcher (it stays this class during its run)
    SocketConnectionHandler.dispatcher = Dispatcher.new(@volt_app)
    app.run(@volt_app.middleware)
  else
    # In developer
    app.run ForkingServer.new(self)
  end

  app
end

#boot_voltObject



39
40
41
42
43
44
# File 'lib/volt/server.rb', line 39

def boot_volt
  # Boot the volt app
  require 'volt/boot'

  @volt_app ||= Volt.boot(@root_path)
end

#display_welcomeObject



35
36
37
# File 'lib/volt/server.rb', line 35

def display_welcome
  puts File.read(File.join(File.dirname(__FILE__), 'server/banner.txt'))
end