-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstart.go
66 lines (50 loc) · 1.33 KB
/
start.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
package cmd
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/HydroProtocol/ethereum-jsonrpc-gateway/core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var startCmd = &cobra.Command{
Use: "start",
Run: func(cmd *cobra.Command, args []string) {
os.Exit(Run())
},
}
func waitExitSignal(ctxStop context.CancelFunc) {
var exitSignal = make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGTERM)
signal.Notify(exitSignal, syscall.SIGINT)
<-exitSignal
logrus.Info("Stopping...")
ctxStop()
}
func Run() int {
ctx, stop := context.WithCancel(context.Background())
go waitExitSignal(stop)
quitLoopConfig := make(chan bool)
core.LoadConfig(ctx, quitLoopConfig)
go core.StartMonitorHttpServer(ctx)
httpServer := &http.Server{Addr: ":3005", Handler: &core.Server{}}
// http server graceful shutdown
go func() {
<-ctx.Done()
quitLoopConfig <- true
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
logrus.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
}()
logrus.Infof("Listening on http://0.0.0.0%s\n", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
logrus.Fatal(err)
}
logrus.Info("Stopped")
return 0
}