From 6e44784255ef0d4a564691aa45fb3a6fbbdae009 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 9 Aug 2022 10:15:16 -0400 Subject: [PATCH] feat(REST): debug (#266) --- go.mod | 1 + go.sum | 1 + waku/node.go | 2 +- waku/v2/rpc/debug.go | 38 ++++++++++++++++++++++++++++++++- waku/v2/rpc/debug_api.yaml | 43 ++++++++++++++++++++++++++++++++++++++ waku/v2/rpc/utils.go | 16 ++++++++++++++ waku/v2/rpc/waku_rpc.go | 18 +++++++++------- 7 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 waku/v2/rpc/debug_api.yaml diff --git a/go.mod b/go.mod index 0f532644..fe87d6ca 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( require ( github.com/flynn/noise v1.0.0 + github.com/gorilla/mux v1.8.0 golang.org/x/text v0.3.7 ) diff --git a/go.sum b/go.sum index a7c060ec..fc803428 100644 --- a/go.sum +++ b/go.sum @@ -794,6 +794,7 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= diff --git a/waku/node.go b/waku/node.go index 97ca8cdb..f8fed39f 100644 --- a/waku/node.go +++ b/waku/node.go @@ -161,7 +161,7 @@ func Execute(options Options) { } if options.Version { - fmt.Printf("version / git commit hash: %s(%s)\n", node.Version, node.GitCommit) + fmt.Printf("version / git commit hash: %s-%s\n", node.Version, node.GitCommit) return } diff --git a/waku/v2/rpc/debug.go b/waku/v2/rpc/debug.go index 4f0d15e2..cc2cafe0 100644 --- a/waku/v2/rpc/debug.go +++ b/waku/v2/rpc/debug.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + "github.com/gorilla/mux" "github.com/status-im/go-waku/waku/v2/node" ) @@ -19,6 +20,19 @@ type InfoReply struct { ListenAddresses []string `json:"listenAddresses,omitempty"` } +// a.Router.HandleFunc("/products", a.getProducts).Methods("GET") + +func NewDebugService(node *node.WakuNode, m *mux.Router) *DebugService { + d := &DebugService{ + node: node, + } + + m.HandleFunc("/debug/v1/info", d.restGetV1Info).Methods("GET") + m.HandleFunc("/debug/v1/version", d.restGetV1Version).Methods("GET") + + return d +} + func (d *DebugService) GetV1Info(r *http.Request, args *InfoArgs, reply *InfoReply) error { reply.ENRUri = d.node.ENR().String() for _, addr := range d.node.ListenAddresses() { @@ -30,6 +44,28 @@ func (d *DebugService) GetV1Info(r *http.Request, args *InfoArgs, reply *InfoRep type VersionResponse string func (d *DebugService) GetV1Version(r *http.Request, args *InfoArgs, reply *VersionResponse) error { - *reply = VersionResponse(fmt.Sprintf("%s(%s)", node.Version, node.GitCommit)) + *reply = VersionResponse(fmt.Sprintf("%s-%s", node.Version, node.GitCommit)) return nil } + +func (d *DebugService) restGetV1Info(w http.ResponseWriter, r *http.Request) { + response := new(InfoReply) + err := d.GetV1Info(r, nil, response) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + writeResponse(w, response) +} + +func (d *DebugService) restGetV1Version(w http.ResponseWriter, r *http.Request) { + response := new(VersionResponse) + err := d.GetV1Version(r, nil, response) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + writeResponse(w, response) +} diff --git a/waku/v2/rpc/debug_api.yaml b/waku/v2/rpc/debug_api.yaml new file mode 100644 index 00000000..1a17ee90 --- /dev/null +++ b/waku/v2/rpc/debug_api.yaml @@ -0,0 +1,43 @@ +openapi: 3.0.3 +info: + title: Waku V2 node REST API + version: 1.0.0 + contact: + name: VAC Team + url: https://forum.vac.dev/ + +tags: + - name: debug + description: Debug REST API for WakuV2 node + +paths: + /debug/v1/info: + get: + summary: Get node info + description: Retrieve information about a Waku v2 node. + operationId: getNodeInfo + tags: + - debug + responses: + '200': + description: Information about a Waku v2 node. + content: + application/json: + schema: + $ref: '#/components/schemas/WakuInfo' + '5XX': + description: Unexpected error. + +components: + schemas: + WakuInfo: + type: object + properties: + listenAddresses: + type: array + items: + type: string + enrUri: + type: string + required: + - listenAddresses diff --git a/waku/v2/rpc/utils.go b/waku/v2/rpc/utils.go index 96fa1446..bd2531f7 100644 --- a/waku/v2/rpc/utils.go +++ b/waku/v2/rpc/utils.go @@ -2,7 +2,9 @@ package rpc import ( "encoding/hex" + "encoding/json" "fmt" + "net/http" "strings" "github.com/status-im/go-waku/waku/v2/protocol/pb" @@ -133,3 +135,17 @@ func (h *ByteArray) UnmarshalText(b []byte) error { return nil } + +func writeResponse(w http.ResponseWriter, value interface{}) { + jsonResponse, err := json.Marshal(value) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + _, err = w.Write(jsonResponse) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } +} diff --git a/waku/v2/rpc/waku_rpc.go b/waku/v2/rpc/waku_rpc.go index b1e0845e..c75e4b8f 100644 --- a/waku/v2/rpc/waku_rpc.go +++ b/waku/v2/rpc/waku_rpc.go @@ -6,6 +6,7 @@ import ( "net/http" "time" + "github.com/gorilla/mux" "github.com/gorilla/rpc/v2" "github.com/status-im/go-waku/waku/v2/node" "go.uber.org/zap" @@ -31,7 +32,15 @@ func NewWakuRpc(node *node.WakuNode, address string, port int, enableAdmin bool, s.RegisterCodec(NewSnakeCaseCodec(), "application/json") s.RegisterCodec(NewSnakeCaseCodec(), "application/json;charset=UTF-8") - err := s.RegisterService(&DebugService{node}, "Debug") + mux := mux.NewRouter() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + t := time.Now() + s.ServeHTTP(w, r) + wrpc.log.Info("served request", zap.String("path", r.URL.Path), zap.Duration("duration", time.Since(t))) + }) + + debugService := NewDebugService(node, mux) + err := s.RegisterService(debugService, "Debug") if err != nil { wrpc.log.Error("registering debug service", zap.Error(err)) } @@ -71,13 +80,6 @@ func NewWakuRpc(node *node.WakuNode, address string, port int, enableAdmin bool, wrpc.privateService = privateService } - mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - t := time.Now() - s.ServeHTTP(w, r) - wrpc.log.Info("served request", zap.String("path", r.URL.Path), zap.Duration("duration", time.Since(t))) - }) - listenAddr := fmt.Sprintf("%s:%d", address, port) server := &http.Server{