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 = false) ⇒ Server

You can also optionally pass in a prebooted app



52
53
54
55
56
57
58
59
# File 'lib/volt/server.rb', line 52

def initialize(root_path = nil, app = false)
  @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.



49
50
51
# File 'lib/volt/server.rb', line 49

def app_path
  @app_path
end

#listenerObject (readonly)

Returns the value of attribute listener.



49
50
51
# File 'lib/volt/server.rb', line 49

def listener
  @listener
end

Instance Method Details

#appObject

App returns the main rack app. In development it will fork a



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/volt/server.rb', line 73

def app
  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.
  if !can_fork || Volt.env.production? || Volt.env.test?
    # 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
    app.run(new_server)
  else
    # In developer
    app.run ForkingServer.new(self)
  end

  app
end

#boot_voltObject



65
66
67
68
69
70
# File 'lib/volt/server.rb', line 65

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

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

#display_welcomeObject



61
62
63
# File 'lib/volt/server.rb', line 61

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

#new_serverObject

new_server returns the core of the Rack app. Volt.boot should be called before generating the new server



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/volt/server.rb', line 105

def new_server
  @rack_app = Rack::Builder.new

  # Should only be used in production
  if Volt.config.deflate
    @rack_app.use Rack::Deflater
    @rack_app.use Rack::Chunked
  end

  @rack_app.use Rack::ContentLength

  @rack_app.use Rack::KeepAlive
  @rack_app.use Rack::ConditionalGet
  @rack_app.use Rack::ETag

  @rack_app.use QuietCommonLogger
  @rack_app.use Rack::ShowExceptions

  component_paths = @volt_app.component_paths
  @rack_app.map '/components' do
    run ComponentHandler.new(component_paths)
  end

  # Serve the opal files
  opal_files = OpalFiles.new(@rack_app, @app_path, @volt_app.component_paths)

  # Serve the main html files from public, also figure out
  # which JS/CSS files to serve.
  @rack_app.use IndexFiles, @volt_app.component_paths, opal_files

  @rack_app.use HttpResource, @volt_app.router

  @rack_app.use Rack::Static,
    urls: ['/'],
    root: 'config/base',
    index: '',
    header_rules: [
      [:all, { 'Cache-Control' => 'public, max-age=86400' }]
    ]

  @rack_app.run lambda { |env| [404, { 'Content-Type' => 'text/html; charset=utf-8' }, ['404 - page not found']] }

  @rack_app
end