chore: use go-chi for rest server instead of archived gorilla/rpc

- RPC server is not modified since it's going to be deprecated
This commit is contained in:
Richard Ramos 2023-02-16 16:05:24 -04:00 committed by RichΛrd
parent f0eaa84aad
commit 9731810b8d
3 changed files with 17 additions and 17 deletions

View File

@ -3,13 +3,13 @@ package rest
import ( import (
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/go-chi/chi/v5"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
) )
type DebugService struct { type DebugService struct {
node *node.WakuNode node *node.WakuNode
mux *mux.Router mux *chi.Mux
} }
type InfoArgs struct { type InfoArgs struct {
@ -23,14 +23,14 @@ type InfoReply struct {
const ROUTE_DEBUG_INFOV1 = "/debug/v1/info" const ROUTE_DEBUG_INFOV1 = "/debug/v1/info"
const ROUTE_DEBUG_VERSIONV1 = "/debug/v1/info" const ROUTE_DEBUG_VERSIONV1 = "/debug/v1/info"
func NewDebugService(node *node.WakuNode, m *mux.Router) *DebugService { func NewDebugService(node *node.WakuNode, m *chi.Mux) *DebugService {
d := &DebugService{ d := &DebugService{
node: node, node: node,
mux: m, mux: m,
} }
m.HandleFunc(ROUTE_DEBUG_INFOV1, d.getV1Info).Methods(http.MethodGet) m.Get(ROUTE_DEBUG_INFOV1, d.getV1Info)
m.HandleFunc(ROUTE_DEBUG_VERSIONV1, d.getV1Version).Methods(http.MethodGet) m.Get(ROUTE_DEBUG_VERSIONV1, d.getV1Version)
return d return d
} }

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/go-chi/chi/v5"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol"
@ -20,7 +21,6 @@ const ROUTE_RELAY_MESSAGESV1 = "/relay/v1/messages/{topic}"
type RelayService struct { type RelayService struct {
node *node.WakuNode node *node.WakuNode
mux *mux.Router
cancel context.CancelFunc cancel context.CancelFunc
log *zap.Logger log *zap.Logger
@ -32,10 +32,9 @@ type RelayService struct {
runner *runnerService runner *runnerService
} }
func NewRelayService(node *node.WakuNode, m *mux.Router, cacheCapacity int, log *zap.Logger) *RelayService { func NewRelayService(node *node.WakuNode, m *chi.Mux, cacheCapacity int, log *zap.Logger) *RelayService {
s := &RelayService{ s := &RelayService{
node: node, node: node,
mux: m,
log: log.Named("relay"), log: log.Named("relay"),
cacheCapacity: cacheCapacity, cacheCapacity: cacheCapacity,
messages: make(map[string][]*pb.WakuMessage), messages: make(map[string][]*pb.WakuMessage),
@ -43,10 +42,10 @@ func NewRelayService(node *node.WakuNode, m *mux.Router, cacheCapacity int, log
s.runner = newRunnerService(node.Broadcaster(), s.addEnvelope) s.runner = newRunnerService(node.Broadcaster(), s.addEnvelope)
m.HandleFunc(ROUTE_RELAY_SUBSCRIPTIONSV1, s.postV1Subscriptions).Methods(http.MethodPost) m.Post(ROUTE_RELAY_SUBSCRIPTIONSV1, s.postV1Subscriptions)
m.HandleFunc(ROUTE_RELAY_SUBSCRIPTIONSV1, s.deleteV1Subscriptions).Methods(http.MethodDelete) m.Delete(ROUTE_RELAY_SUBSCRIPTIONSV1, s.deleteV1Subscriptions)
m.HandleFunc(ROUTE_RELAY_MESSAGESV1, s.getV1Messages).Methods(http.MethodGet) m.Get(ROUTE_RELAY_MESSAGESV1, s.getV1Messages)
m.HandleFunc(ROUTE_RELAY_MESSAGESV1, s.postV1Message).Methods(http.MethodPost) m.Post(ROUTE_RELAY_MESSAGESV1, s.postV1Message)
return s return s
} }

View File

@ -4,10 +4,10 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"net/http/pprof"
"sync" "sync"
"github.com/gorilla/mux" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -25,11 +25,12 @@ func NewWakuRest(node *node.WakuNode, address string, port int, enableAdmin bool
wrpc := new(WakuRest) wrpc := new(WakuRest)
wrpc.log = log.Named("rest") wrpc.log = log.Named("rest")
mux := mux.NewRouter() mux := chi.NewRouter()
mux.Use(middleware.Logger)
mux.Use(middleware.NoCache)
if enablePProf { if enablePProf {
mux.PathPrefix("/debug/").Handler(http.DefaultServeMux) mux.Mount("/debug", middleware.Profiler())
mux.HandleFunc("/debug/pprof/", pprof.Index)
} }
_ = NewDebugService(node, mux) _ = NewDebugService(node, mux)