lib/hanami/slice/routing/middleware/stack.rb in hanami-2.0.0.beta1.1 vs lib/hanami/slice/routing/middleware/stack.rb in hanami-2.0.0.beta2
- old
+ new
@@ -1,22 +1,37 @@
# frozen_string_literal: true
-require "rack/builder"
-
module Hanami
class Slice
module Routing
- # Hanami::Applicatione::Router middleware stack
- #
# @since 2.0.0
# @api private
module Middleware
- # Middleware stack
+ # Wraps a rack app with a middleware stack
#
+ # We use this class to add middlewares to the rack application generated
+ # from {Hanami::Slice::Router}.
+ #
+ # ```
+ # stack = Hanami::Slice::Routing::Middleware::Stack.new
+ # stack.use(Rack::ContentType, "text/html")
+ # stack.to_rack_app(a_rack_app)
+ # ```
+ #
+ # Middlewares can be mounted on specific paths:
+ #
+ # ```
+ # stack.with("/api") do
+ # stack.use(Rack::ContentType, "application/json")
+ # end
+ # ```
+ #
# @since 2.0.0
# @api private
class Stack
+ include Enumerable
+
# @since 2.0.0
# @api private
ROOT_PREFIX = "/"
private_constant :ROOT_PREFIX
@@ -75,9 +90,15 @@
end
# @since 2.0.0
# @api private
def to_rack_app(app)
+ unless Hanami.bundled?("rack")
+ raise "Add \"rack\" to your `Gemfile` to run Hanami as a rack app"
+ end
+
+ require "rack/builder"
+
s = self
Rack::Builder.new do
s.each do |prefix, stack|
s.mapped(self, prefix) do