-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
25 lines (21 loc) · 900 Bytes
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package box
import (
"context"
)
// Endpoint is the generic representation of any endpoint
// which takes a request and returns a reponse.
type Endpoint[TIn, TOut any] func(ctx context.Context, req TIn) (TOut, error)
// Middleware is an [Endpoint] middleware which can be used to wrap
// around endpoints and decorate them with auxillary functionality, like
// request logging, instrumentation, context enrichment etc.
type Middleware[TIn, TOut any] func(next Endpoint[TIn, TOut]) Endpoint[TIn, TOut]
// Chain is a convenience function to chain multiple [Middleware]s together
// and finally wraps an [Endpoint].
func Chain[TIn, TOut any](outer Middleware[TIn, TOut], others ...Middleware[TIn, TOut]) Middleware[TIn, TOut] {
return func(next Endpoint[TIn, TOut]) Endpoint[TIn, TOut] {
for i := len(others) - 1; i >= 0; i-- { // reverse
next = others[i](next)
}
return outer(next)
}
}