-
Notifications
You must be signed in to change notification settings - Fork 14
/
defaultcallback.go
126 lines (109 loc) · 3.58 KB
/
defaultcallback.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package common
import (
"context"
"net/http"
"time"
"github.com/anz-bank/sysl-go/config"
"github.com/anz-bank/sysl-go/validator"
"github.com/go-chi/chi"
)
func DefaultCallback() Callback {
return Callback{
DownstreamTimeout: 120 * time.Second,
RouterBasePath: "/",
UpstreamConfig: Config{},
}
}
type Callback struct {
DownstreamTimeout time.Duration
RouterBasePath string
UpstreamConfig validator.Validator
MapErrorFunc func(ctx context.Context, err error) *HTTPError // MapErrorFunc may be left nil to use default behaviour.
WriteErrorFunc func(ctx context.Context, w http.ResponseWriter, httpError *HTTPError) // WriteError may be left nil to use default behaviour.
AddMiddlewareFunc func(ctx context.Context, r chi.Router) // AddMiddlewareFunc may be left nil to use default behaviour.
}
type Config struct{}
func (c Config) Validate() error {
return nil
}
func (g Callback) AddMiddleware(ctx context.Context, r chi.Router) {
if g.AddMiddlewareFunc != nil {
g.AddMiddlewareFunc(ctx, r)
}
}
func (g Callback) BasePath() string {
return g.RouterBasePath
}
func (g Callback) Config() interface{} {
return g.UpstreamConfig
}
func (g Callback) HandleError(ctx context.Context, w http.ResponseWriter, kind Kind, message string, cause error) {
se := CreateError(ctx, kind, message, cause)
g.MapError(ctx, se).WriteError(ctx, w)
}
func (g Callback) DownstreamTimeoutContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(ctx, g.DownstreamTimeout)
}
// MapError maps an error to an HTTPError in instances where custom error mapping is required.
// Return nil to perform default error mapping; defined as:
// 1. CustomError.HTTPError if the original error is a CustomError, otherwise
// 2. common.MapError.
func (g Callback) MapError(ctx context.Context, err error) *HTTPError {
if g.MapErrorFunc == nil {
return nil
}
return g.MapErrorFunc(ctx, err)
}
func (g Callback) WriteError(ctx context.Context, w http.ResponseWriter, httpError *HTTPError) {
if g.WriteErrorFunc == nil {
httpError.WriteError(ctx, w)
} else {
g.WriteErrorFunc(ctx, w, httpError)
}
}
func NewCallbackV3(
config *config.GenCodeConfig,
downstreamTimeOut time.Duration,
mapError func(ctx context.Context, err error) *HTTPError,
writeError func(ctx context.Context, w http.ResponseWriter, httpError *HTTPError),
addMiddleware func(ctx context.Context, r chi.Router),
) Callback {
// construct the rest configuration (aka. gen callback)
return Callback{
DownstreamTimeout: downstreamTimeOut,
RouterBasePath: config.Upstream.HTTP.BasePath,
UpstreamConfig: &config.Upstream,
MapErrorFunc: mapError,
WriteErrorFunc: writeError,
AddMiddlewareFunc: addMiddleware,
}
}
// NewCallbackV2 is deprecated, prefer NewCallbackV3.
func NewCallbackV2(
config *config.GenCodeConfig,
downstreamTimeOut time.Duration,
mapError func(ctx context.Context, err error) *HTTPError,
addMiddleware func(ctx context.Context, r chi.Router),
) Callback {
// construct the rest configuration (aka. gen callback)
return Callback{
DownstreamTimeout: downstreamTimeOut,
RouterBasePath: config.Upstream.HTTP.BasePath,
UpstreamConfig: &config.Upstream,
MapErrorFunc: mapError,
AddMiddlewareFunc: addMiddleware,
}
}
// NewCallback is deprecated, prefer NewCallbackV2.
func NewCallback(
config *config.GenCodeConfig,
downstreamTimeOut time.Duration,
mapError func(ctx context.Context, err error) *HTTPError,
) Callback {
return NewCallbackV2(
config,
downstreamTimeOut,
mapError,
nil,
)
}