Go http.Hander based middleware stack with context sharing

Overview

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.

You might also like...
gorilla/csrf provides Cross Site Request Forgery (CSRF) prevention middleware for Go web applications & services 🔒

gorilla/csrf gorilla/csrf is a HTTP middleware library that provides cross-site request forgery (CSRF) protection. It includes: The csrf.Protect middl

URL Rewrite middleware for gin

Url Rewrite middleware for gin Example In this exable these urls use the same route http://localhost:1234/test-me http://localhost:1234/index.php/test

A customized middleware of DAPR.

A customized middleware of DAPR.

Gin middleware for session.

wsession Gin middleware for session management with multi-backend support: cookie-based Redis memstore Usage Start using it Download and install it: g

Fiber middleware for server-timing

Server Timing This is a Fiber middleware for the [W3C Server-Timing API] based on mitchellh/go-server-timing

echo-http - Echo http service

echo-http - Echo http service Responds with json-formatted echo of the incoming request and with a predefined message. Can be install directly (go get

HTTP/2 Apple Push Notification service (APNs) provider for Go with token-based connection

APNs Provider HTTP/2 Apple Push Notification service (APNs) provider for Go with token-based connection Example: key, err := apns.AuthKeyFromFile("Aut

Composable chains of nested http.Handler instances.

chain go get github.com/codemodus/chain Package chain aids the composition of nested http.Handler instances. Nesting functions is a simple concept. I

Add interceptors to GO http.Client

mediary Add interceptors to http.Client and you will be able to Dump request and/or response to a Log Alter your requests before they are sent or resp

Owner
go-on - web toolkit in Go
go-on - web toolkit in Go
Lightweight Middleware for net/http

MuxChain MuxChain is a small package designed to complement net/http for specifying chains of handlers. With it, you can succinctly compose layers of

Stephen Searles 209 Dec 10, 2022
Idiomatic HTTP Middleware for Golang

Negroni Notice: This is the library formerly known as github.com/codegangsta/negroni -- Github will automatically redirect requests to this repository

null 7.3k Jan 2, 2023
A tiny http middleware for Golang with added handlers for common needs.

rye A simple library to support http services. Currently, rye provides a middleware handler which can be used to chain http handlers together while pr

InVision 99 Jan 4, 2023
A collection of useful middleware for Go HTTP services & web applications 🛃

gorilla/handlers Package handlers is a collection of handlers (aka "HTTP middleware") for use with Go's net/http package (or any framework supporting

Gorilla Web Toolkit 1.5k Dec 31, 2022
Simple middleware to rate-limit HTTP requests.

Tollbooth This is a generic middleware to rate-limit HTTP requests. NOTE 1: This library is considered finished. NOTE 2: Major version changes are bac

Didip Kerabat 2.3k Dec 28, 2022
OpenID Connect (OIDC) http middleware for Go

Go OpenID Connect (OIDC) HTTP Middleware Introduction This is a middleware for http to make it easy to use OpenID Connect. Currently Supported framewo

Xenit AB 71 Jan 1, 2023
Go HTTP middleware to filter clients by IP

Go HTTP middleware to filter clients by IP

cristaltech 4 Oct 30, 2022
Chi ip banner is a chi middleware that bans some ips from your Chi http server.

Chi Ip Banner Chi ip banner is a chi middleware that bans some ips from your Chi http server. It reads a .txt file in your project's root, called bani

null 1 Jan 4, 2022
Painless middleware chaining for Go

Alice Alice provides a convenient way to chain your HTTP middleware functions and the app handler. In short, it transforms Middleware1(Middleware2(Mid

Justinas Stankevičius 2.7k Dec 26, 2022
A Go middleware that stores various information about your web application (response time, status code count, etc.)

Go stats handler stats is a net/http handler in golang reporting various metrics about your web application. This middleware has been developed and re

Florent Messa 586 Dec 10, 2022