-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.go
155 lines (135 loc) · 4.28 KB
/
module.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
package api
import (
"context"
"encoding/json"
"net/http"
"runtime/debug"
"strings"
"time"
"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/payments/internal/app/connectors/bankingcircle"
"github.com/formancehq/payments/internal/app/connectors/currencycloud"
"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/oauth2/oauth2introspect"
"github.com/formancehq/go-libs/otlp"
"github.com/formancehq/payments/internal/app/connectors/dummypay"
"github.com/formancehq/payments/internal/app/connectors/modulr"
"github.com/formancehq/payments/internal/app/connectors/stripe"
"github.com/formancehq/payments/internal/app/connectors/wise"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go.uber.org/fx"
)
//nolint:gosec // false positive
const (
otelTracesFlag = "otel-traces"
authBasicEnabledFlag = "auth-basic-enabled"
authBasicCredentialsFlag = "auth-basic-credentials"
authBearerEnabledFlag = "auth-bearer-enabled"
authBearerIntrospectURLFlag = "auth-bearer-introspect-url"
authBearerAudienceFlag = "auth-bearer-audience"
authBearerAudiencesWildcardFlag = "auth-bearer-audiences-wildcard"
serviceName = "Payments"
)
func HTTPModule() fx.Option {
return fx.Options(
fx.Invoke(func(m *mux.Router, lc fx.Lifecycle) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
//nolint:gomnd // allow timeout values
srv := &http.Server{
Handler: m,
Addr: "0.0.0.0:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}()
return nil
},
})
}),
fx.Provide(fx.Annotate(httpRouter, fx.ParamTags(``, `group:"connectorHandlers"`))),
addConnector[dummypay.Config](dummypay.NewLoader()),
addConnector[modulr.Config](modulr.NewLoader()),
addConnector[stripe.Config](stripe.NewLoader()),
addConnector[wise.Config](wise.NewLoader()),
addConnector[currencycloud.Config](currencycloud.NewLoader()),
addConnector[bankingcircle.Config](bankingcircle.NewLoader()),
)
}
func httpRecoveryFunc(ctx context.Context, e interface{}) {
if viper.GetBool(otelTracesFlag) {
otlp.RecordAsError(ctx, e)
} else {
logrus.Errorln(e)
debug.PrintStack()
}
}
func httpCorsHandler() func(http.Handler) http.Handler {
return cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut},
AllowCredentials: true,
}).Handler
}
func httpServeFunc(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
handler.ServeHTTP(w, r)
})
}
func sharedAuthMethods() []auth.Method {
methods := make([]auth.Method, 0)
if viper.GetBool(authBasicEnabledFlag) {
credentials := auth.Credentials{}
for _, kv := range viper.GetStringSlice(authBasicCredentialsFlag) {
parts := strings.SplitN(kv, ":", 2)
credentials[parts[0]] = auth.Credential{
Password: parts[1],
}
}
methods = append(methods, auth.NewHTTPBasicMethod(credentials))
}
if viper.GetBool(authBearerEnabledFlag) {
methods = append(methods, auth.NewHttpBearerMethod(
auth.NewIntrospectionValidator(
oauth2introspect.NewIntrospecter(viper.GetString(authBearerIntrospectURLFlag)),
viper.GetBool(authBearerAudiencesWildcardFlag),
auth.AudienceIn(viper.GetStringSlice(authBearerAudienceFlag)...),
),
))
}
return methods
}
func handleServerError(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(http.StatusInternalServerError)
logging.GetLogger(r.Context()).Error(err)
// TODO: Opentracing
err = json.NewEncoder(w).Encode(api.ErrorResponse{
ErrorCode: "INTERNAL",
ErrorMessage: err.Error(),
})
if err != nil {
panic(err)
}
}
func handleValidationError(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(http.StatusBadRequest)
logging.GetLogger(r.Context()).Error(err)
// TODO: Opentracing
err = json.NewEncoder(w).Encode(api.ErrorResponse{
ErrorCode: "VALIDATION",
ErrorMessage: err.Error(),
})
if err != nil {
panic(err)
}
}