-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
232 lines (204 loc) · 5.69 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"fmt"
"io"
_ "net/http/pprof"
"os"
"runtime"
"time"
"github.com/xmidt-org/ancla"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/arrange/arrangepprof"
"github.com/xmidt-org/touchstone"
"github.com/xmidt-org/touchstone/touchhttp"
"go.uber.org/fx"
"go.uber.org/zap"
"github.com/goph/emperror"
"github.com/spf13/pflag"
"github.com/xmidt-org/candlelight"
)
// convenient global values
const (
DefaultKeyID = "current"
apiVersion = "v3"
prevAPIVersion = "v2"
applicationName = "tr1d1um"
apiBase = "api/" + apiVersion
prevAPIBase = "api/" + prevAPIVersion
apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}"
)
const (
translationServicesKey = "supportedServices"
targetURLKey = "targetURL"
netDialerTimeoutKey = "netDialerTimeout"
clientTimeoutKey = "clientTimeout"
reqTimeoutKey = "respWaitTimeout"
reqRetryIntervalKey = "requestRetryInterval"
reqMaxRetriesKey = "requestMaxRetries"
wrpSourceKey = "WRPSource"
hooksSchemeKey = "hooksScheme"
reducedTransactionLoggingCodesKey = "logging.reducedLoggingResponseCodes"
authAcquirerKey = "authAcquirer"
webhookConfigKey = "webhook"
tracingConfigKey = "tracing"
)
var (
// dynamic versioning
Version string
Date string
Commit string
)
var defaults = map[string]interface{}{
translationServicesKey: []string{}, // no services allowed by the default
targetURLKey: "localhost:6000",
netDialerTimeoutKey: "5s",
clientTimeoutKey: "50s",
reqTimeoutKey: "40s",
reqRetryIntervalKey: "2s",
reqMaxRetriesKey: 2,
wrpSourceKey: "dns:localhost",
hooksSchemeKey: "https",
}
type XmidtClientTimeoutConfigIn struct {
fx.In
XmidtClientTimeout httpClientTimeout `name:"xmidtClientTimeout"`
}
type ArgusClientTimeoutConfigIn struct {
fx.In
ArgusClientTimeout httpClientTimeout `name:"argusClientTimeout"`
}
type TracingConfigIn struct {
fx.In
TracingConfig candlelight.Config
Logger *zap.Logger
}
type ConstOut struct {
fx.Out
DefaultKeyID string `name:"default_key_id"`
}
func consts() ConstOut {
return ConstOut{
DefaultKeyID: DefaultKeyID,
}
}
func configureXmidtClientTimeout(in XmidtClientTimeoutConfigIn) httpClientTimeout {
xct := in.XmidtClientTimeout
if xct.ClientTimeout == 0 {
xct.ClientTimeout = time.Second * 135
}
if xct.NetDialerTimeout == 0 {
xct.NetDialerTimeout = time.Second * 5
}
if xct.RequestTimeout == 0 {
xct.RequestTimeout = time.Second * 129
}
return xct
}
func configureArgusClientTimeout(in ArgusClientTimeoutConfigIn) httpClientTimeout {
act := in.ArgusClientTimeout
if act.ClientTimeout == 0 {
act.ClientTimeout = time.Second * 50
}
if act.NetDialerTimeout == 0 {
act.NetDialerTimeout = time.Second * 5
}
return act
}
func loadTracing(in TracingConfigIn) (candlelight.Tracing, error) {
traceConfig := in.TracingConfig
traceConfig.ApplicationName = applicationName
tracing, err := candlelight.New(traceConfig)
if err != nil {
return candlelight.Tracing{}, err
}
in.Logger.Info("tracing status", zap.Bool("enabled", !tracing.IsNoop()))
return tracing, nil
}
func printVersion(f *pflag.FlagSet, arguments []string) (bool, error) {
if err := f.Parse(arguments); err != nil {
return true, err
}
if pVersion, _ := f.GetBool("version"); pVersion {
printVersionInfo(os.Stdout)
return true, nil
}
return false, nil
}
func printVersionInfo(writer io.Writer) {
fmt.Fprintf(writer, "%s:\n", applicationName)
fmt.Fprintf(writer, " version: \t%s\n", Version)
fmt.Fprintf(writer, " go version: \t%s\n", runtime.Version())
fmt.Fprintf(writer, " built time: \t%s\n", Date)
fmt.Fprintf(writer, " git commit: \t%s\n", Commit)
fmt.Fprintf(writer, " os/arch: \t%s/%s\n", runtime.GOOS, runtime.GOARCH)
}
func exitIfError(logger *zap.Logger, err error) {
if err != nil {
if logger != nil {
logger.Error("failed to parse arguments", zap.Error(err))
}
fmt.Fprintf(os.Stderr, "Error: %#v\n", err.Error())
os.Exit(1)
}
}
//nolint:funlen
func tr1d1um(arguments []string) (exitCode int) {
v, l, f, err := setup(arguments)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
// This allows us to communicate the version of the binary upon request.
if done, parseErr := printVersion(f, arguments); done {
// if we're done, we're exiting no matter what
exitIfError(l, emperror.Wrap(parseErr, "failed to parse arguments"))
os.Exit(0)
}
app := fx.New(
arrange.LoggerFunc(l.Sugar().Infof),
fx.Supply(l),
fx.Supply(v),
arrange.ForViper(v),
arrange.ProvideKey("xmidtClientTimeout", httpClientTimeout{}),
arrange.ProvideKey("argusClientTimeout", httpClientTimeout{}),
touchstone.Provide(),
touchhttp.Provide(),
ancla.ProvideMetrics(),
arrangepprof.HTTP{
RouterName: "server_pprof",
}.Provide(),
fx.Provide(
consts,
arrange.UnmarshalKey(tracingConfigKey, candlelight.Config{}),
fx.Annotated{
Name: "xmidt_client_timeout",
Target: configureXmidtClientTimeout,
},
fx.Annotated{
Name: "argus_client_timeout",
Target: configureArgusClientTimeout,
},
loadTracing,
newHTTPClient,
),
provideAuthChain("authx.inbound"),
provideServers(),
provideHandlers(),
)
switch err := app.Err(); {
case errors.Is(err, pflag.ErrHelp):
return
case err == nil:
app.Run()
default:
fmt.Fprintln(os.Stderr, err)
return 2
}
return 0
}
func main() {
os.Exit(tr1d1um(os.Args))
}