This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
163 lines (138 loc) · 4.4 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
watermillmessage "github.com/ThreeDotsLabs/watermill/message"
"github.com/asyncapi/event-gateway/config"
"github.com/asyncapi/event-gateway/kafka"
"github.com/asyncapi/event-gateway/message"
"github.com/go-chi/chi/v5"
"github.com/kelseyhightower/envconfig"
"github.com/olahol/melody"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
const configPrefix = "eventgateway"
func main() {
c := config.NewApp()
if err := envconfig.Process(configPrefix, c); err != nil {
_ = envconfig.Usage(configPrefix, c)
logrus.WithError(err).Fatal()
}
if c.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
kafkaProxyConfig, err := c.ProxyConfig()
if err != nil {
_ = envconfig.Usage(configPrefix, c)
logrus.WithError(err).Fatal()
}
watermillLogger := message.NewWatermillLogrusLogger(logrus.StandardLogger())
messageRouter, err := watermillmessage.NewRouter(watermillmessage.RouterConfig{}, watermillLogger)
if err != nil {
logrus.WithError(err).Fatal()
}
defer messageRouter.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m := melody.New()
handleInterruptions(cancel, func() error {
return m.CloseWithMsg(melody.FormatCloseMessage(1000, "The server says goodbye :)"))
})
if kafkaProxyConfig.MessageSubscriber != nil {
defer kafkaProxyConfig.MessageSubscriber.Close()
defer kafkaProxyConfig.MessagePublisher.Close()
messageRouter.AddNoPublisherHandler("consume-validation-errors-from-kafka", c.KafkaProxy.MessageValidation.PublishToKafkaTopic, kafkaProxyConfig.MessageSubscriber, validationErrorsHandler(m))
}
kafkaProxy, err := kafka.NewProxy(kafkaProxyConfig, messageRouter)
if err != nil {
_ = envconfig.Usage(configPrefix, c)
logrus.WithError(err).Fatal()
}
runWebsocketServer(c.WSServerPort, "/ws", m)
runHealthCheckServer(80, "/")
group, ctx := errgroup.WithContext(ctx)
group.Go(func() error {
return messageRouter.Run(ctx) // Note: Can not be called until fully configured.
})
group.Go(func() error {
return kafkaProxy(ctx)
})
if err := group.Wait(); err != nil {
logrus.WithError(err).Fatal()
}
}
func runHealthCheckServer(port int, path string) {
r := chi.NewRouter()
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
go func() {
address := fmt.Sprintf(":%v", port)
logrus.Infof("Healthcheck server listening on %s", address)
if err := http.ListenAndServe(address, r); err != nil {
logrus.WithError(err).Fatal("error running healthcheck server")
}
}()
}
func runWebsocketServer(port int, path string, m *melody.Melody) {
r := chi.NewRouter()
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
_ = m.HandleRequest(w, r)
})
go func() {
address := fmt.Sprintf(":%v", port)
logrus.Infof("Websocket server listening on %s", address)
if err := http.ListenAndServe(address, r); err != nil {
logrus.WithError(err).Fatal("error running websocket server")
}
}()
}
func validationErrorsHandler(m *melody.Melody) watermillmessage.NoPublishHandlerFunc {
return func(msg *watermillmessage.Message) error {
validationError, err := message.ValidationErrorFromMessage(msg)
if err != nil {
logrus.WithError(err).WithField("uuid", msg.UUID).Error("Couldn't determine if the message is rather invalid or not")
return nil // no retry
}
if validationError == nil {
return nil
}
content, err := json.Marshal(msg)
if err != nil {
logrus.WithError(err).Error("error marshaling message")
content = []byte(fmt.Sprintf(
"The message %s is invalid: %s. However, there was an error during encoding and couldn't be shown completely. Please drop us an issue on github.",
msg.UUID,
validationError.Error(),
))
}
logrus.WithError(validationError).Debug("Message is invalid")
if err := m.Broadcast(content); err != nil {
logrus.WithError(err).Error("error broadcasting message to all ws sessions")
}
return nil
}
}
func handleInterruptions(cancel context.CancelFunc, funcs ...func() error) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-c
logrus.WithField("signal", s).Info("Stopping AsyncAPI Event-Gateway...")
cancel()
for _, f := range funcs {
if err := f(); err != nil {
logrus.WithError(err).Error("error shutting down")
}
}
time.Sleep(time.Second)
os.Exit(0)
}()
}