-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
273 lines (229 loc) · 7.65 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"context"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
clinic "github.com/tidepool-org/clinic/client"
"github.com/tidepool-org/go-common/clients/disc"
"github.com/tidepool-org/go-common/clients/shoreline"
"github.com/tidepool-org/go-common/errors"
"github.com/tidepool-org/go-common/events"
"github.com/tidepool-org/marketo-service/handler"
"github.com/tidepool-org/marketo-service/marketo"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
const (
keycloakUsersTopic = "keycloak.public.user_entity"
keycloakRolesTopic = "keycloak.public.user_role_mapping"
)
type Config struct {
Marketo marketo.Config `json:"marketo"`
}
type ServiceConfig struct {
ClinicsHost string `envconfig:"TIDEPOOL_CLINIC_CLIENT_ADDRESS" default:"http://clinic:8080"`
ListenAddress string `envconfig:"LISTEN_ADDRESS" default:":8080"`
ServerSecret string `envconfig:"TIDEPOOL_SERVER_SECRET" required:"true"`
ShorelineHost string `envconfig:"TIDEPOOL_SHORELINE_CLIENT_ADDRESS" default:"http://shoreline:9107"`
}
func (s *ServiceConfig) LoadFromEnv() error {
return envconfig.Process("", s)
}
func main() {
var config Config
logger := log.New(os.Stdout, "marketo-service", log.LstdFlags|log.Lshortfile)
config.Marketo.ID, _ = os.LookupEnv("MARKETO_ID")
config.Marketo.URL, _ = os.LookupEnv("MARKETO_URL")
config.Marketo.Secret, _ = os.LookupEnv("MARKETO_SECRET")
config.Marketo.ClinicRole, _ = os.LookupEnv("MARKETO_CLINIC_ROLE")
config.Marketo.PatientRole, _ = os.LookupEnv("MARKETO_PATIENT_ROLE")
unParsedTimeout, found := os.LookupEnv("MARKETO_TIMEOUT")
if found {
parsedTimeout64, err := strconv.ParseInt(unParsedTimeout, 10, 32)
parsedTimeout := uint(parsedTimeout64)
if err != nil {
logger.Println(err)
}
config.Marketo.Timeout = parsedTimeout
}
var marketoManager marketo.Manager
if err := config.Marketo.Validate(); err != nil {
//log.Fatalf("WARNING: Marketo config is invalid: %v", err)
} else {
log.Print("initializing marketo manager")
marketoManager, _ = marketo.NewManager(logger, config.Marketo)
}
serviceConfig := &ServiceConfig{}
if err := serviceConfig.LoadFromEnv(); err != nil {
log.Fatalln(err)
}
cloudEventsConfig := events.NewConfig()
if err := cloudEventsConfig.LoadFromEnv(); err != nil {
logger.Println("error loading kafka config")
log.Fatalln(err)
}
shorelineClient, err := buildShoreline(serviceConfig)
if err != nil {
log.Fatalln(err)
}
clinicService, err := buildClinicService(serviceConfig, shorelineClient)
if err != nil {
log.Fatalln(err)
}
userEventsHandler := &handler.UserEventsHandler{
Clinics: clinicService,
Shoreline: shorelineClient,
MarketoManager: marketoManager,
}
handlers := []events.EventHandler{
events.NewUserEventsHandler(userEventsHandler),
&events.DebugEventHandler{},
}
createConsumer := func() (events.MessageConsumer, error) {
return events.NewCloudEventsMessageHandler(handlers)
}
cg, err := events.NewFaultTolerantConsumerGroup(cloudEventsConfig, createConsumer)
if err != nil {
log.Fatalln(err)
}
keycloakEventsHandler := handler.KeycloakEventsHandler{
Clinics: clinicService,
MarketoManager: marketoManager,
Shoreline: shorelineClient,
}
keycloakUsersConfig := *cloudEventsConfig
keycloakUsersConfig.KafkaTopic = keycloakUsersTopic
keycloakUsersConfig.KafkaDeadLettersTopic = ""
// CDC topic use '.' separator instead of '-'
if strings.HasSuffix(keycloakUsersConfig.KafkaTopicPrefix, "-") {
keycloakUsersConfig.KafkaTopicPrefix = strings.TrimSuffix(keycloakUsersConfig.KafkaTopicPrefix, "-") + "."
}
keycloakUsersCg, err := events.NewFaultTolerantConsumerGroup(&keycloakUsersConfig, func() (events.MessageConsumer, error) {
return handler.NewKeycloakUserEventsConsumer(&keycloakEventsHandler)
})
if err != nil {
log.Fatalln(err)
}
keycloakRolesConfig := *cloudEventsConfig
keycloakRolesConfig.KafkaTopic = keycloakRolesTopic
keycloakRolesConfig.KafkaDeadLettersTopic = ""
// CDC topic use '.' separator instead of '-'
if strings.HasSuffix(keycloakRolesConfig.KafkaTopicPrefix, "-") {
keycloakRolesConfig.KafkaTopicPrefix = strings.TrimSuffix(keycloakRolesConfig.KafkaTopicPrefix, "-") + "."
}
keycloakRolesCg, err := events.NewFaultTolerantConsumerGroup(&keycloakRolesConfig, func() (events.MessageConsumer, error) {
return handler.NewKeycloakRoleEventsConsumer(&keycloakEventsHandler)
})
if err != nil {
log.Fatalln(err)
}
router := mux.NewRouter()
refreshUser := handler.RefreshUser(userEventsHandler, shorelineClient)
router.HandleFunc("/v1/users/{userId}/marketo", refreshUser).Methods("POST")
srv := &http.Server{
Addr: serviceConfig.ListenAddress,
Handler: router,
}
wg := sync.WaitGroup{}
wg.Add(4)
shutdown := make(chan struct{}, 2)
// listen to signals to stop consumer
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func(stop chan os.Signal) {
<-stop
log.Println("SIGINT or SIGTERM received")
shutdown <- struct{}{}
}(stop)
_, cancel := context.WithCancel(context.Background())
go func(wg *sync.WaitGroup) {
defer func() { shutdown <- struct{}{} }()
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Println(errors.Wrap(err, "Unable to start server"))
}
}(&wg)
go func(wg *sync.WaitGroup) {
defer func() { shutdown <- struct{}{} }()
err = cg.Start()
if err != nil {
log.Println(errors.Wrap(err, "Unable to start consumer"))
} else {
log.Println("Consumer stopped")
}
}(&wg)
go func(wg *sync.WaitGroup) {
defer func() { shutdown <- struct{}{} }()
err = keycloakUsersCg.Start()
if err != nil {
log.Println(errors.Wrap(err, "Unable to start consumer"))
} else {
log.Println("Consumer stopped")
}
}(&wg)
go func(wg *sync.WaitGroup) {
defer func() { shutdown <- struct{}{} }()
err = keycloakRolesCg.Start()
if err != nil {
log.Println(errors.Wrap(err, "Unable to start consumer"))
} else {
log.Println("Consumer stopped")
}
}(&wg)
go func(shutdown chan struct{}, cancel context.CancelFunc, wg *sync.WaitGroup) {
defer cancel()
<-shutdown
log.Println("Shutting down")
go func() {
defer wg.Done()
shutdownCtx, c := context.WithTimeout(context.Background(), time.Second*60)
defer c()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Println(errors.Wrap(err, "Unable to shutdown server"))
}
}()
go func() {
defer wg.Done()
if err := cg.Stop(); err != nil {
log.Println(errors.Wrap(err, "Unable to stop user events consumer group"))
}
}()
go func() {
defer wg.Done()
if err := keycloakUsersCg.Stop(); err != nil {
log.Println(errors.Wrap(err, "Unable to stop keycloak users consumer group"))
}
}()
go func() {
defer wg.Done()
if err := keycloakRolesCg.Stop(); err != nil {
log.Println(errors.Wrap(err, "Unable to stop keycloak roles consumer group"))
}
}()
}(shutdown, cancel, &wg)
wg.Wait()
}
func buildShoreline(config *ServiceConfig) (shoreline.Client, error) {
httpClient := &http.Client{}
client := shoreline.NewShorelineClientBuilder().
WithHostGetter(disc.NewStaticHostGetterFromString(config.ShorelineHost)).
WithHttpClient(httpClient).
WithName("marketo-service").
WithSecret(config.ServerSecret).
WithTokenRefreshInterval(time.Hour).
Build()
return client, client.Start()
}
func buildClinicService(config *ServiceConfig, shorelineClient shoreline.Client) (clinic.ClientWithResponsesInterface, error) {
opts := clinic.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Add("x-tidepool-session-token", shorelineClient.TokenProvide())
return nil
})
return clinic.NewClientWithResponses(config.ClinicsHost, opts)
}