Sha256: d2f786fdfc49b6db719f3fcedb46cfcc73db3a228025be26da68a1e946e0288c

Contents?: true

Size: 1000 Bytes

Versions: 1

Compression:

Stored size: 1000 Bytes

Contents

require 'rack'
require File.dirname(__FILE__) + '/rack_logger'

module Tres
  class Server
    attr_accessor :app

    def initialize app
      @app = app
      @static_server = Rack::File.new @app.root/'build'
    end

    def call env
      response = serve_static env
      response = serve_asset env if not_found?(response)
      response = serve_index if not_found?(response)
      response
    end

    def to_rack_app
      me = self
      Rack::Builder.new do
        use ::Tres::RackLogger
        use Rack::Lint
        run me
      end      
    end

    private
    def serve_static env
      @static_server.call env
    end

    def serve_asset env
      @app.asset_packager.sprockets.call env
    end

    def serve_index 
      [ 200, { 'Content-Type' => 'text/html' }, File.open(@app.root/'build'/'index.html') ]
    end

    def not_found
      [ 404, { 'Content-Type' => 'text/plain' }, ['Not found'] ]
    end

    def not_found? response
      response[0] == 404
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tres-0.1.0 lib/tres/server.rb