Skip to content

Commit 27adc85

Browse files
committed
add comments
1 parent fd61fa7 commit 27adc85

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

client.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ import (
1010
)
1111

1212
// Client implements remote engine and delegates all calls to remote http server
13+
// if AuthUser and AuthPasswd defined will be used for basic auth in each call to server
1314
type Client struct {
14-
API string // URL to jrpc server with entrypoint, i.e. http://127.0.0.1:8080/command
15-
Client http.Client
16-
AuthUser string
17-
AuthPasswd string
15+
API string // URL to jrpc server with entrypoint, i.e. http://127.0.0.1:8080/command
16+
Client http.Client // http client injected by user
17+
AuthUser string // basic auth user name, should match Server.AuthUser, optional
18+
AuthPasswd string // basic auth password, should match Server.AuthPasswd, optional
1819

19-
id uint64 // used with atomic
20+
id uint64 // used with atomic to populate unique id to Request.ID
2021
}
2122

22-
// Call remote server with given method and arguments
23+
// Call remote server with given method and arguments.
24+
// Empty args will be ignored, single arg will be marshaled as-us and multiple args marshalled as []interface{}.
25+
// Returns Response and error. Note: Response has it's own Error field, but that onw controlled by server.
26+
// Returned error represent client-level errors, like failed http call, failed marshaling and so on.
2327
func (r *Client) Call(method string, args ...interface{}) (*Response, error) {
2428

2529
var b []byte

jrpc.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010

1111
// Request encloses method name and all params
1212
type Request struct {
13-
Method string `json:"method"`
14-
Params interface{} `json:"params,omitempty"`
15-
ID uint64 `json:"id"`
13+
Method string `json:"method"` // method (function) name
14+
Params interface{} `json:"params,omitempty"` // function arguments
15+
ID uint64 `json:"id"` // unique call id
1616
}
1717

1818
// Response encloses result and error received from remote server
1919
type Response struct {
20-
Result *json.RawMessage `json:"result,omitempty"`
21-
Error string `json:"error,omitempty"`
22-
ID uint64 `json:"id"`
20+
Result *json.RawMessage `json:"result,omitempty"` // response json
21+
Error string `json:"error,omitempty"` // optional remote (server side / plugin side) error
22+
ID uint64 `json:"id"` // unique call id, echoed Request.ID to allow calls tracing
2323
}
2424

2525
// EncodeResponse convert anything (type interface{}) and incoming error (if any) to Response

server.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020

2121
// Server is json-rpc server with an optional basic auth
2222
type Server struct {
23-
API string
24-
AuthUser string
25-
AuthPasswd string
26-
Version string
27-
AppName string
28-
Logger L
29-
funcs struct {
23+
API string // url path, i.e. "/command" or "/rpc" etc.
24+
AuthUser string // basic auth user name, should match Client.AuthUser, optional
25+
AuthPasswd string // basic auth password, should match Client.AuthPasswd, optional
26+
Version string // server version, injected from main and used for informational headers only
27+
AppName string // plugin name, injected from main and used for informational headers only
28+
Logger L // logger, if nil will default to NoOpLogger
29+
30+
funcs struct {
3031
m map[string]ServerFn
3132
once sync.Once
3233
}
@@ -37,11 +38,8 @@ type Server struct {
3738
}
3839
}
3940

40-
// Encoder is a function to encode call's result to Response
41-
type Encoder func(id uint64, resp interface{}, e error) (Response, error)
42-
43-
// ServerFn handler registered for each method with Add
44-
// Implementations provided by consumer and define response logic.
41+
// ServerFn handler registered for each method with Add or Group.
42+
// Implementations provided by consumer and defines response logic.
4543
type ServerFn func(id uint64, params json.RawMessage) Response
4644

4745
// Run http server on given port
@@ -92,7 +90,7 @@ func (s *Server) Shutdown() error {
9290
return s.httpServer.Shutdown(ctx)
9391
}
9492

95-
// Add method handler
93+
// Add method handler. Handler will be called on matching method (Request.Method)
9694
func (s *Server) Add(method string, fn ServerFn) {
9795
s.httpServer.Lock()
9896
defer s.httpServer.Unlock()
@@ -112,13 +110,14 @@ func (s *Server) Add(method string, fn ServerFn) {
112110
// HandlersGroup alias for map of handlers
113111
type HandlersGroup map[string]ServerFn
114112

115-
// Group of handlers with common prefix
113+
// Group of handlers with common prefix, match on group.method
116114
func (s *Server) Group(prefix string, m HandlersGroup) {
117115
for k, v := range m {
118116
s.Add(prefix+"."+k, v)
119117
}
120118
}
121119

120+
// handler is http handler multiplexing calls by req.Method
122121
func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
123122
req := struct {
124123
ID uint64 `json:"id"`
@@ -144,6 +143,7 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
144143
render.JSON(w, r, fn(req.ID, params))
145144
}
146145

146+
// basicAuth middleware. enabled only if both AuthUser and AuthPasswd defined.
147147
func (s *Server) basicAuth(h http.Handler) http.Handler {
148148
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
149149

0 commit comments

Comments
 (0)