rpc/server: refactor handler methods a little

request.In is a natural request representation, one can always get
request.Params from it.
This commit is contained in:
Roman Khimov 2020-04-28 16:56:33 +03:00
parent 8f417c8e8b
commit 21c31d7d24
2 changed files with 9 additions and 9 deletions

View file

@ -110,11 +110,11 @@ func (s *Server) Start(errChan chan error) {
s.log.Info("RPC server is not enabled")
return
}
s.Handler = http.HandlerFunc(s.requestHandler)
s.Handler = http.HandlerFunc(s.handleHTTPRequest)
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
if cfg := s.config.TLSConfig; cfg.Enabled {
s.https.Handler = http.HandlerFunc(s.requestHandler)
s.https.Handler = http.HandlerFunc(s.handleHTTPRequest)
s.log.Info("starting rpc-server (https)", zap.String("endpoint", s.https.Addr))
go func() {
err := s.https.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile)
@ -148,7 +148,7 @@ func (s *Server) Shutdown() error {
return err
}
func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request) {
func (s *Server) handleHTTPRequest(w http.ResponseWriter, httpRequest *http.Request) {
req := request.NewIn()
if httpRequest.Method != "POST" {
@ -168,16 +168,16 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
return
}
s.handleRequest(w, req)
}
func (s *Server) handleRequest(w http.ResponseWriter, req *request.In) {
reqParams, err := req.Params()
if err != nil {
s.WriteErrorResponse(req, w, response.NewInvalidParamsError("Problem parsing request parameters", err))
return
}
s.methodHandler(w, req, *reqParams)
}
func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams request.Params) {
s.log.Debug("processing rpc request",
zap.String("method", req.Method),
zap.String("params", fmt.Sprintf("%v", reqParams)))
@ -191,7 +191,7 @@ func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams
handler, ok := rpcHandlers[req.Method]
if ok {
results, resultsErr = handler(s, reqParams)
results, resultsErr = handler(s, *reqParams)
} else {
resultsErr = response.NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil)
}

View file

@ -55,7 +55,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
server, err := network.NewServer(serverConfig, chain, logger)
require.NoError(t, err)
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
handler := http.HandlerFunc(rpcServer.requestHandler)
handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
return chain, handler
}