-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.go
More file actions
159 lines (134 loc) · 3.93 KB
/
Copy pathserver.go
File metadata and controls
159 lines (134 loc) · 3.93 KB
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
package server
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"time"
"github.com/shutter-network/encrypting-rpc-server/db"
"github.com/shutter-network/encrypting-rpc-server/metrics"
"github.com/shutter-network/encrypting-rpc-server/utils"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/pkg/errors"
"github.com/shutter-network/encrypting-rpc-server/rpc"
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
medleyService "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
)
type JSONRPCProxy struct {
backend http.Handler
processor http.Handler
}
func (p *JSONRPCProxy) SelectHandler(method string) http.Handler {
// route the eth_namespace to the l2-backend
switch method {
case "eth_sendTransaction":
return p.processor
case "eth_sendRawTransaction":
return p.processor
default:
return p.backend
}
}
func (p *JSONRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
rpcreq := medley.RPCRequest{}
err = json.Unmarshal(body, &rpcreq)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
selectedHandler := p.SelectHandler(rpcreq.Method)
if selectedHandler == p.processor {
utils.Logger.Info().Str("method", rpcreq.Method).Msg("dispatching to processor")
} else {
utils.Logger.Info().Str("method", rpcreq.Method).Msg("dispatching to backend")
}
// make the body available again before letting reverse proxy handle the rest
r.Body = io.NopCloser(bytes.NewBuffer(body))
startTime := time.Now()
selectedHandler.ServeHTTP(w, r)
if selectedHandler == p.backend {
metrics.UpstreamRequestDuration.WithLabelValues(rpcreq.Method).Observe(time.Since(startTime).Seconds())
}
}
type server struct {
processor rpc.Processor
config rpc.Config
postgresDatabase *db.PostgresDb
}
func NewRPCService(processor rpc.Processor, config rpc.Config, pgDb *db.PostgresDb) medleyService.Service {
return &server{
processor: processor,
config: config,
postgresDatabase: pgDb,
}
}
func (srv *server) rpcHandler(ctx context.Context) (http.Handler, *[]rpc.RPCService, error) {
rpcServices := []rpc.RPCService{
&rpc.EthService{},
}
rpcServer := ethrpc.NewServer()
for _, service := range rpcServices {
service.Init(srv.processor, srv.config)
err := rpcServer.RegisterName(service.Name(), service)
if err != nil {
return nil, nil, errors.Wrap(err, "error while trying to register RPCService")
}
}
p := &JSONRPCProxy{
backend: NewReverseProxy(srv.config.BackendURL),
processor: rpcServer,
}
return p, &rpcServices, nil
}
func (srv *server) setupRouter(ctx context.Context) (*chi.Mux, *[]rpc.RPCService, error) {
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
handler, services, err := srv.rpcHandler(ctx)
if err != nil {
return nil, nil, err
}
router.Mount("/", handler)
return router, services, nil
}
func (srv *server) Start(ctx context.Context, runner medleyService.Runner) error {
handler, services, err := srv.setupRouter(ctx)
if err != nil {
return err
}
httpServer := &http.Server{
Addr: srv.config.HTTPListenAddress,
Handler: handler,
ReadHeaderTimeout: 5 * time.Second,
}
runner.Go(func() error {
srv.postgresDatabase.Start(ctx)
return nil
})
for _, service := range *services {
if err := runner.StartService(service); err != nil {
return err
}
}
if srv.processor.MetricsConfig.Enabled {
if err := runner.StartService(srv.processor.MetricsServer); err != nil {
return err
}
}
runner.Go(httpServer.ListenAndServe)
runner.Go(func() error {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
return httpServer.Shutdown(shutdownCtx)
})
return nil
}