Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
Middleware is a chainable behavior modifier for handlers.
func Chain ¶
func Chain(mws ...Middleware) Middleware
Chain returns a middleware that chains the passed middlewares.
Example ¶
package main import ( "context" "fmt" ) func main() { e := Chain( annotate("first"), annotate("second"), annotate("third"), )(myHandler) if _, err := e(ctx, req); err != nil { panic(err) } } var ( ctx = context.Background() req = struct{}{} ) func annotate(s string) Middleware { return func(next Handler) Handler { return func(ctx context.Context, request interface{}) (interface{}, error) { fmt.Println(s, "pre") defer fmt.Println(s, "post") return next(ctx, request) } } } func myHandler(context.Context, interface{}) (interface{}, error) { fmt.Println("my handler!") return struct{}{}, nil }
Output: first pre second pre third pre my handler! third post second post first post
Click to show internal directories.
Click to hide internal directories.