Sha256: b7820a4de89d87dbd7a7b44ad7a8ff7cf526fc3ee6d695b858ad12a8cb4eb09f

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

require 'thread'
require 'rack/builder'
require 'hanami/router'

module Hanami
  class Container
    class Router < ::Hanami::Router
      def mount(app, options)
        app = app.new(path_prefix: options.fetch(:at)) if hanami_app?(app)
        super(app, options)
      end

      private

      def hanami_app?(app)
        app.ancestors.include? Hanami::Application
      end
    end

    attr_reader :routes

    def self.configure(options = {}, &blk)
      Mutex.new.synchronize do
        @@options       = options
        @@configuration = blk
      end
    end

    def initialize
      Mutex.new.synchronize do
        assert_configuration_presence!
        prepare_middleware_stack!
      end
    end

    def call(env)
      @builder.call(env)
    end

    private
    def assert_configuration_presence!
      unless self.class.class_variable_defined?(:@@configuration)
        raise ArgumentError.new("#{ self.class } doesn't have any application mounted.")
      end
    end

    def prepare_middleware_stack!
      @builder = ::Rack::Builder.new
      @routes  = Router.new(&@@configuration)

      if Hanami.environment.serve_static_assets?
        require 'hanami/static'
        @builder.use Hanami::Static
      end

      @builder.run @routes
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hanami-0.7.3 lib/hanami/container.rb
hanami-0.7.2 lib/hanami/container.rb
hanami-0.7.1 lib/hanami/container.rb
hanami-0.7.0 lib/hanami/container.rb