/
handler.go
49 lines (40 loc) · 1.37 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package jsonrpc2
import (
"encoding/json"
)
// Handler handles an individual RPC call.
type Handler interface {
// ServeRPC is invoked when an RPC call is received. If a response is needed
// for a request, null will be sent as the response result. If the request is
// a notification, ResponseWriter must NOT be used, even for delivering an
// error.
//
// Written responses may not be delivered right away if the request is a batch
// request.
ServeRPC(w ResponseWriter, r *Request)
}
type ResponseWriter interface {
// WriteMessage writes a success response to the client. The value as provided
// here will be marshaled to json. An error will be returned if the msg could
// not be marshaled to JSON.
WriteMessage(msg interface{}) error
// WriteError writes an error response to the caller.
WriteError(errorCode int, err error) error
}
type Request struct {
// Notification indicates an asynchronous call from the server and responses
// should not be written.
Notification bool
Method string
Params json.RawMessage
Client *Client
}
// HandlerFunc implements Handler.
type HandlerFunc func(w ResponseWriter, r *Request)
// ServeRPC implements Handler.
func (f HandlerFunc) ServeRPC(w ResponseWriter, r *Request) {
f(w, r)
}
// DefaultHandler is the default handler used by a server. It returns
// ErrorMethodNotFound for each RPC.
var DefaultHandler = NewServeMux()