-
Notifications
You must be signed in to change notification settings - Fork 72
/
middleware.go
51 lines (46 loc) · 1.39 KB
/
middleware.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Package middleware provides a middleware interface for ogen.
package middleware
import (
"context"
"net/http"
)
// Request is request context type for middleware.
type Request struct {
// Context is request context.
Context context.Context
// OperationName is the ogen operation name. It is guaranteed to be unique and not empty.
OperationName string
// OperationID is the spec operation ID, if any.
OperationID string
// Body is the operation request body. May be nil, if the operation has not body.
Body any
// Params is the operation parameters.
Params map[string]any
// Raw is the raw http request.
Raw *http.Request
}
// Response is response type for middleware.
type Response struct {
// Type is the operation response type.
Type any
}
type (
// Next is the next middleware/handler in the chain.
Next = func(req Request) (Response, error)
// Middleware is middleware type.
Middleware func(req Request, next Next) (Response, error)
)
// ChainMiddlewares chains middlewares into a single middleware, which will be executed in the order they are passed.
func ChainMiddlewares(m ...Middleware) Middleware {
if len(m) == 0 {
return func(req Request, next Next) (Response, error) {
return next(req)
}
}
tail := ChainMiddlewares(m[1:]...)
return func(req Request, next Next) (Response, error) {
return m[0](req, func(req Request) (Response, error) {
return tail(req, next)
})
}
}