Sha256: 4e4437fb1ed7b06987a1fbf081a7a7ac1ca09fabd009a9280df173849940754b

Contents?: true

Size: 901 Bytes

Versions: 5

Compression:

Stored size: 901 Bytes

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'cotton_tail'

app = CottonTail::App.new

upcase = lambda { |(env, req, res)|
  res[:body] = res.body.upcase
  [env, req, res]
}

print = lambda { |(env, req, res)|
  puts res.body
  [env, req, res]
}

Interceptor = Class.new do
  def initialize(app)
    @app = app
  end

  def call(msg)
    _env, req, _res = msg
    if req.routing_key == 'intercept.with.middleware'
      puts 'INTERCEPTED!'
      return
    end

    @app.call msg
  end
end

app.config.middleware do |d|
  d.insert_before CottonTail::Middleware::Router, Interceptor
  d.use upcase
  d.use print
end

app.routes.draw do
  queue 'hello_world_queue', exclusive: true do
    handle 'say.hello' do
      'Hello world!'
    end

    handle 'say.goodbye' do
      'Goodbye cruel world!'
    end

    bind 'intercept.with.middleware'
  end
end

app.start

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cotton-tail-0.7.0 examples/app_with_middleware.rb
cotton-tail-0.6.1 examples/app_with_middleware.rb
cotton-tail-0.6.0 examples/app_with_middleware.rb
cotton-tail-0.5.0 examples/app_with_middleware.rb
cotton-tail-0.4.1 examples/app_with_middleware.rb