You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

  1. package jsonrpc
  2. import (
  3. "net/http"
  4. "reflect"
  5. "github.com/alecthomas/jsonschema"
  6. "github.com/intel-go/fastjson"
  7. )
  8. // A MethodReference is a reference of JSON-RPC method.
  9. type MethodReference struct {
  10. Name string `json:"name"`
  11. Handler string `json:"handler"`
  12. Params *jsonschema.Schema `json:"params,omitempty"`
  13. Result *jsonschema.Schema `json:"result,omitempty"`
  14. }
  15. // ServeDebug views registered method list.
  16. func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) {
  17. ms := mr.Methods()
  18. if len(ms) == 0 {
  19. w.WriteHeader(http.StatusNotFound)
  20. return
  21. }
  22. l := make([]*MethodReference, 0, len(ms))
  23. for k, md := range ms {
  24. l = append(l, makeMethodReference(k, md))
  25. }
  26. w.Header().Set(contentTypeKey, contentTypeValue)
  27. if err := fastjson.NewEncoder(w).Encode(l); err != nil {
  28. w.WriteHeader(http.StatusInternalServerError)
  29. return
  30. }
  31. }
  32. func makeMethodReference(k string, md Metadata) *MethodReference {
  33. mr := &MethodReference{
  34. Name: k,
  35. }
  36. tv := reflect.TypeOf(md.Handler)
  37. if tv.Kind() == reflect.Ptr {
  38. tv = tv.Elem()
  39. }
  40. mr.Handler = tv.Name()
  41. if md.Params != nil {
  42. mr.Params = jsonschema.Reflect(md.Params)
  43. }
  44. if md.Result != nil {
  45. mr.Result = jsonschema.Reflect(md.Result)
  46. }
  47. return mr
  48. }