Code Monkey home page Code Monkey logo

middleware's Introduction

Middleware

Build Status

This is a generalized library for using middleware patterns within your Ruby projects.

To get started, the best place to look is the user guide.

Installation

This project is distributed as a RubyGem:

$ gem install middleware

Usage

Once you create a basic middleware, you can use the builder to have a nice DSL to build middleware stacks. Calling the middleware is simple, as well.

# Basic middleware that just prints the inbound and
# outbound steps.
class Trace
  def initialize(app, value)
    @app   = app
    @value = value
  end

  def call(env)
    puts "--> #{@value}"
    @app.call(env)
    puts "<-- #{@value}"
  end
end

# Build the actual middleware stack which runs a sequence
# of slightly different versions of our middleware.
stack = Middleware::Builder.new do
  use Trace, "A"
  use Trace, "B"
  use Trace, "C"
end

# Run it!
stack.call(nil)

And the output:

--> A
--> B
--> C
<-- C
<-- B
<-- A

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

middleware's People

Contributors

bearded avatar craiglittle avatar ktdreyer avatar mitchellh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

middleware's Issues

Recovery out of the stack

What is the correct way to raise an error partway through the stack and detect it in the previous calls to undo things as needed?

I could wrap each @app.call(env) with begin/rescue and raise errors as needed, but that seems a little inelegant. Is there a better way?

Confusion regarding EMPTY_MIDDLEWARE

Hi, I'm a little confused about how EMPTY_MIDDLEWARE works. It's empty and therefore returns nil. Since it runs last, it makes all of my stacks return nil instead of their current env.

I'm doing the following in my code to work around this:

::Middleware::Runner.const_set :EMPTY_MIDDLEWARE, lambda { |env| env }

Am I misunderstanding something? Doing the whole stack backwards?

New gem version

Would be good to push a new version to rubygems if & when the outstanding pull requests have been accepted.

Middleware param passing fail

I'm not sure exactly what this issue is (or what to do about it), but for some reason building a stack with a parameter out of a hash dies.

require 'rubygems'
require 'middleware'

class MW1
  def initialize(app, value)
    @app = app
    @value = value
  end

  def call(env)
    puts "before"
    @value = @app.call(env)
    puts "after"
  end
end

@config = {'foo' => 'bar'}
var = @config[:var]

pass = Middleware::Builder.new do
  use MW1, var
  use MW1, 3
end

pass.call(nil)

fail = Middleware::Builder.new do
  use MW1, @config['foo']
  use MW1, 3
end

fail.call(nil)

output:

before
before
after
after
fail.rb:28: undefined method `[]' for nil:NilClass (NoMethodError)
    from /Library/Ruby/Gems/1.8/gems/middleware-0.1.0/lib/middleware/builder.rb:38:in `instance_eval'
    from /Library/Ruby/Gems/1.8/gems/middleware-0.1.0/lib/middleware/builder.rb:38:in `initialize'
    from fail.rb:27:in `new'
    from fail.rb:27

Immutable Stacks or ability to easily copy a stack

I have a project where I would like to setup a reference stack and then make a few different stacks that differ in only a small way from the reference stack. For instance:

stack = Middleware::Builder.new do
  use Trace, "A"
  use Trace, "B"
end

stack2 = stack.dup
stack2.insert_after(2, Trace, "C")

stack2.call
--> A
--> B
--> C
<-- C
<-- B
<-- A

But currently ater I run the above code and I call stack.run, it gives me the same output that stack2 gave me.

If I open a pull request to fix this would you be open to supporting this feature?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.