-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathproxy.go
104 lines (86 loc) · 3.19 KB
/
proxy.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: Apache-2.0
/*
Package proxy provides proxy and proxy middleware interfaces and implementations.
*/
package proxy
import (
"context"
"errors"
"io"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
// Namespace to be used in extra config
const Namespace = "github.com/devopsfaith/krakend/proxy"
// Metadata is the Metadata of the Response which contains Headers and StatusCode
type Metadata struct {
Headers map[string][]string
StatusCode int
}
// Response is the entity returned by the proxy
type Response struct {
Data map[string]interface{}
IsComplete bool
Metadata Metadata
Io io.Reader
}
// readCloserWrapper is Io.Reader which is closed when the Context is closed or canceled
type readCloserWrapper struct {
ctx context.Context
rc io.ReadCloser
}
// NewReadCloserWrapper Creates a new closeable io.Read
func NewReadCloserWrapper(ctx context.Context, in io.ReadCloser) io.Reader {
wrapper := readCloserWrapper{ctx, in}
go wrapper.closeOnCancel()
return wrapper
}
func (w readCloserWrapper) Read(b []byte) (int, error) {
return w.rc.Read(b)
}
// closeOnCancel closes the io.Reader when the context is Done
func (w readCloserWrapper) closeOnCancel() {
<-w.ctx.Done()
w.rc.Close()
}
var (
// ErrNoBackends is the error returned when an endpoint has no backends defined
ErrNoBackends = errors.New("all endpoints must have at least one backend")
// ErrTooManyBackends is the error returned when an endpoint has too many backends defined
ErrTooManyBackends = errors.New("too many backends for this proxy")
// ErrTooManyProxies is the error returned when a middleware has too many proxies defined
ErrTooManyProxies = errors.New("too many proxies for this proxy middleware")
// ErrNotEnoughProxies is the error returned when an endpoint has not enough proxies defined
ErrNotEnoughProxies = errors.New("not enough proxies for this endpoint")
)
// Proxy processes a request in a given context and returns a response and an error
type Proxy func(ctx context.Context, request *Request) (*Response, error)
// BackendFactory creates a proxy based on the received backend configuration
type BackendFactory func(remote *config.Backend) Proxy
// Middleware adds a middleware, decorator or wrapper over a collection of proxies,
// exposing a proxy interface.
//
// Proxy middlewares can be stacked:
//
// var p Proxy
// p := EmptyMiddleware(NoopProxy)
// response, err := p(ctx, r)
type Middleware func(next ...Proxy) Proxy
// EmptyMiddlewareWithLoggger is a dummy middleware, useful for testing and fallback
func EmptyMiddlewareWithLogger(logger logging.Logger, next ...Proxy) Proxy {
if len(next) > 1 {
logger.Fatal("too many proxies for this proxy middleware: EmptyMiddleware only accepts 1 proxy, got %d", len(next))
return nil
}
return next[0]
}
func EmptyMiddleware(next ...Proxy) Proxy {
return EmptyMiddlewareWithLogger(logging.NoOp, next...)
}
func emptyMiddlewareFallback(logger logging.Logger) Middleware {
return func(next ...Proxy) Proxy {
return EmptyMiddlewareWithLogger(logger, next...)
}
}
// NoopProxy is a do nothing proxy, useful for testing
func NoopProxy(_ context.Context, _ *Request) (*Response, error) { return nil, nil }