Sha256: fbb792b2c35d926715945360f783377c1c1b9c1d67b495fe0bba875d04364346

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

require 'sitehub/getter_setter_methods'
require 'sitehub/forward_proxy_builder'
require 'sitehub/middleware'
require 'logger'

class SiteHub
  class InvalidProxyDefinitionException < Exception
  end

  class Builder
    attr_reader :sitehub, :forward_proxies, :reverse_proxies

    include Middleware
    extend GetterSetterMethods

    getter_setters :access_logger, :error_logger
    getter_setter :sitehub_cookie_name, RECORDED_ROUTES_COOKIE

    def force_ssl(except: [])
      @force_ssl = true
      @ssl_exclusions = except
    end

    def initialize(&block)
      @forward_proxies = ForwardProxies.new
      @reverse_proxies = {}
      instance_eval(&block) if block
    end

    def build
      forward_proxies.init
      add_default_middleware
      middlewares.reverse!
      apply_middleware(forward_proxies)
    end

    def add_default_middleware
      use Middleware::ReverseProxy, reverse_proxies
      use Middleware::TransactionId
      use Middleware::ErrorHandling
      use Middleware::Logging::AccessLogger, access_logger || ::Logger.new(STDOUT)
      use Middleware::Logging::ErrorLogger, error_logger || ::Logger.new(STDERR)
      use Rack::FiberPool
      use Rack::SslEnforcer, except: @ssl_exclusions if @force_ssl
    end

    def proxy(opts = {}, &block)
      if opts.is_a?(Hash)
        mapped_path, url = *opts.to_a.flatten
      else
        mapped_path = opts
        url = nil
      end

      forward_proxies << ForwardProxyBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
                                                 url: url,
                                                 mapped_path: mapped_path,
                                                 &block)
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sitehub-0.4.8 lib/sitehub/builder.rb
sitehub-0.4.7 lib/sitehub/builder.rb
sitehub-0.4.6 lib/sitehub/builder.rb