Class: Brite::Server

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

Overview

Brite::Server is a Rack-based server useful for testing sites locally. Most sites cannot be fully previewed by loading static files into a browser. Rather, a webserver is required to render and navigate a site completely. So this light server is provided to facilitate this.

Defined Under Namespace

Classes: Index

Constant Summary collapse

RACK_FILE =

Rack configuration file.

'brite.ru'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Server

Setup new instance of Brite::Server.



35
36
37
38
39
40
41
42
43
44
# File 'lib/brite/server.rb', line 35

def initialize(argv)
  @options = ::Rack::Server::Options.new.parse!(argv)

  @root = argv.first || Dir.pwd

  @options[:app] = app
  @options[:pid] = "#{tmp_dir}/pids/server.pid"

  @options[:Port] ||= '4444'
end

Instance Attribute Details

#optionsObject (readonly)

Server options, parsed from command line.



32
33
34
# File 'lib/brite/server.rb', line 32

def options
  @options
end

Class Method Details

.start(argv) ⇒ Object



27
28
29
# File 'lib/brite/server.rb', line 27

def self.start(argv)
  new(argv).start
end

Instance Method Details

#appObject

If the site has a ‘brite.ru` file, that will be used to start the server, otherwise a standard Rack::Directory server ise used.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/brite/server.rb', line 90

def app
  @app ||= (
    if ::File.exist?(rack_file)
      app, options = Rack::Builder.parse_file(rack_file, opt_parser)
      @options.merge!(options)
      app
    else
      root = self.root
      Rack::Builder.new do
        use Index, root
        run Rack::Directory.new("#{root}")
      end
    end
  )
end

#configObject

Load Brite configuration.



74
75
76
# File 'lib/brite/server.rb', line 74

def config
  @config ||= Brite::Config.new(root)
end

#ensure_brite_siteObject

Ensure root is a Brite Site.



67
68
69
70
71
# File 'lib/brite/server.rb', line 67

def ensure_brite_site
  return true if File.exist?(rack_file)
  return true if config.file
  abort "Not a brite site."
end

#rack_fileObject

Configuration file for server.



84
85
86
# File 'lib/brite/server.rb', line 84

def rack_file
  RACK_FILE
end

#rootObject

Site root directory.



79
80
81
# File 'lib/brite/server.rb', line 79

def root
  @root
end

#startObject

Start the server.



55
56
57
58
59
60
61
62
63
64
# File 'lib/brite/server.rb', line 55

def start
  ensure_brite_site

  # create required tmp directories if not found
  %w(cache pids sessions sockets).each do |dir_to_make|
    FileUtils.mkdir_p(File.join(tmp_dir, dir_to_make))
  end

  ::Rack::Server.start(options)
end

#tmp_dirObject

Temporary directory used by the rack server.



50
51
52
# File 'lib/brite/server.rb', line 50

def tmp_dir
  @tmp_dir ||= File.join(Dir.tmpdir, 'brite', root)
end