From 649c3954c6e63d26cc8facbd34fad18fdb831443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Thu, 27 Jul 2023 21:23:27 +0300 Subject: [PATCH] Make RPC available with versioned path --- jsonrpc/http.go | 21 +++++++++++++-------- jsonrpc/http_test.go | 2 +- jsonrpc/websocket.go | 10 ++++++++-- jsonrpc/websocket_test.go | 2 +- node/node.go | 4 ++-- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/jsonrpc/http.go b/jsonrpc/http.go index 99c7a0f4f5..ec31428719 100644 --- a/jsonrpc/http.go +++ b/jsonrpc/http.go @@ -16,16 +16,18 @@ const MaxRequestBodySize = 10 * 1024 * 1024 // 10MB var _ service.Service = (*HTTP)(nil) type HTTP struct { - rpc *Server - log utils.SimpleLogger - listener net.Listener + rpc *Server + log utils.SimpleLogger + listener net.Listener + urlPrefix string } -func NewHTTP(listener net.Listener, rpc *Server, log utils.SimpleLogger) *HTTP { +func NewHTTP(urlPrefix string, listener net.Listener, rpc *Server, log utils.SimpleLogger) *HTTP { return &HTTP{ - rpc: rpc, - log: log, - listener: listener, + urlPrefix: urlPrefix, + rpc: rpc, + log: log, + listener: listener, } } @@ -33,9 +35,12 @@ func NewHTTP(listener net.Listener, rpc *Server, log utils.SimpleLogger) *HTTP { func (h *HTTP) Run(ctx context.Context) error { errCh := make(chan error) + mux := http.NewServeMux() + mux.Handle("/", h) + mux.Handle(h.urlPrefix, h) srv := &http.Server{ Addr: h.listener.Addr().String(), - Handler: h, + Handler: mux, // ReadTimeout also sets ReadHeaderTimeout and IdleTimeout. ReadTimeout: 30 * time.Second, } diff --git a/jsonrpc/http_test.go b/jsonrpc/http_test.go index 3210fb2362..5cd1e4463a 100644 --- a/jsonrpc/http_test.go +++ b/jsonrpc/http_test.go @@ -28,7 +28,7 @@ func TestHTTP(t *testing.T) { log := utils.NewNopZapLogger() rpc := jsonrpc.NewServer(log) require.NoError(t, rpc.RegisterMethod(method)) - server := jsonrpc.NewHTTP(listener, rpc, log) + server := jsonrpc.NewHTTP("/vX.Y.Z", listener, rpc, log) ctx, cancel := context.WithTimeout(context.Background(), time.Second) t.Cleanup(func() { diff --git a/jsonrpc/websocket.go b/jsonrpc/websocket.go index 4b05f71fba..5be06fb4ae 100644 --- a/jsonrpc/websocket.go +++ b/jsonrpc/websocket.go @@ -19,14 +19,16 @@ type Websocket struct { log utils.SimpleLogger connParams *WebsocketConnParams listener net.Listener + urlPrefix string } -func NewWebsocket(listener net.Listener, rpc *Server, log utils.SimpleLogger) *Websocket { +func NewWebsocket(urlPrefix string, listener net.Listener, rpc *Server, log utils.SimpleLogger) *Websocket { ws := &Websocket{ rpc: rpc, log: log, connParams: DefaultWebsocketConnParams(), listener: listener, + urlPrefix: urlPrefix, } return ws } @@ -78,8 +80,12 @@ func (ws *Websocket) Handler(ctx context.Context) http.Handler { func (ws *Websocket) Run(ctx context.Context) error { errCh := make(chan error) + handler := ws.Handler(ctx) + mux := http.NewServeMux() + mux.Handle("/", handler) + mux.Handle(ws.urlPrefix, handler) srv := &http.Server{ - Handler: ws.Handler(ctx), + Handler: mux, ReadHeaderTimeout: 1 * time.Second, } diff --git a/jsonrpc/websocket_test.go b/jsonrpc/websocket_test.go index 3e1cc47d5a..c7b9c24c61 100644 --- a/jsonrpc/websocket_test.go +++ b/jsonrpc/websocket_test.go @@ -29,7 +29,7 @@ func testConnection(t *testing.T) *websocket.Conn { ctx := context.Background() rpc := jsonrpc.NewServer(utils.NewNopZapLogger()) require.NoError(t, rpc.RegisterMethod(method)) - ws := jsonrpc.NewWebsocket(l, rpc, utils.NewNopZapLogger()) + ws := jsonrpc.NewWebsocket("/vX.Y.Z", l, rpc, utils.NewNopZapLogger()) go func() { t.Helper() require.NoError(t, ws.Run(context.Background())) diff --git a/node/node.go b/node/node.go index b8a4e7e7de..01d1a9c29c 100644 --- a/node/node.go +++ b/node/node.go @@ -288,13 +288,13 @@ func makeRPC(httpPort, wsPort uint16, rpcHandler *rpc.Handler, log utils.SimpleL if err != nil { return nil, fmt.Errorf("listen on http port %d: %w", httpPort, err) } - httpServer := jsonrpc.NewHTTP(httpListener, jsonrpcServer, log) + httpServer := jsonrpc.NewHTTP("/v0_4", httpListener, jsonrpcServer, log) wsListener, err := net.Listen("tcp", fmt.Sprintf(":%d", wsPort)) if err != nil { return nil, fmt.Errorf("listen on websocket port %d: %w", wsPort, err) } - wsServer := jsonrpc.NewWebsocket(wsListener, jsonrpcServer, log) + wsServer := jsonrpc.NewWebsocket("/v0_4", wsListener, jsonrpcServer, log) return []service.Service{httpServer, wsServer}, nil }