From b50704fd3bfce709975ced5e83ad9923be020388 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 17 Feb 2020 15:17:02 +0300 Subject: [PATCH] rpc: move server-related code to a separate package --- cli/server/server.go | 12 ++++++------ pkg/rpc/{ => server}/doc.go | 4 ++-- pkg/rpc/{ => server}/prometheus.go | 2 +- pkg/rpc/{ => server}/server.go | 6 +++--- pkg/rpc/{ => server}/server_helper_test.go | 6 +++--- pkg/rpc/{ => server}/server_test.go | 2 +- pkg/rpc/{ => server}/testdata/test_contract.avm | Bin pkg/rpc/{ => server}/testdata/testblocks.acc | Bin 8 files changed, 16 insertions(+), 16 deletions(-) rename pkg/rpc/{ => server}/doc.go (94%) rename pkg/rpc/{ => server}/prometheus.go (99%) rename pkg/rpc/{ => server}/server.go (98%) rename pkg/rpc/{ => server}/server_helper_test.go (98%) rename pkg/rpc/{ => server}/server_test.go (99%) rename pkg/rpc/{ => server}/testdata/test_contract.avm (100%) rename pkg/rpc/{ => server}/testdata/testblocks.acc (100%) diff --git a/cli/server/server.go b/cli/server/server.go index a8af9571a..f2bc41edd 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -14,7 +14,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network/metrics" - "github.com/CityOfZion/neo-go/pkg/rpc" + "github.com/CityOfZion/neo-go/pkg/rpc/server" "github.com/pkg/errors" "github.com/urfave/cli" "go.uber.org/zap" @@ -341,18 +341,18 @@ func startServer(ctx *cli.Context) error { return err } - server, err := network.NewServer(serverConfig, chain, log) + serv, err := network.NewServer(serverConfig, chain, log) if err != nil { return cli.NewExitError(fmt.Errorf("failed to create network server: %v", err), 1) } - rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server, log) + rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, serv, log) errChan := make(chan error) - go server.Start(errChan) + go serv.Start(errChan) go rpcServer.Start(errChan) fmt.Println(logo()) - fmt.Println(server.UserAgent) + fmt.Println(serv.UserAgent) fmt.Println() var shutdownErr error @@ -364,7 +364,7 @@ Main: cancel() case <-grace.Done(): - server.Shutdown() + serv.Shutdown() if serverErr := rpcServer.Shutdown(); serverErr != nil { shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server") } diff --git a/pkg/rpc/doc.go b/pkg/rpc/server/doc.go similarity index 94% rename from pkg/rpc/doc.go rename to pkg/rpc/server/doc.go index fb44d1b1d..dbf7aae9a 100644 --- a/pkg/rpc/doc.go +++ b/pkg/rpc/server/doc.go @@ -1,5 +1,5 @@ /* -Package rpc implements NEO-specific JSON-RPC 2.0 server. +Package server implements NEO-specific JSON-RPC 2.0 server. This package is currently in alpha and is subject to change. Server @@ -45,4 +45,4 @@ Unsupported methods submitblock (needs to be implemented in pkg/core/blockchain.go) */ -package rpc +package server diff --git a/pkg/rpc/prometheus.go b/pkg/rpc/server/prometheus.go similarity index 99% rename from pkg/rpc/prometheus.go rename to pkg/rpc/server/prometheus.go index 2e5d5b4db..63312433d 100644 --- a/pkg/rpc/prometheus.go +++ b/pkg/rpc/server/prometheus.go @@ -1,4 +1,4 @@ -package rpc +package server import "github.com/prometheus/client_golang/prometheus" diff --git a/pkg/rpc/server.go b/pkg/rpc/server/server.go similarity index 98% rename from pkg/rpc/server.go rename to pkg/rpc/server/server.go index e795b1ee5..d4ecb4518 100644 --- a/pkg/rpc/server.go +++ b/pkg/rpc/server/server.go @@ -1,4 +1,4 @@ -package rpc +package server import ( "context" @@ -38,8 +38,8 @@ var invalidBlockHeightError = func(index int, height int) error { return errors.Errorf("Param at index %d should be greater than or equal to 0 and less then or equal to current block height, got: %d", index, height) } -// NewServer creates a new Server struct. -func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server { +// New creates a new Server struct. +func New(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server { httpServer := &http.Server{ Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10), } diff --git a/pkg/rpc/server_helper_test.go b/pkg/rpc/server/server_helper_test.go similarity index 98% rename from pkg/rpc/server_helper_test.go rename to pkg/rpc/server/server_helper_test.go index 8b00d684e..496d2f7bb 100644 --- a/pkg/rpc/server_helper_test.go +++ b/pkg/rpc/server/server_helper_test.go @@ -1,4 +1,4 @@ -package rpc +package server import ( "net/http" @@ -178,7 +178,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu var nBlocks uint32 net := config.ModeUnitTestNet - configPath := "../../config" + configPath := "../../../config" cfg, err := config.Load(configPath, net) require.NoError(t, err, "could not load config") @@ -210,7 +210,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu serverConfig := network.NewServerConfig(cfg) server, err := network.NewServer(serverConfig, chain, logger) require.NoError(t, err) - rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server, logger) + rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger) handler := http.HandlerFunc(rpcServer.requestHandler) return chain, handler diff --git a/pkg/rpc/server_test.go b/pkg/rpc/server/server_test.go similarity index 99% rename from pkg/rpc/server_test.go rename to pkg/rpc/server/server_test.go index cad05c0fb..3ba9096da 100644 --- a/pkg/rpc/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -1,4 +1,4 @@ -package rpc +package server import ( "bytes" diff --git a/pkg/rpc/testdata/test_contract.avm b/pkg/rpc/server/testdata/test_contract.avm similarity index 100% rename from pkg/rpc/testdata/test_contract.avm rename to pkg/rpc/server/testdata/test_contract.avm diff --git a/pkg/rpc/testdata/testblocks.acc b/pkg/rpc/server/testdata/testblocks.acc similarity index 100% rename from pkg/rpc/testdata/testblocks.acc rename to pkg/rpc/server/testdata/testblocks.acc