Class: Server

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

Instance Method Summary collapse

Constructor Details

#initialize(root_path = nil) ⇒ Server

Returns a new instance of Server.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/volt/server.rb', line 46

def initialize(root_path=nil)
  root_path ||= Dir.pwd
  Volt.root = root_path

  @app_path = File.expand_path(File.join(root_path, "app"))
  
  @component_paths = ComponentPaths.new(root_path)
  
  setup_change_listener
  
  display_welcome
end

Instance Method Details

#appObject



71
72
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/volt/server.rb', line 71

def app
  @app = Rack::Builder.new
  
  # Should only be used in production
  # @app.use Rack::Deflater

  # @app.use Rack::Chunked
  @app.use Rack::ContentLength

  @app.use Rack::KeepAlive
  @app.use Rack::ConditionalGet
  @app.use Rack::ETag
  
  @app.use Rack::CommonLogger
  @app.use Rack::ShowExceptions

  component_paths = @component_paths
  @app.map '/components' do
    run ComponentHandler.new(component_paths)
  end
  
  # Serve the opal files
  opal_files = OpalFiles.new(@app, @app_path, @component_paths)

  # Serve the main html files from public, also figure out
  # which JS/CSS files to serve.
  @app.use IndexFiles, @component_paths, opal_files
  
  # Handle socks js connection
  if RUBY_PLATFORM != 'java'
    component_paths.require_in_components
    SocketConnectionHandler.dispatcher = Dispatcher.new
    
    @app.map "/channel" do
      run Rack::SockJS.new(SocketConnectionHandler)#, :websocket => false
    end
  end
  
  @app.use Rack::Static,
    :urls => ["/"],
    :root => "public",
    :index => "",
    :header_rules => [
      [:all, {'Cache-Control' => 'public, max-age=86400'}]
    ]

  @app.run lambda{ |env| [ 404, { 'Content-Type'  => 'text/html' }, ['404 - page not found'] ] }
  
  return @app
end

#display_welcomeObject



59
60
61
# File 'lib/volt/server.rb', line 59

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

#setup_change_listenerObject



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

def setup_change_listener
  # Setup the listeners for file changes
  listener = Listen.to("#{@app_path}/") do |modified, added, removed|
    SocketConnectionHandler.send_message_all(nil, 'reload')
  end
  listener.start
end