MCPcopy Index your code
hub / github.com/codemodus/chain

github.com/codemodus/chain @v2.1.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.1.2 ↗ · + Follow
30 symbols 104 edges 5 files 8 documented · 27%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Core symbols most depended-on inside this repo

New
called by 9
chain.go
Append
called by 9
chain.go
EndFn
called by 9
chain.go
Merge
called by 4
chain.go
End
called by 3
chain.go
appendHandlers
called by 2
chain.go
Copy
called by 1
chain.go
emptyHandler
called by 0
chain.go

Shape

Function 24
Method 5
Struct 1

Languages

Go100%

Modules by API surface

chain_unit_test.go10 symbols
chain.go9 symbols
chain_example_test.go5 symbols
chain_func_test.go4 symbols
chain_bench_test.go2 symbols

For agents

$ claude mcp add chain \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact