Class: ZAssets::Server

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

Constant Summary collapse

HANDLERS =
%w[puma unicorn thin webrick].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Server

Returns a new instance of Server.



7
8
9
# File 'lib/zassets/server.rb', line 7

def initialize config
  @config = config
end

Instance Method Details

#appObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zassets/server.rb', line 31

def app
  config = @config

  @app ||= Rack::Builder.app do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    use Rack::Lint

    map config[:base_url] do
      run SprocketsEnv.new(config)
    end

    map '/' do
      static_files = Rack::File.new(config[:public_path])

      if config[:public_file]
        run Rack::Cascade.new([
          static_files,
          MemoryFile.new(File.new(File.join(
            config[:public_path],
            config[:public_file]
          )))
        ])
      else
        run static_files
      end
    end
  end
end

#handlerObject



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

def handler
  Rack::Handler.get(@config[:server]) || Rack::Handler.pick(HANDLERS)
end

#optionsObject



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

def options
  @options ||= {
    environment:  :development,
    Host:         @config[:host],
    Port:         @config[:port]
  }
end

#runObject



11
12
13
14
15
16
17
# File 'lib/zassets/server.rb', line 11

def run
  handler.run app, options do |server|
    i[INT TERM].each do |sig|
      trap(sig) { server.respond_to?(:stop!) ? server.stop! : server.stop }
    end
  end
end