1
package jsonrpc
2

3
import (
4
	"net/http"
5
	"reflect"
6

7
	"github.com/alecthomas/jsonschema"
8
	"github.com/intel-go/fastjson"
9
)
10

11
// A MethodReference is a reference of JSON-RPC method.
12
type MethodReference struct {
13
	Name    string             `json:"name"`
14
	Handler string             `json:"handler"`
15
	Params  *jsonschema.Schema `json:"params,omitempty"`
16
	Result  *jsonschema.Schema `json:"result,omitempty"`
17
}
18

19
// ServeDebug views registered method list.
20 6
func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) { // nolint: unparam
21 6
	ms := mr.Methods()
22 6
	if len(ms) == 0 {
23 6
		w.WriteHeader(http.StatusNotFound)
24 6
		return
25
	}
26 6
	l := make([]*MethodReference, 0, len(ms))
27
	for k, md := range ms {
28 6
		l = append(l, makeMethodReference(k, md))
29
	}
30 6
	w.Header().Set(contentTypeKey, contentTypeValue)
31 6
	if err := fastjson.NewEncoder(w).Encode(l); err != nil {
32 0
		w.WriteHeader(http.StatusInternalServerError)
33 0
		return
34
	}
35
}
36

37
func makeMethodReference(k string, md Metadata) *MethodReference {
38 6
	mr := &MethodReference{
39 6
		Name: k,
40
	}
41 6
	tv := reflect.TypeOf(md.Handler)
42 6
	if tv.Kind() == reflect.Ptr {
43 6
		tv = tv.Elem()
44
	}
45 6
	mr.Handler = tv.Name()
46 6
	if md.Params != nil {
47 6
		mr.Params = jsonschema.Reflect(md.Params)
48
	}
49 6
	if md.Result != nil {
50 6
		mr.Result = jsonschema.Reflect(md.Result)
51
	}
52 6
	return mr
53
}

Read our documentation on viewing source code .

Loading