ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - codemodus/chain: Composable chains of nested http.Handler instances.
Composable chains of nested http.Handler instances. - codemodus/chain
Visit Site

GitHub - codemodus/chain: Composable chains of nested http.Handler instances.

GitHub - codemodus/chain: 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. If your nested handler order does not need to be composable, please do not use this or any similar package and avoid adding a dependency to your project.

Usage

type Chain
    func New(handlers ...func(http.Handler) http.Handler) *Chain
    func (c *Chain) Append(handlers ...func(http.Handler) http.Handler) *Chain
    func (c *Chain) Copy(chain *Chain)
    func (c *Chain) End(handler http.Handler) http.Handler
    func (c *Chain) EndFn(handlerFunc http.HandlerFunc) http.Handler
    func (c *Chain) Merge(chains ...*Chain) *Chain

Setup

import (
    // ...

    "github.com/codemodus/chain"
)

func main() {
    // ...

  	// Nested handlers write either "0" or "1" to the response body before
	// and after ServeHTTP() is called.
	//
	// endHandler writes "_END_" to the response body.

	ch00 := New(nestedHandler0, nestedHandler0)
	ch001 := ch00.Append(nestedHandler1)

	ch1 := New(nestedHandler1)
	ch1001 := ch1.Merge(ch001)

	mux := http.NewServeMux()
	mux.Handle("/00_End", ch00.EndFn(endHandler))     // Resp Body: "00_END_00"
	mux.Handle("/001_End", ch001.EndFn(endHandler))   // Resp Body: "001_END_100"
	mux.Handle("/1001_End", ch1001.EndFn(endHandler)) // Resp Body: "1001_END_1001"

    // ...
}

Nestable http.Handler

func nestableHandler(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // ...
        
        next.ServeHTTP(w, r)
    	
        // ...
    })
}

More Info

Changes in go1.7+/chain2.0+

As of Go 1.7, the http package's Request type includes a field (accessed via the Context() method) which holds an implementation of context.Context. Further, the context package has been added to the standard library. There is now no need for the custom Handler defined in previous versions of chain. Please refer to the following command to ease the process of updating your source.

sed -r -e 's/chain\.Handler/http.Handler/g' \
    -e 's/[a-zA-Z0-9]+ context\.Context, ([a-zA-Z0-9]+) (http\.ResponseWriter)/\1 \2/' \
    -e 's/ServeHTTPContext\([a-zA-Z0-9]+, /ServeHTTP(/'

Beyond this, any usage of chain.Set(context.Context) will need to be modified manually. Adding the affected logic as a nested handler is a simple and effective alternative. Don't forget to run gofmt/goimports.

Documentation

View the GoDoc

Benchmarks

These results are for comparison of normally nested functions, and chained functions. Each benchmark includes 10 functions prior to the final handler.

go1.7
benchmark             iter      time/iter   bytes alloc         allocs
---------             ----      ---------   -----------         ------
BenchmarkChain10     20000    61.01 μs/op     3684 B/op   51 allocs/op
BenchmarkChain10-4   20000    68.62 μs/op     3691 B/op   51 allocs/op
BenchmarkChain10-8   20000    69.33 μs/op     3696 B/op   51 allocs/op
BenchmarkNest10      20000    60.36 μs/op     3684 B/op   51 allocs/op
BenchmarkNest10-4    20000    70.82 μs/op     3692 B/op   51 allocs/op
BenchmarkNest10-8    20000    71.03 μs/op     3697 B/op   51 allocs/op

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory