mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 03:41:34 +00:00
rpc: use zap.Logger
This commit is contained in:
parent
aecdf470e7
commit
289cb1c1d9
5 changed files with 40 additions and 43 deletions
|
@ -320,7 +320,7 @@ func startServer(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
server := network.NewServer(serverConfig, chain, log)
|
||||
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
|
||||
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server, log)
|
||||
errChan := make(chan error)
|
||||
|
||||
go server.Start(errChan)
|
||||
|
|
1
go.mod
1
go.mod
|
@ -11,7 +11,6 @@ require (
|
|||
github.com/nspcc-dev/rfc6979 v0.1.0
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/prometheus/client_golang v1.2.1
|
||||
github.com/sirupsen/logrus v1.4.2
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/syndtr/goleveldb v0.0.0-20180307113352-169b1b37be73
|
||||
github.com/urfave/cli v1.20.0
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -73,7 +73,7 @@ func (r *Request) Params() (*Params, error) {
|
|||
}
|
||||
|
||||
// WriteErrorResponse writes an error response to the ResponseWriter.
|
||||
func (r Request) WriteErrorResponse(w http.ResponseWriter, err error) {
|
||||
func (s *Server) WriteErrorResponse(r *Request, w http.ResponseWriter, err error) {
|
||||
jsonErr, ok := err.(*Error)
|
||||
if !ok {
|
||||
jsonErr = NewInternalServerError("Internal server error", err)
|
||||
|
@ -85,32 +85,34 @@ func (r Request) WriteErrorResponse(w http.ResponseWriter, err error) {
|
|||
ID: r.RawID,
|
||||
}
|
||||
|
||||
logFields := log.Fields{
|
||||
"err": jsonErr.Cause,
|
||||
"method": r.Method,
|
||||
}
|
||||
params, err := r.Params()
|
||||
if err == nil {
|
||||
logFields["params"] = *params
|
||||
logFields := []zap.Field{
|
||||
zap.Error(jsonErr.Cause),
|
||||
zap.String("method", r.Method),
|
||||
}
|
||||
|
||||
log.WithFields(logFields).Error("Error encountered with rpc request")
|
||||
params, err := r.Params()
|
||||
if err == nil {
|
||||
logFields = append(logFields, zap.Any("params", params))
|
||||
}
|
||||
|
||||
s.log.Error("Error encountered with rpc request", logFields...)
|
||||
|
||||
w.WriteHeader(jsonErr.HTTPCode)
|
||||
r.writeServerResponse(w, response)
|
||||
s.writeServerResponse(r, w, response)
|
||||
}
|
||||
|
||||
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
||||
func (r Request) WriteResponse(w http.ResponseWriter, result interface{}) {
|
||||
func (s *Server) WriteResponse(r *Request, w http.ResponseWriter, result interface{}) {
|
||||
response := Response{
|
||||
JSONRPC: r.JSONRPC,
|
||||
Result: result,
|
||||
ID: r.RawID,
|
||||
}
|
||||
|
||||
r.writeServerResponse(w, response)
|
||||
s.writeServerResponse(r, w, response)
|
||||
}
|
||||
|
||||
func (r Request) writeServerResponse(w http.ResponseWriter, response Response) {
|
||||
func (s *Server) writeServerResponse(r *Request, w http.ResponseWriter, response Response) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
if r.enableCORSWorkaround {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
@ -120,12 +122,9 @@ func (r Request) writeServerResponse(w http.ResponseWriter, response Response) {
|
|||
encoder := json.NewEncoder(w)
|
||||
err := encoder.Encode(response)
|
||||
|
||||
logFields := log.Fields{
|
||||
"err": err,
|
||||
"method": r.Method,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.WithFields(logFields).Error("Error encountered while encoding response")
|
||||
s.log.Error("Error encountered while encoding response",
|
||||
zap.String("err", err.Error()),
|
||||
zap.String("method", r.Method))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
|
||||
"github.com/CityOfZion/neo-go/pkg/util"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -26,6 +26,7 @@ type (
|
|||
chain core.Blockchainer
|
||||
config config.RPCConfig
|
||||
coreServer *network.Server
|
||||
log *zap.Logger
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -34,7 +35,7 @@ var invalidBlockHeightError = func(index int, height int) error {
|
|||
}
|
||||
|
||||
// NewServer creates a new Server struct.
|
||||
func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server) Server {
|
||||
func NewServer(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),
|
||||
}
|
||||
|
@ -44,6 +45,7 @@ func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *netwo
|
|||
chain: chain,
|
||||
config: conf,
|
||||
coreServer: coreServer,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,13 +53,11 @@ func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *netwo
|
|||
// listening on the configured port.
|
||||
func (s *Server) Start(errChan chan error) {
|
||||
if !s.config.Enabled {
|
||||
log.Info("RPC server is not enabled")
|
||||
s.log.Info("RPC server is not enabled")
|
||||
return
|
||||
}
|
||||
s.Handler = http.HandlerFunc(s.requestHandler)
|
||||
log.WithFields(log.Fields{
|
||||
"endpoint": s.Addr,
|
||||
}).Info("starting rpc-server")
|
||||
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
|
||||
|
||||
errChan <- s.ListenAndServe()
|
||||
}
|
||||
|
@ -65,9 +65,7 @@ func (s *Server) Start(errChan chan error) {
|
|||
// Shutdown overrides the http.Server Shutdown
|
||||
// method.
|
||||
func (s *Server) Shutdown() error {
|
||||
log.WithFields(log.Fields{
|
||||
"endpoint": s.Addr,
|
||||
}).Info("shutting down rpc-server")
|
||||
s.log.Info("shutting down rpc-server", zap.String("endpoint", s.Addr))
|
||||
return s.Server.Shutdown(context.Background())
|
||||
}
|
||||
|
||||
|
@ -75,7 +73,8 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
|||
req := NewRequest(s.config.EnableCORSWorkaround)
|
||||
|
||||
if httpRequest.Method != "POST" {
|
||||
req.WriteErrorResponse(
|
||||
s.WriteErrorResponse(
|
||||
req,
|
||||
w,
|
||||
NewInvalidParamsError(
|
||||
fmt.Sprintf("Invalid method '%s', please retry with 'POST'", httpRequest.Method), nil,
|
||||
|
@ -86,13 +85,13 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
|||
|
||||
err := req.DecodeData(httpRequest.Body)
|
||||
if err != nil {
|
||||
req.WriteErrorResponse(w, NewParseError("Problem parsing JSON-RPC request body", err))
|
||||
s.WriteErrorResponse(req, w, NewParseError("Problem parsing JSON-RPC request body", err))
|
||||
return
|
||||
}
|
||||
|
||||
reqParams, err := req.Params()
|
||||
if err != nil {
|
||||
req.WriteErrorResponse(w, NewInvalidParamsError("Problem parsing request parameters", err))
|
||||
s.WriteErrorResponse(req, w, NewInvalidParamsError("Problem parsing request parameters", err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -100,10 +99,9 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
|||
}
|
||||
|
||||
func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
|
||||
log.WithFields(log.Fields{
|
||||
"method": req.Method,
|
||||
"params": fmt.Sprintf("%v", reqParams),
|
||||
}).Info("processing rpc request")
|
||||
s.log.Info("processing rpc request",
|
||||
zap.String("method", req.Method),
|
||||
zap.String("params", fmt.Sprintf("%v", reqParams)))
|
||||
|
||||
var (
|
||||
results interface{}
|
||||
|
@ -268,11 +266,11 @@ Methods:
|
|||
}
|
||||
|
||||
if resultsErr != nil {
|
||||
req.WriteErrorResponse(w, resultsErr)
|
||||
s.WriteErrorResponse(req, w, resultsErr)
|
||||
return
|
||||
}
|
||||
|
||||
req.WriteResponse(w, results)
|
||||
s.WriteResponse(req, w, results)
|
||||
}
|
||||
|
||||
func (s *Server) getrawtransaction(reqParams Params) (interface{}, error) {
|
||||
|
|
|
@ -181,7 +181,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
|
|||
require.NoError(t, err, "could not load config")
|
||||
|
||||
memoryStore := storage.NewMemoryStore()
|
||||
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, zaptest.NewLogger(t))
|
||||
logger := zaptest.NewLogger(t)
|
||||
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, logger)
|
||||
require.NoError(t, err, "could not create chain")
|
||||
|
||||
go chain.Run()
|
||||
|
@ -199,8 +200,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
|
|||
}
|
||||
|
||||
serverConfig := network.NewServerConfig(cfg)
|
||||
server := network.NewServer(serverConfig, chain, zaptest.NewLogger(t))
|
||||
rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
|
||||
server := network.NewServer(serverConfig, chain, logger)
|
||||
rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
||||
handler := http.HandlerFunc(rpcServer.requestHandler)
|
||||
|
||||
return chain, handler
|
||||
|
|
Loading…
Reference in a new issue