Class: Pieces::Server

Inherits:
Rack::Server
  • Object
show all
Defined in:
lib/pieces/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Server

Returns a new instance of Server.



14
15
16
17
# File 'lib/pieces/server.rb', line 14

def initialize(config)
  @config = config
  super({})
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/pieces/server.rb', line 12

def config
  @config
end

Class Method Details

.start(config = {}) ⇒ Object



8
9
10
# File 'lib/pieces/server.rb', line 8

def self.start(config = {})
  new(Config.new(config)).start
end

Instance Method Details

#appObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pieces/server.rb', line 32

def app
  urls = files_to_serve(config.path)
  build_path = "#{config.path}/build"
  assets_app = sprockets_env

  Rack::Builder.app do
    use Rack::Reloader
    map('/assets') { run assets_app } unless defined? ::Rails
    use Rack::Static, urls: [''], root: build_path, index: 'index.html'
    run Proc.new { |env| [404, {}, ['Not found']] }
  end
end

#sprockets_envObject



24
25
26
27
28
29
30
# File 'lib/pieces/server.rb', line 24

def sprockets_env
  Sprockets::Environment.new(config.path).tap do |env|
    env.append_path 'app/assets/javascripts'
    env.append_path 'app/assets/stylesheets'
    env.append_path 'app/views'
  end
end

#startObject



19
20
21
22
# File 'lib/pieces/server.rb', line 19

def start
  Pieces::Listener.new(config).listen
  super
end