Coder Social home page Coder Social logo

wrap's Introduction

wrap

Package wrap creates a fast and flexible middleware stack for http.Handlers.

Build Status Coverage Status GoDoc Total views

Features

  • small; core is only 13 LOC
  • based on http.Handler interface; integrates fine with net/http
  • middleware stacks are http.Handlers too and may be embedded
  • has a solution for per request context sharing
  • freely mix middleware with and without context (same interface)
  • has debugging helper
  • low memory footprint
  • fast
  • easy to create adapters / wrappers for 3rd party middleware

How does it work

Import it via gopkg.in: import "gopkg.in/go-on/wrap.v2".

wrap.New(w ...Wrapper) creates a stack of middlewares. Wrapper is defined as

type Wrapper interface {
    Wrap(next http.Handler) (previous http.Handler)
}

Each wrapper wraps the the http.Handler that comes further down the middleware stack and returns a http.Handler that handles the request previously.

Examples

See example_test.go for a simple example without context and example_context_test.go for an example with context sharing.

Also look into the repository of blessed middleware github.com/go-on/wrap-contrib/wraps.

Middleware

more examples and middleware and can be found at github.com/go-on/wrap-contrib

Router

A router that is also tested but may change, can be found at github.com/go-on/router

Benchmarks (go 1.3)

// The overhead of n writes to http.ResponseWriter via n wrappers
// vs n writes in a loop within a single http.Handler

BenchmarkServing2Simple     1000000 1067    ns/op   1,00x
BenchmarkServing2Wrappers   1000000 1121    ns/op   1,05x

BenchmarkServing50Simple    100000  26041   ns/op   1,00x
BenchmarkServing50Wrappers  100000  27053   ns/op   1,04x

BenchmarkServing100Simple   50000   52074   ns/op   1,00x
BenchmarkServing100Wrappers 50000   53450   ns/op   1,03x

Credits

Initial inspiration came from Christian Neukirchen's rack for ruby some years ago.

wrap's People

Contributors

metakeule avatar

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.