lib/pieces/server.rb in pieces-0.4.5 vs lib/pieces/server.rb in pieces-0.5.0
- old
+ new
@@ -3,45 +3,49 @@
require 'sprockets'
require 'pieces/listener'
module Pieces
class Server < Rack::Server
- attr_reader :path
+ def self.start(config = {})
+ new(Config.new(config)).start
+ end
- def initialize(options = {})
- @path = options[:path] || Dir.pwd
- super
+ attr_reader :config
+
+ def initialize(config)
+ @config = config
+ super({})
end
def start
- Pieces::Listener.new(path: path).listen
+ Pieces::Listener.new(config).listen
super
end
def sprockets_env
- Sprockets::Environment.new.tap do |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
def app
- urls = files_to_serve(path)
- build_path = "#{path}/build"
+ urls = files_to_serve(config.path)
+ build_path = "#{config.path}/build"
assets_app = sprockets_env
Rack::Builder.app do
use Rack::Reloader
- use Rack::Static, urls: urls, root: build_path, index: 'index.html'
+ use Rack::Static, urls: [''], root: build_path, index: 'index.html'
map('/assets') { run assets_app } unless defined? ::Rails
run Proc.new { |env| [404, {}, ['Not found']] }
end
end
private
def files_to_serve(path)
- Dir["#{path}/build/**/*"].map { |file| file.sub("#{path}/build", '') }
+ Dir["#{config.path}/build/**/*"].map { |file| file.sub("#{config.path}/build", '') }
end
end
end