This repository has been archived by the owner on Sep 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
79 lines (66 loc) · 1.94 KB
/
options.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
package king
import (
"context"
"net/http"
"github.com/altipla-consulting/sentry"
"github.com/juju/errors"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"github.com/altipla-consulting/king/peer"
"github.com/altipla-consulting/king/runtime"
)
func WithHttprouter(router *httprouter.Router) ServerOption {
return func(server *Server) {
server.router = router
}
}
func Debug(debug bool) ServerOption {
return func(server *Server) {
server.debug = debug
}
}
func WithLogrus() ServerOption {
return func(server *Server) {
server.errorMiddlewares = append(server.errorMiddlewares, func(ctx context.Context, appErr error) {
if server.debug {
log.WithFields(log.Fields{"err": appErr.Error()}).Error("call failed")
log.Error("Error stack:\n", errors.ErrorStack(appErr))
} else {
log.WithFields(log.Fields{"err": appErr.Error(), "stack": errors.ErrorStack(appErr)}).Error("call failed")
}
})
}
}
func WithSentry(dsn string) ServerOption {
client := sentry.NewClient(dsn)
return func(server *Server) {
server.errorMiddlewares = append(server.errorMiddlewares, func(ctx context.Context, appErr error) {
if errors.IsNotFound(appErr) {
return
}
r := peer.RequestFromContext(ctx)
client.ReportRequest(appErr, r)
})
}
}
func WithHttpClient(client *http.Client) runtime.ClientOption {
return func(caller *runtime.ClientCaller) {
caller.Client = client
}
}
func WithAuthorization(token string) runtime.ClientOption {
return func(caller *runtime.ClientCaller) {
caller.Authorization = token
}
}
func WithServerTraceOption(traceOption trace.StartOption) ServerOption {
return func(server *Server) {
server.traceOptions = append(server.traceOptions, traceOption)
}
}
func WithClientTraceOption(traceOption trace.StartOption) runtime.ClientOption {
return func(caller *runtime.ClientCaller) {
caller.TraceOptions = append(caller.TraceOptions, traceOption)
}
}