-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
196 lines (164 loc) · 4.77 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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/absmach/mgate"
"github.com/absmach/mgate/examples/simple"
"github.com/absmach/mgate/pkg/http"
"github.com/absmach/mgate/pkg/mqtt"
"github.com/absmach/mgate/pkg/mqtt/websocket"
"github.com/absmach/mgate/pkg/session"
"github.com/caarlos0/env/v11"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
)
const (
mqttWithoutTLS = "MGATE_MQTT_WITHOUT_TLS_"
mqttWithTLS = "MGATE_MQTT_WITH_TLS_"
mqttWithmTLS = "MGATE_MQTT_WITH_MTLS_"
mqttWSWithoutTLS = "MGATE_MQTT_WS_WITHOUT_TLS_"
mqttWSWithTLS = "MGATE_MQTT_WS_WITH_TLS_"
mqttWSWithmTLS = "MGATE_MQTT_WS_WITH_MTLS_"
httpWithoutTLS = "MGATE_HTTP_WITHOUT_TLS_"
httpWithTLS = "MGATE_HTTP_WITH_TLS_"
httpWithmTLS = "MGATE_HTTP_WITH_MTLS_"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
logger := slog.New(logHandler)
handler := simple.New(logger)
var interceptor session.Interceptor
// Loading .env file to environment
err := godotenv.Load()
if err != nil {
panic(err)
}
// mGate server Configuration for MQTT without TLS
mqttConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWithoutTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT without TLS
mqttProxy := mqtt.New(mqttConfig, handler, interceptor, logger)
g.Go(func() error {
return mqttProxy.Listen(ctx)
})
// mGate server Configuration for MQTT with TLS
mqttTLSConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWithTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT with TLS
mqttTLSProxy := mqtt.New(mqttTLSConfig, handler, interceptor, logger)
g.Go(func() error {
return mqttTLSProxy.Listen(ctx)
})
// mGate server Configuration for MQTT with mTLS
mqttMTLSConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWithmTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT with mTLS
mqttMTlsProxy := mqtt.New(mqttMTLSConfig, handler, interceptor, logger)
g.Go(func() error {
return mqttMTlsProxy.Listen(ctx)
})
// mGate server Configuration for MQTT over Websocket without TLS
wsConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWSWithoutTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT over Websocket without TLS
wsProxy := websocket.New(wsConfig, handler, interceptor, logger)
g.Go(func() error {
return wsProxy.Listen(ctx)
})
// mGate server Configuration for MQTT over Websocket with TLS
wsTLSConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWSWithTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT over Websocket with TLS
wsTLSProxy := websocket.New(wsTLSConfig, handler, interceptor, logger)
g.Go(func() error {
return wsTLSProxy.Listen(ctx)
})
// mGate server Configuration for MQTT over Websocket with mTLS
wsMTLSConfig, err := mgate.NewConfig(env.Options{Prefix: mqttWSWithmTLS})
if err != nil {
panic(err)
}
// mGate server for MQTT over Websocket with mTLS
wsMTLSProxy := websocket.New(wsMTLSConfig, handler, interceptor, logger)
g.Go(func() error {
return wsMTLSProxy.Listen(ctx)
})
// mGate server Configuration for HTTP without TLS
httpConfig, err := mgate.NewConfig(env.Options{Prefix: httpWithoutTLS})
if err != nil {
panic(err)
}
// mGate server for HTTP without TLS
httpProxy, err := http.NewProxy(httpConfig, handler, logger)
if err != nil {
panic(err)
}
g.Go(func() error {
return httpProxy.Listen(ctx)
})
// mGate server Configuration for HTTP with TLS
httpTLSConfig, err := mgate.NewConfig(env.Options{Prefix: httpWithTLS})
if err != nil {
panic(err)
}
// mGate server for HTTP with TLS
httpTLSProxy, err := http.NewProxy(httpTLSConfig, handler, logger)
if err != nil {
panic(err)
}
g.Go(func() error {
return httpTLSProxy.Listen(ctx)
})
// mGate server Configuration for HTTP with mTLS
httpMTLSConfig, err := mgate.NewConfig(env.Options{Prefix: httpWithmTLS})
if err != nil {
panic(err)
}
// mGate server for HTTP with mTLS
httpMTLSProxy, err := http.NewProxy(httpMTLSConfig, handler, logger)
if err != nil {
panic(err)
}
g.Go(func() error {
return httpMTLSProxy.Listen(ctx)
})
g.Go(func() error {
return StopSignalHandler(ctx, cancel, logger)
})
if err := g.Wait(); err != nil {
logger.Error(fmt.Sprintf("mGate service terminated with error: %s", err))
} else {
logger.Info("mGate service stopped")
}
}
func StopSignalHandler(ctx context.Context, cancel context.CancelFunc, logger *slog.Logger) error {
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT)
select {
case <-c:
cancel()
return nil
case <-ctx.Done():
return nil
}
}