Module: Vitrine::Server

Defined in:
lib/server.rb

Constant Summary collapse

DEFAULTS =
{ root: Dir.getwd, port: 9292, :address => '127.0.0.1' }

Class Method Summary collapse

Class Method Details

.build_app(options) ⇒ Object

Builds the Rack application with all the wrappers



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/server.rb', line 21

def self.build_app(options)
  Rack::Builder.new do
    use Rack::ShowStatus
    use Rack::ShowExceptions
    
    guardfile_path = options[:root] + '/Guardfile'
    if File.exist?(guardfile_path)
      $stderr.puts "Attaching LiveReload via Guardfile at #{guardfile_path.inspect}"
      # Assume livereload is engaged
      use Rack::LiveReload
    else
      $stderr.puts "No Guardfile found, so there won't be any livereload injection"
    end

    vitrine = Vitrine::App.new
    vitrine.settings.set :root, options[:root]
    run vitrine
  end
end

.check_dirs_present!(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/server.rb', line 7

def self.check_dirs_present!(options)
  views = options[:root] + '/views'
  unless File.exist?(views) and File.directory?(views)
    $stderr.puts "WARNING: `views' directory not found under the current tree, you might want to create it"
  end
  
  public_folder = options[:root] + '/public'
  unless File.exist?(public_folder) and File.directory?(public_folder)
    $stderr.puts "ERROR: `public' directory not found under the current tree, you should create it. Vitrine won't run without it"
    exit 1
  end
end

.start(passed_options = {}) ⇒ Object

Run the server, largely stolen from Serve



69
70
71
72
73
74
75
76
# File 'lib/server.rb', line 69

def self.start(passed_options = {})
  options = DEFAULTS.merge(passed_options)
  check_dirs_present!(options)
  
  $stderr.puts "Vitrine v.#{Vitrine::VERSION} booting in dev mode"
  app = build_app(options)
  start_server(app, options)
end

.start_server(app, options) ⇒ Object

Pick a server handler engine and run the passed app on it, honoring the passed options



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/server.rb', line 43

def self.start_server(app, options)
  begin
    # Try Thin
    thin = Rack::Handler.get('thin')
    thin.run app, :Port => options[:port], :Host => options[:address] do |server|
      puts "Thin #{Thin::VERSION::STRING} available at http://#{options[:address]}:#{options[:port]}"
    end
  rescue LoadError
    begin
      # Then Mongrel
      mongrel = Rack::Handler.get('mongrel')
      mongrel.run app, :Port => options[:port], :Host => options[:address] do |server|
        puts "Mongrel #{Mongrel::Const::MONGREL_VERSION} available at http://#{options[:address]}:#{options[:port]}"
      end
    rescue LoadError
      # Then WEBrick
      puts "Install Mongrel or Thin for better performance."
      webrick = Rack::Handler.get('webrick')
      webrick.run app, :Port => options[:port], :Host => options[:address] do |server|
        trap("INT") { server.shutdown }
      end
    end
  end
end