forked from TrueCloudLab/neoneo-go
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:
parent
de91418d45
commit
57de98e1a3
2 changed files with 9 additions and 9 deletions
|
@ -111,11 +111,11 @@ func (s *Server) Start(errChan chan error) {
|
||||||
s.log.Info("RPC server is not enabled")
|
s.log.Info("RPC server is not enabled")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.Handler = http.HandlerFunc(s.requestHandler)
|
s.Handler = http.HandlerFunc(s.handleHTTPRequest)
|
||||||
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
|
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
|
||||||
|
|
||||||
if cfg := s.config.TLSConfig; cfg.Enabled {
|
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))
|
s.log.Info("starting rpc-server (https)", zap.String("endpoint", s.https.Addr))
|
||||||
go func() {
|
go func() {
|
||||||
err := s.https.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile)
|
err := s.https.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile)
|
||||||
|
@ -149,7 +149,7 @@ func (s *Server) Shutdown() error {
|
||||||
return err
|
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()
|
req := request.NewIn()
|
||||||
|
|
||||||
if httpRequest.Method != "POST" {
|
if httpRequest.Method != "POST" {
|
||||||
|
@ -169,16 +169,16 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.handleRequest(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRequest(w http.ResponseWriter, req *request.In) {
|
||||||
reqParams, err := req.Params()
|
reqParams, err := req.Params()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.WriteErrorResponse(req, w, response.NewInvalidParamsError("Problem parsing request parameters", err))
|
s.WriteErrorResponse(req, w, response.NewInvalidParamsError("Problem parsing request parameters", err))
|
||||||
return
|
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",
|
s.log.Debug("processing rpc request",
|
||||||
zap.String("method", req.Method),
|
zap.String("method", req.Method),
|
||||||
zap.String("params", fmt.Sprintf("%v", reqParams)))
|
zap.String("params", fmt.Sprintf("%v", reqParams)))
|
||||||
|
@ -192,7 +192,7 @@ func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams
|
||||||
|
|
||||||
handler, ok := rpcHandlers[req.Method]
|
handler, ok := rpcHandlers[req.Method]
|
||||||
if ok {
|
if ok {
|
||||||
results, resultsErr = handler(s, reqParams)
|
results, resultsErr = handler(s, *reqParams)
|
||||||
} else {
|
} else {
|
||||||
resultsErr = response.NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil)
|
resultsErr = response.NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
|
||||||
server, err := network.NewServer(serverConfig, chain, logger)
|
server, err := network.NewServer(serverConfig, chain, logger)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
||||||
handler := http.HandlerFunc(rpcServer.requestHandler)
|
handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
|
||||||
|
|
||||||
return chain, handler
|
return chain, handler
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue