Sha256: b3ed8f705809a9b80cf3279bff577c35ff11a5b4ed8b2e9409d62ddb4661340f

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'sitehub/collection_methods'
require 'sitehub/candidate_routes'
require 'forwardable'

class SiteHub
  class InvalidProxyDefinitionException < StandardError
  end

  class ConfigError < StandardError
  end

  class Core
    class << self
      # TODO: default action for missing key, throw exception?
      def from_hash(config)
        new do
          extend CollectionMethods
          sitehub_cookie_name config[:sitehub_cookie_name] if config[:sitehub_cookie_name]
          sitehub_cookie_path config[:sitehub_cookie_path] if config[:sitehub_cookie_path]

          collection!(config, :proxies).each do |proxy|
            mappings.add_route candidate_routes: CandidateRoutes.from_hash(proxy, sitehub_cookie_name, sitehub_cookie_path )
          end

          collection(config, :reverse_proxies).each do |proxy|
            reverse_proxy proxy[:downstream_url] => proxy[:path]
          end
        end
      end
    end

    include Equality
    extend Forwardable

    attr_reader :mappings, :reverse_proxies
    def_delegator :mappings, :sitehub_cookie_name
    def_delegator :mappings, :sitehub_cookie_path

    def initialize(&block)
      @reverse_proxies = {}
      @mappings = Middleware::CandidateRouteMappings.new
      instance_eval(&block) if block
    end

    def build
      Middleware::ReverseProxy.new(mappings.init, reverse_proxies)
    end

    def proxy(opts = {}, &block)
      mapped_path, url = *(opts.respond_to?(:to_a) ? opts.to_a : [opts]).flatten

      mappings.add_route(url: url,
                         mapped_path: mapped_path,
                         &block)
    end

    def reverse_proxy(hash)
      reverse_proxies.merge!(hash)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha11 lib/sitehub/core.rb