-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
118 lines (106 loc) · 2.59 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/flashbots/go-template/common"
"github.com/flashbots/go-template/httpserver"
"github.com/google/uuid"
"github.com/urfave/cli/v2" // imports as package "cli"
)
var flags []cli.Flag = []cli.Flag{
&cli.StringFlag{
Name: "listen-addr",
Value: "127.0.0.1:8080",
Usage: "address to listen on for API",
},
&cli.StringFlag{
Name: "metrics-addr",
Value: "127.0.0.1:8090",
Usage: "address to listen on for Prometheus metrics",
},
&cli.BoolFlag{
Name: "log-json",
Value: false,
Usage: "log in JSON format",
},
&cli.BoolFlag{
Name: "log-debug",
Value: false,
Usage: "log debug messages",
},
&cli.BoolFlag{
Name: "log-uid",
Value: false,
Usage: "generate a uuid and add to all log messages",
},
&cli.StringFlag{
Name: "log-service",
Value: "your-project",
Usage: "add 'service' tag to logs",
},
&cli.BoolFlag{
Name: "pprof",
Value: false,
Usage: "enable pprof debug endpoint",
},
&cli.Int64Flag{
Name: "drain-seconds",
Value: 45,
Usage: "seconds to wait in drain HTTP request",
},
}
func main() {
app := &cli.App{
Name: "httpserver",
Usage: "Serve API, and metrics",
Flags: flags,
Action: func(cCtx *cli.Context) error {
listenAddr := cCtx.String("listen-addr")
metricsAddr := cCtx.String("metrics-addr")
logJSON := cCtx.Bool("log-json")
logDebug := cCtx.Bool("log-debug")
logUID := cCtx.Bool("log-uid")
logService := cCtx.String("log-service")
enablePprof := cCtx.Bool("pprof")
drainDuration := time.Duration(cCtx.Int64("drain-seconds")) * time.Second
log := common.SetupLogger(&common.LoggingOpts{
Debug: logDebug,
JSON: logJSON,
Service: logService,
Version: common.Version,
})
if logUID {
id := uuid.Must(uuid.NewRandom())
log = log.With("uid", id.String())
}
cfg := &httpserver.HTTPServerConfig{
ListenAddr: listenAddr,
MetricsAddr: metricsAddr,
Log: log,
EnablePprof: enablePprof,
DrainDuration: drainDuration,
GracefulShutdownDuration: 30 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 30 * time.Second,
}
srv, err := httpserver.New(cfg)
if err != nil {
cfg.Log.Error("failed to create server", "err", err)
return err
}
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
srv.RunInBackground()
<-exit
// Shutdown server once termination signal is received
srv.Shutdown()
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}