forked from resgateio/resgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
366 lines (324 loc) · 12 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/resgateio/resgate/logger"
"github.com/resgateio/resgate/nats"
"github.com/resgateio/resgate/server"
)
const (
// StopTimeout is the duration Resgate waits for all processes to
// stop before forcefully exiting with an error and a stack trace.
StopTimeout = 10 * time.Second
// DefaultNatsURL is the default NATS server to connect to.
DefaultNatsURL = "nats://127.0.0.1:4222"
// DefaultRequestTimeout is the timeout duration for NATS requests in milliseconds.
DefaultRequestTimeout = 3000
)
var usageStr = `
Usage: resgate [options]
Server Options:
-n, --nats <url> NATS Server URL (default: nats://127.0.0.1:4222)
-i --addr <host> Bind to HOST address (default: 0.0.0.0)
-p, --port <port> HTTP port for client connections (default: 8080)
-w, --wspath <path> WebSocket path for clients (default: /)
-a, --apipath <path> Web resource path for clients (default: /api/)
-r, --reqtimeout <milliseconds> Timeout duration for NATS requests (default: 3000)
-u, --headauth <method> Resource method for header authentication
-t, --wsheadauth <method> Resource method for WebSocket header authentication
-m, --metricsport <port> HTTP port for OpenMetrics connections (default: disabled)
--apiencoding <type> Encoding for web resources: json, jsonflat (default: json)
--putmethod <methodName> Call method name mapped to HTTP PUT requests
--deletemethod <methodName> Call method name mapped to HTTP DELETE requests
--patchmethod <methodName> Call method name mapped to HTTP PATCH requests
--wscompression Enable WebSocket per message compression
--resetthrottle <limit> Limit on parallel requests sent in response to a system reset
--referencethrottle <limit> Limit on parallel requests sent when following resource references
-c, --config <file> Configuration file
Security Options:
--tls Enable TLS for HTTP (default: false)
--tlscert <file> HTTP server certificate file
--tlskey <file> Private key for HTTP server certificate
--creds <file> NATS User Credentials file
--natscert <file> NATS Client certificate file
--natskey <file> NATS Client certificate key file
--natsrootca <file> NATS Root CA file(s)
--alloworigin <origin> Allowed origin(s): *, or <scheme>://<hostname>[:<port>] (default: *)
Logging Options:
-D, --debug Enable debugging output
-V, --trace Enable trace logging
-DV Debug and trace
Common Options:
-h, --help Show this message
-v, --version Show version
Configuration Documentation: https://resgate.io/docs/get-started/configuration/
`
// Config holds server configuration
type Config struct {
NatsURL string `json:"natsUrl"`
NatsCreds string `json:"natsCreds"`
NatsTLSCert string `json:"natsCert"`
NatsTLSKey string `json:"natsKey"`
NatsRootCAs []string `json:"natsRootCAs"`
RequestTimeout int `json:"requestTimeout"`
BufferSize int `json:"bufferSize"`
Debug bool `json:"debug"`
Trace bool `json:"trace"`
server.Config
}
// StringSlice is a slice of strings implementing the flag.Value interface.
type StringSlice []string
func (s *StringSlice) String() string {
if s == nil {
return ""
}
return strings.Join(*s, ";")
}
// Set adds a value to the slice.
func (s *StringSlice) Set(v string) error {
*s = append(*s, v)
return nil
}
// SetDefault sets the default values
func (c *Config) SetDefault() {
if c.NatsURL == "" {
c.NatsURL = DefaultNatsURL
}
if c.RequestTimeout == 0 {
c.RequestTimeout = DefaultRequestTimeout
}
if c.NatsRootCAs == nil {
c.NatsRootCAs = []string{}
}
if c.BufferSize == 0 {
c.BufferSize = 8192
}
c.Config.SetDefault()
}
// Init takes a path to a json encoded file and loads the config
// If no file exists, a new file with default settings is created
func (c *Config) Init(fs *flag.FlagSet, args []string) {
var (
showHelp bool
showVersion bool
configFile string
port uint
headauth string
wsheadauth string
metricsport uint
addr string
natsRootCAs StringSlice
debugTrace bool
allowOrigin StringSlice
putMethod string
deleteMethod string
patchMethod string
)
fs.BoolVar(&showHelp, "h", false, "Show this message.")
fs.BoolVar(&showHelp, "help", false, "Show this message.")
fs.StringVar(&configFile, "c", "", "Configuration file.")
fs.StringVar(&configFile, "config", "", "Configuration file.")
fs.StringVar(&c.NatsURL, "n", "", "NATS Server URL.")
fs.StringVar(&c.NatsURL, "nats", "", "NATS Server URL.")
fs.StringVar(&addr, "i", "", "Bind to HOST address.")
fs.StringVar(&addr, "addr", "", "Bind to HOST address.")
fs.UintVar(&port, "p", 0, "HTTP port for client connections.")
fs.UintVar(&port, "port", 0, "HTTP port for client connections.")
fs.StringVar(&c.WSPath, "w", "", "WebSocket path for clients.")
fs.StringVar(&c.WSPath, "wspath", "", "WebSocket path for clients.")
fs.StringVar(&c.APIPath, "a", "", "Web resource path for clients.")
fs.StringVar(&c.APIPath, "apipath", "", "Web resource path for clients.")
fs.StringVar(&headauth, "u", "", "Resource method for header authentication.")
fs.StringVar(&headauth, "headauth", "", "Resource method for header authentication.")
fs.StringVar(&wsheadauth, "t", "", "Resource method for WebSocket header authentication.")
fs.StringVar(&wsheadauth, "wsheadauth", "", "Resource method for WebSocket header authentication.")
fs.UintVar(&metricsport, "m", 0, "HTTP port for OpenMetrics connections (default: disabled)")
fs.UintVar(&metricsport, "metricsport", 0, "HTTP port for OpenMetrics connections (default: disabled)")
fs.BoolVar(&c.TLS, "tls", false, "Enable TLS for HTTP.")
fs.StringVar(&c.TLSCert, "tlscert", "", "HTTP server certificate file.")
fs.StringVar(&c.TLSKey, "tlskey", "", "Private key for HTTP server certificate.")
fs.StringVar(&c.APIEncoding, "apiencoding", "", "Encoding for web resources.")
fs.IntVar(&c.RequestTimeout, "r", 0, "Timeout in milliseconds for NATS requests.")
fs.IntVar(&c.RequestTimeout, "reqtimeout", 0, "Timeout in milliseconds for NATS requests.")
fs.StringVar(&c.NatsCreds, "creds", "", "NATS User Credentials file.")
fs.StringVar(&c.NatsTLSCert, "natscert", "", "NATS Client certificate file.")
fs.StringVar(&c.NatsTLSKey, "natskey", "", "NATS Client certificate key file.")
fs.Var(&natsRootCAs, "natsrootca", "NATS Root CA file(s).")
fs.Var(&allowOrigin, "alloworigin", "Allowed origin(s) for CORS.")
fs.StringVar(&putMethod, "putmethod", "", "Call method name mapped to HTTP PUT requests.")
fs.StringVar(&deleteMethod, "deletemethod", "", "Call method name mapped to HTTP DELETE requests.")
fs.StringVar(&patchMethod, "patchmethod", "", "Call method name mapped to HTTP PATCH requests.")
fs.BoolVar(&c.WSCompression, "wscompression", false, "Enable WebSocket per message compression.")
fs.IntVar(&c.ResetThrottle, "resetthrottle", 0, "Limit on parallel requests sent in response to a system reset.")
fs.IntVar(&c.ReferenceThrottle, "referencethrottle", 0, "Limit on parallel requests sent when following resource references.")
fs.BoolVar(&c.Debug, "D", false, "Enable debugging output.")
fs.BoolVar(&c.Debug, "debug", false, "Enable debugging output.")
fs.BoolVar(&c.Trace, "V", false, "Enable trace logging.")
fs.BoolVar(&c.Trace, "trace", false, "Enable trace logging.")
fs.BoolVar(&debugTrace, "DV", false, "Enable debug and trace logging.")
fs.BoolVar(&showVersion, "version", false, "Print version information.")
fs.BoolVar(&showVersion, "v", false, "Print version information.")
if err := fs.Parse(args); err != nil {
printAndDie(fmt.Sprintf("Error parsing command arguments: %s", err.Error()), true)
}
if port >= 1<<16 {
printAndDie(fmt.Sprintf(`Invalid port "%d": must be less than 65536`, port), true)
}
if metricsport >= 1<<16 {
printAndDie(fmt.Sprintf(`Invalid metrics port "%d": must be less than 65536`, metricsport), true)
}
if showHelp {
usage()
}
if showVersion {
version()
}
writeConfig := false
if configFile != "" {
fin, err := os.ReadFile(configFile)
if err != nil {
if !os.IsNotExist(err) {
printAndDie(fmt.Sprintf("Error loading config file: %s", err), false)
}
c.SetDefault()
writeConfig = true
} else {
err = json.Unmarshal(fin, c)
if err != nil {
printAndDie(fmt.Sprintf("Error parsing config file: %s", err), false)
}
// Overwrite configFile options with command line options
fs.Parse(args)
}
}
if port > 0 {
c.Port = uint16(port)
}
if metricsport > 0 {
c.MetricsPort = uint16(metricsport)
}
// Helper function to set string pointers to nil if empty.
setString := func(v string, s **string) {
if v == "" {
*s = nil
} else {
*s = &v
}
}
fs.Visit(func(f *flag.Flag) {
switch f.Name {
case "u":
fallthrough
case "headauth":
setString(headauth, &c.HeaderAuth)
case "t":
fallthrough
case "wsheadauth":
setString(wsheadauth, &c.WSHeaderAuth)
case "natsrootca":
c.NatsRootCAs = natsRootCAs
case "alloworigin":
str := allowOrigin.String()
c.AllowOrigin = &str
case "putmethod":
setString(putMethod, &c.PUTMethod)
case "deletemethod":
setString(deleteMethod, &c.DELETEMethod)
case "patchmethod":
setString(patchMethod, &c.PATCHMethod)
case "i":
fallthrough
case "addr":
c.Addr = &addr
case "DV":
c.Debug = true
c.Trace = true
}
})
// Any value not set, set it now
c.SetDefault()
// Write config file
if writeConfig {
fout, err := json.MarshalIndent(c, "", "\t")
if err != nil {
printAndDie(fmt.Sprintf("Error encoding config: %s", err), false)
}
os.WriteFile(configFile, fout, os.FileMode(0664))
}
}
// usage will print out the flag options for the server.
func usage() {
fmt.Printf("%s\n", usageStr)
os.Exit(0)
}
// version will print out the current resgate and protocol version.
func version() {
fmt.Printf("resgate v%s\nprotocol v%s\n", server.Version, server.ProtocolVersion)
os.Exit(0)
}
func printAndDie(msg string, showUsage bool) {
fmt.Fprintln(os.Stderr, msg)
if showUsage {
fmt.Fprintln(os.Stderr, usageStr)
}
os.Exit(1)
}
func main() {
fs := flag.NewFlagSet("resgate", flag.ExitOnError)
fs.Usage = usage
var cfg Config
cfg.Init(fs, os.Args[1:])
l := logger.NewStdLogger(cfg.Debug, cfg.Trace)
// Remove below if clause after release of version >= 1.3.x
if cfg.RequestTimeout <= 10 {
fmt.Fprintf(os.Stderr, "[DEPRECATED] Request timeout should be in milliseconds.\nChange your requestTimeout from %d to %d, and you won't be bothered anymore.\n", cfg.RequestTimeout, cfg.RequestTimeout*1000)
cfg.RequestTimeout *= 1000
}
serv, err := server.NewService(&nats.Client{
URL: cfg.NatsURL,
Creds: cfg.NatsCreds,
ClientCert: cfg.NatsTLSCert,
ClientKey: cfg.NatsTLSKey,
RootCAs: cfg.NatsRootCAs,
RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Millisecond,
BufferSize: cfg.BufferSize,
Logger: l,
}, cfg.Config)
if err != nil {
printAndDie(fmt.Sprintf("Failed to initialize server: %s", err.Error()), false)
}
serv.SetLogger(l)
if err := serv.Start(); err != nil {
printAndDie(fmt.Sprintf("Failed to start server: %s", err.Error()), false)
}
stop := make(chan os.Signal, 1)
signal.Notify(stop,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGQUIT)
select {
case <-stop:
case err := <-serv.StopChannel():
if err != nil {
printAndDie(fmt.Sprintf("Server stopped with an error: %s", err.Error()), false)
}
}
// Await for waitGroup to be done
done := make(chan struct{})
go func() {
defer close(done)
serv.Stop(nil)
}()
select {
case <-done:
case <-time.After(StopTimeout):
panic("Shutdown timed out")
}
}