This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
cmd.go
149 lines (138 loc) · 5.34 KB
/
cmd.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
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package storehttp
import (
"context"
"flag"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/stratumn/go-core/jsonhttp"
"github.com/stratumn/go-core/jsonws"
"github.com/stratumn/go-core/monitoring"
"github.com/stratumn/go-core/store"
)
var (
storeEventsChanSize int
addr string
wsReadBufSize int
wsWriteBufSize int
wsWriteChanSize int
wsWriteTimeout time.Duration
wsPongTimeout time.Duration
wsPingInterval time.Duration
wsMaxMsgSize int64
certFile string
keyFile string
readTimeout time.Duration
writeTimeout time.Duration
maxHeaderBytes int
shutdownTimeout time.Duration
enableCORS bool
)
// Run launches a storehttp server.
func Run(
a store.Adapter,
config *Config,
monitoringConfig *monitoring.Config,
httpConfig *jsonhttp.Config,
basicConfig *jsonws.BasicConfig,
bufConnConfig *jsonws.BufferedConnConfig,
shutdownTimeout time.Duration,
) {
monitoring.LogEntry().Info("Copyright (c) 2017 Stratumn SAS")
monitoring.LogEntry().Info("Apache License 2.0")
monitoring.LogEntry().Infof("Runtime %s %s %s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
h := New(a, config, httpConfig, basicConfig, bufConnConfig)
if err := h.exposeMetrics(monitoringConfig); err != nil {
monitoring.LogEntry().Fatal(err)
}
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigc
monitoring.LogEntry().WithField("signal", sig).Info("Got exit signal")
monitoring.LogEntry().Info("Cleaning up")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := h.Shutdown(ctx); err != nil {
monitoring.LogEntry().WithField("error", err).Fatal("Failed to shutdown server")
}
monitoring.LogEntry().Info("Stopped")
os.Exit(0)
}()
monitoring.LogEntry().WithField("http", httpConfig.Address).Info("Listening")
if err := h.ListenAndServe(); err != nil {
monitoring.LogEntry().WithField("error", err).Fatal("Server stopped")
}
}
// RegisterFlags register the flags used by RunWithFlags.
func RegisterFlags() {
flag.IntVar(&storeEventsChanSize, "store_events_chan_size", DefaultStoreEventsChanSize, "Size of the store events channel")
flag.StringVar(&addr, "http", DefaultAddress, "HTTP address")
flag.IntVar(&wsReadBufSize, "ws_read_buf_size", jsonws.DefaultWebSocketReadBufferSize, "Web socket read buffer size")
flag.IntVar(&wsWriteBufSize, "ws_write_buf_size", jsonws.DefaultWebSocketWriteBufferSize, "Web socket write buffer size")
flag.IntVar(&wsWriteChanSize, "ws_write_chan_size", jsonws.DefaultWebSocketWriteChanSize, "Size of a web socket connection write channel")
flag.DurationVar(&wsWriteTimeout, "ws_write_timeout", jsonws.DefaultWebSocketWriteTimeout, "Timeout for a web socket write")
flag.DurationVar(&wsPongTimeout, "ws_pong_timeout", jsonws.DefaultWebSocketPongTimeout, "Timeout for a web socket expected pong")
flag.DurationVar(&wsPingInterval, "ws_ping_interval", jsonws.DefaultWebSocketPingInterval, "Interval between web socket pings")
flag.Int64Var(&wsMaxMsgSize, "max_msg_size", jsonws.DefaultWebSocketMaxMsgSize, "Maximum size of a received web socket message")
flag.StringVar(&certFile, "tls_cert", "", "TLS certificate file")
flag.StringVar(&keyFile, "tls_key", "", "TLS private key file")
flag.DurationVar(&readTimeout, "read_timeout", jsonhttp.DefaultReadTimeout, "Read timeout")
flag.DurationVar(&writeTimeout, "write_timeout", jsonhttp.DefaultWriteTimeout, "Write timeout")
flag.IntVar(&maxHeaderBytes, "max_header_bytes", jsonhttp.DefaultMaxHeaderBytes, "Maximum header bytes")
flag.DurationVar(&shutdownTimeout, "shutdown_timeout", 10*time.Second, "Shutdown timeout")
flag.BoolVar(&enableCORS, "enable_cors", false, "Allow cross-origin requests")
}
// RunWithFlags should be called after RegisterFlags and flag.Parse to launch
// a storehttp server configured using flag values.
func RunWithFlags(a store.Adapter) {
config := &Config{
StoreEventsChanSize: storeEventsChanSize,
}
monitoringConfig := monitoring.ConfigurationFromFlags()
httpConfig := &jsonhttp.Config{
Address: addr,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: maxHeaderBytes,
CertFile: certFile,
KeyFile: keyFile,
EnableCORS: enableCORS,
}
basicConfig := &jsonws.BasicConfig{
ReadBufferSize: wsReadBufSize,
WriteBufferSize: wsWriteBufSize,
EnableCORS: enableCORS,
}
bufConnConfig := &jsonws.BufferedConnConfig{
Size: wsWriteChanSize,
WriteTimeout: wsWriteTimeout,
PongTimeout: wsPongTimeout,
PingInterval: wsPingInterval,
MaxMsgSize: wsMaxMsgSize,
}
Run(
a,
config,
monitoringConfig,
httpConfig,
basicConfig,
bufConnConfig,
shutdownTimeout,
)
}