/
forkchoice.go
141 lines (109 loc) · 2.89 KB
/
forkchoice.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
package forky
import (
"context"
"io/fs"
"net/http"
"time"
"github.com/ethpandaops/forky/pkg/forky/api"
"github.com/ethpandaops/forky/pkg/forky/service"
static "github.com/ethpandaops/forky/web"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
// Server is the main server for the forkchoice service.
// It glues together the service and the http api, while
// also providing metrics and static file serving.
type Server struct {
log *logrus.Logger
Cfg Config
svc *service.ForkChoice
http *api.HTTP
}
func NewServer(log *logrus.Logger, conf *Config) *Server {
if err := conf.Validate(); err != nil {
log.Fatalf("invalid config: %s", err)
}
// Create our service which will glue everything together.
svc, err := service.NewForkChoice("forky", log, conf.Forky, service.DefaultOptions().SetMetricsEnabled(conf.Metrics.Enabled))
if err != nil {
log.Fatalf("failed to create service: %s", err)
}
// Create our HTTP API.
apiOpts := api.DefaultOptions().SetMetricsEnabled(conf.Metrics.Enabled)
h, err := api.NewHTTP(log, svc, conf.HTTP, apiOpts)
if err != nil {
log.Fatalf("failed to create http api: %s", err)
}
// Create our server.
s := &Server{
Cfg: *conf,
log: log,
svc: svc,
http: h,
}
return s
}
func (s *Server) Start(ctx context.Context) error {
if err := s.svc.Start(ctx); err != nil {
return err
}
if s.Cfg.PProfAddr != nil {
if err := s.ServePProf(ctx); err != nil {
return err
}
}
router := httprouter.New()
frontend, err := fs.Sub(static.FS, "build/frontend")
if err != nil {
return err
}
filesystem := http.FS(frontend)
router.NotFound = wrapHandler(http.FileServer(filesystem), filesystem)
if s.Cfg.Metrics.Enabled {
if err := s.ServeMetrics(ctx); err != nil {
return err
}
}
server := &http.Server{
Addr: s.Cfg.ListenAddr,
ReadHeaderTimeout: 3 * time.Minute,
WriteTimeout: 15 * time.Minute,
}
if err := s.http.BindToRouter(ctx, router); err != nil {
return err
}
server.Handler = router
s.log.Infof("Serving http at %s", s.Cfg.ListenAddr)
if err := server.ListenAndServe(); err != nil {
s.log.Fatal(err)
}
return nil
}
func (s *Server) ServePProf(ctx context.Context) error {
pprofServer := &http.Server{
Addr: *s.Cfg.PProfAddr,
ReadHeaderTimeout: 120 * time.Second,
}
go func() {
s.log.Infof("Serving pprof at %s", *s.Cfg.PProfAddr)
if err := pprofServer.ListenAndServe(); err != nil {
s.log.Fatal(err)
}
}()
return nil
}
func (s *Server) ServeMetrics(ctx context.Context) error {
go func() {
server := &http.Server{
Addr: s.Cfg.Metrics.Addr,
ReadHeaderTimeout: 15 * time.Second,
}
server.Handler = promhttp.Handler()
s.log.Infof("Serving metrics at %s", s.Cfg.Metrics.Addr)
if err := server.ListenAndServe(); err != nil {
s.log.Fatal(err)
}
}()
return nil
}