Module: Shellac

Defined in:
lib/shellac.rb,
lib/shellac/config.rb,
lib/shellac/version.rb,
lib/shellac/application.rb,
lib/shellac/config/task.rb,
lib/shellac/storage_engine.rb,
lib/shellac/config/tasklist.rb,
lib/shellac/storage_engine/hash.rb

Defined Under Namespace

Classes: ApplicationHelpers, ConfigClass, Launcher, Storage_engine

Constant Summary collapse

Config =
ConfigClass.new
VERSION =
"0.1.1"
Application =
lambda { |env|
  hdrs = Shellac::ApplicationHelpers.request_headers( env )
  hdrs.delete("Host")
  response = nil

  original_url = ::Rack::Request.new( env ).url

  if Shellac::Config[:routes].has_key?( env["SERVER_NAME"] )
    Shellac::Config[:routes][ env["SERVER_NAME"] ].each do |route|
      if cached_response = Shellac::Config[:storageengine]["response:#{original_url}"]
        response = cached_response
      elsif to_url = route[:func].call(original_url, route[:regexp])
        fetched_response = HTTP.request(
          env["REQUEST_METHOD"].downcase.intern,
          to_url,
          HTTP.default_options.with_headers( hdrs )
        )
        fetched_body = fetched_response.body
        body = []
        if fetched_response.chunked?
          while partial = fetched_body.readpartial
            body << ( partial.bytesize.to_s(16) << "\r\n" << partial << "\r\n")
          end
          body << "0\r\n\r\n"
        else
          while partial = fetched_body.readpartial
            body << partial
          end
        end
        # Make this sexy and make it so that the exact caching key can be
        # specified instead of just crude path based caching.

        response = [
          fetched_response.status.code,
          fetched_response.headers.to_h,
          body
        ]
        Shellac::Config[:storageengine]["response:#{original_url}"] = response
        break
      end
    end
  end

  if response
    response
  else
    [200, {}, ["undefined"]]
  end
}