// Chain is the helper function for composing middlewares // in specific order func (wares Middlewares) Chain() endpoint.Middleware { // if no middleware, return an passthrought middleware if wares.Len() == 0 { return func(inner endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (response interface{}, err error) { return inner(ctx, request) } } } // sort the middlewares slice := wares.Slice() return endpoint.Chain(slice[0], slice[1:]...) }
func ExampleChain() { e := endpoint.Chain( annotate("first"), annotate("second"), annotate("third"), )(myEndpoint) if _, err := e(ctx, req); err != nil { panic(err) } // Output: // first pre // second pre // third pre // my endpoint! // third post // second post // first post }