rpc: move server-related code to a separate package

This commit is contained in:
Evgenii Stratonikov 2020-02-17 15:17:02 +03:00 committed by Roman Khimov
parent d24c6d1d9e
commit b50704fd3b
8 changed files with 16 additions and 16 deletions

View file

@ -14,7 +14,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network"
"github.com/CityOfZion/neo-go/pkg/network/metrics" "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/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
"go.uber.org/zap" "go.uber.org/zap"
@ -341,18 +341,18 @@ func startServer(ctx *cli.Context) error {
return err return err
} }
server, err := network.NewServer(serverConfig, chain, log) serv, err := network.NewServer(serverConfig, chain, log)
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create network server: %v", err), 1) 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) errChan := make(chan error)
go server.Start(errChan) go serv.Start(errChan)
go rpcServer.Start(errChan) go rpcServer.Start(errChan)
fmt.Println(logo()) fmt.Println(logo())
fmt.Println(server.UserAgent) fmt.Println(serv.UserAgent)
fmt.Println() fmt.Println()
var shutdownErr error var shutdownErr error
@ -364,7 +364,7 @@ Main:
cancel() cancel()
case <-grace.Done(): case <-grace.Done():
server.Shutdown() serv.Shutdown()
if serverErr := rpcServer.Shutdown(); serverErr != nil { if serverErr := rpcServer.Shutdown(); serverErr != nil {
shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server") shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server")
} }

View file

@ -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. This package is currently in alpha and is subject to change.
Server Server
@ -45,4 +45,4 @@ Unsupported methods
submitblock (needs to be implemented in pkg/core/blockchain.go) submitblock (needs to be implemented in pkg/core/blockchain.go)
*/ */
package rpc package server

View file

@ -1,4 +1,4 @@
package rpc package server
import "github.com/prometheus/client_golang/prometheus" import "github.com/prometheus/client_golang/prometheus"

View file

@ -1,4 +1,4 @@
package rpc package server
import ( import (
"context" "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) 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. // New creates a new Server struct.
func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server { func New(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server {
httpServer := &http.Server{ httpServer := &http.Server{
Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10), Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10),
} }

View file

@ -1,4 +1,4 @@
package rpc package server
import ( import (
"net/http" "net/http"
@ -178,7 +178,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
var nBlocks uint32 var nBlocks uint32
net := config.ModeUnitTestNet net := config.ModeUnitTestNet
configPath := "../../config" configPath := "../../../config"
cfg, err := config.Load(configPath, net) cfg, err := config.Load(configPath, net)
require.NoError(t, err, "could not load config") 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) serverConfig := network.NewServerConfig(cfg)
server, err := network.NewServer(serverConfig, chain, logger) server, err := network.NewServer(serverConfig, chain, logger)
require.NoError(t, err) 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) handler := http.HandlerFunc(rpcServer.requestHandler)
return chain, handler return chain, handler

View file

@ -1,4 +1,4 @@
package rpc package server
import ( import (
"bytes" "bytes"