mirror of https://github.com/status-im/go-waku.git
feat(REST): debug (#266)
This commit is contained in:
parent
dec00e69ad
commit
6e44784255
1
go.mod
1
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
|
||||
)
|
||||
|
||||
|
|
1
go.sum
1
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=
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Reference in New Issue