Skip to content

Commit

Permalink
Make RPC available with versioned path
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Jul 28, 2023
1 parent 146cf26 commit 649c395
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
21 changes: 13 additions & 8 deletions jsonrpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ 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,
}
}

// Run starts to listen for HTTP requests
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,
}
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 8 additions & 2 deletions jsonrpc/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 649c395

Please sign in to comment.