feat(REST): debug (#266)

This commit is contained in:
Richard Ramos 2022-08-09 10:15:16 -04:00 committed by GitHub
parent dec00e69ad
commit 6e44784255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 109 additions and 10 deletions

1
go.mod
View File

@ -40,6 +40,7 @@ require (
require ( require (
github.com/flynn/noise v1.0.0 github.com/flynn/noise v1.0.0
github.com/gorilla/mux v1.8.0
golang.org/x/text v0.3.7 golang.org/x/text v0.3.7
) )

1
go.sum
View File

@ -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.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.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.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/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 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk=
github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ=

View File

@ -161,7 +161,7 @@ func Execute(options Options) {
} }
if options.Version { 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 return
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/gorilla/mux"
"github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/node"
) )
@ -19,6 +20,19 @@ type InfoReply struct {
ListenAddresses []string `json:"listenAddresses,omitempty"` 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 { func (d *DebugService) GetV1Info(r *http.Request, args *InfoArgs, reply *InfoReply) error {
reply.ENRUri = d.node.ENR().String() reply.ENRUri = d.node.ENR().String()
for _, addr := range d.node.ListenAddresses() { for _, addr := range d.node.ListenAddresses() {
@ -30,6 +44,28 @@ func (d *DebugService) GetV1Info(r *http.Request, args *InfoArgs, reply *InfoRep
type VersionResponse string type VersionResponse string
func (d *DebugService) GetV1Version(r *http.Request, args *InfoArgs, reply *VersionResponse) error { 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 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)
}

View File

@ -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

View File

@ -2,7 +2,9 @@ package rpc
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"net/http"
"strings" "strings"
"github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/pb"
@ -133,3 +135,17 @@ func (h *ByteArray) UnmarshalText(b []byte) error {
return nil 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
}
}

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/gorilla/mux"
"github.com/gorilla/rpc/v2" "github.com/gorilla/rpc/v2"
"github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/node"
"go.uber.org/zap" "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")
s.RegisterCodec(NewSnakeCaseCodec(), "application/json;charset=UTF-8") 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 { if err != nil {
wrpc.log.Error("registering debug service", zap.Error(err)) 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 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) listenAddr := fmt.Sprintf("%s:%d", address, port)
server := &http.Server{ server := &http.Server{