/
server.go
109 lines (94 loc) · 2.89 KB
/
server.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
package node
import (
"context"
"errors"
"net"
"net/http"
"strconv"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
ophttp "github.com/patex-ecosystem/patex-network/pt-node/http"
"github.com/patex-ecosystem/patex-network/pt-node/metrics"
"github.com/patex-ecosystem/patex-network/pt-node/p2p"
"github.com/patex-ecosystem/patex-network/pt-node/rollup"
"github.com/patex-ecosystem/patex-network/pt-node/sources"
)
type rpcServer struct {
endpoint string
apis []rpc.API
httpServer *http.Server
appVersion string
listenAddr net.Addr
log log.Logger
sources.L2Client
}
func newRPCServer(ctx context.Context, rpcCfg *RPCConfig, rollupCfg *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, appVersion string, m metrics.Metricer) (*rpcServer, error) {
api := NewNodeAPI(rollupCfg, l2Client, dr, log.New("rpc", "node"), m)
// TODO: extend RPC config with options for WS, IPC and HTTP RPC connections
endpoint := net.JoinHostPort(rpcCfg.ListenAddr, strconv.Itoa(rpcCfg.ListenPort))
r := &rpcServer{
endpoint: endpoint,
apis: []rpc.API{{
Namespace: "patex",
Service: api,
Authenticated: false,
}},
appVersion: appVersion,
log: log,
}
return r, nil
}
func (s *rpcServer) EnableAdminAPI(api *adminAPI) {
s.apis = append(s.apis, rpc.API{
Namespace: "admin",
Version: "",
Service: api,
Authenticated: false,
})
}
func (s *rpcServer) EnableP2P(backend *p2p.APIBackend) {
s.apis = append(s.apis, rpc.API{
Namespace: p2p.NamespaceRPC,
Version: "",
Service: backend,
Authenticated: false,
})
}
func (s *rpcServer) Start() error {
srv := rpc.NewServer()
if err := node.RegisterApis(s.apis, nil, srv); err != nil {
return err
}
// The CORS and VHosts arguments below must be set in order for
// other services to connect to the ptnode. VHosts in particular
// defaults to localhost, which will prevent containers from
// calling into the ptnode without an "invalid host" error.
nodeHandler := node.NewHTTPHandlerStack(srv, []string{"*"}, []string{"*"}, nil)
mux := http.NewServeMux()
mux.Handle("/", nodeHandler)
mux.HandleFunc("/healthz", healthzHandler(s.appVersion))
listener, err := net.Listen("tcp", s.endpoint)
if err != nil {
return err
}
s.listenAddr = listener.Addr()
s.httpServer = ophttp.NewHttpServer(mux)
go func() {
if err := s.httpServer.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) { // todo improve error handling
s.log.Error("http server failed", "err", err)
}
}()
return nil
}
func (r *rpcServer) Stop() {
_ = r.httpServer.Shutdown(context.Background())
}
func (r *rpcServer) Addr() net.Addr {
return r.listenAddr
}
func healthzHandler(appVersion string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(appVersion))
}
}