Skip to content

Commit

Permalink
Expose server.process() via server.Do()
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyfast authored and dizzyfool committed Sep 13, 2019
1 parent 75fab97 commit 4b07723
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s Server) ServeWS(w http.ResponseWriter, r *http.Request) {
break
}

data, err := json.Marshal(s.process(newRequestContext(r.Context(), r), message))
data, err := s.Do(newRequestContext(r.Context(), r), message)
if err != nil {
s.printf("marshal json response failed with err=%v", err)
c.WriteControl(websocket.CloseInternalServerErr, nil, time.Time{})
Expand Down
9 changes: 7 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (s *Server) SetLogger(printer Printer) {

// process process JSON-RPC 2.0 message, invokes correct method for namespace and returns JSON-RPC 2.0 Response.
func (s *Server) process(ctx context.Context, message json.RawMessage) interface{} {
requests := []Request{}
var requests []Request
// parsing batch requests
batch := IsArray(message)

Expand Down Expand Up @@ -247,6 +247,11 @@ func (s Server) processRequest(ctx context.Context, req Request) Response {
return resp
}

// Do process JSON-RPC 2.0 request, invokes correct method for namespace and returns JSON-RPC 2.0 Response or marshaller error.
func (s Server) Do(ctx context.Context, req []byte) ([]byte, error) {
return json.Marshal(s.process(ctx, req))
}

func (s Server) printf(format string, v ...interface{}) {
if s.logger != nil {
s.logger.Printf(format, v...)
Expand Down Expand Up @@ -300,7 +305,7 @@ func IsArray(message json.RawMessage) bool {
func ConvertToObject(keys []string, params json.RawMessage) (json.RawMessage, error) {
paramCount := len(keys)

rawParams := []json.RawMessage{}
var rawParams []json.RawMessage
if err := json.Unmarshal(params, &rawParams); err != nil {
return nil, err
}
Expand Down

0 comments on commit 4b07723

Please sign in to comment.