2021-11-02 09:54:34 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-12-11 00:08:25 +00:00
|
|
|
"net/http/pprof"
|
2021-11-06 10:49:47 +00:00
|
|
|
"time"
|
2021-11-02 09:54:34 +00:00
|
|
|
|
2022-08-09 14:15:16 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-11-02 09:54:34 +00:00
|
|
|
"github.com/gorilla/rpc/v2"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-02 09:54:34 +00:00
|
|
|
)
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
type WakuRPC struct {
|
2021-11-02 09:54:34 +00:00
|
|
|
node *node.WakuNode
|
|
|
|
server *http.Server
|
2021-11-18 14:20:58 +00:00
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2022-01-18 18:17:06 +00:00
|
|
|
|
2023-08-04 13:07:43 +00:00
|
|
|
relayService *RelayService
|
|
|
|
filterService *FilterService
|
|
|
|
adminService *AdminService
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func NewWakuRPC(node *node.WakuNode, address string, port int, enableAdmin bool, enablePProf bool, cacheCapacity int, log *zap.Logger) *WakuRPC {
|
|
|
|
wrpc := new(WakuRPC)
|
2022-01-18 18:17:06 +00:00
|
|
|
wrpc.log = log.Named("rpc")
|
|
|
|
|
2021-11-02 09:54:34 +00:00
|
|
|
s := rpc.NewServer()
|
|
|
|
s.RegisterCodec(NewSnakeCaseCodec(), "application/json")
|
|
|
|
s.RegisterCodec(NewSnakeCaseCodec(), "application/json;charset=UTF-8")
|
|
|
|
|
2022-08-09 14:15:16 +00:00
|
|
|
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)))
|
|
|
|
})
|
|
|
|
|
2022-12-11 00:08:25 +00:00
|
|
|
if enablePProf {
|
|
|
|
mux.PathPrefix("/debug/").Handler(http.DefaultServeMux)
|
|
|
|
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
debugService := NewDebugService(node)
|
2022-08-09 14:15:16 +00:00
|
|
|
err := s.RegisterService(debugService, "Debug")
|
2021-11-02 09:54:34 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
wrpc.log.Error("registering debug service", zap.Error(err))
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:59:23 +00:00
|
|
|
var relayService *RelayService
|
|
|
|
if node.Relay() != nil {
|
|
|
|
relayService = NewRelayService(node, cacheCapacity, log)
|
|
|
|
err = s.RegisterService(relayService, "Relay")
|
|
|
|
if err != nil {
|
|
|
|
wrpc.log.Error("registering relay service", zap.Error(err))
|
|
|
|
}
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:29:31 +00:00
|
|
|
err = s.RegisterService(&StoreService{node, log}, "Store")
|
2021-11-09 14:15:45 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
wrpc.log.Error("registering store service", zap.Error(err))
|
2021-11-09 14:15:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:47:28 +00:00
|
|
|
if enableAdmin {
|
|
|
|
adminService := &AdminService{node, log.Named("admin")}
|
|
|
|
err = s.RegisterService(adminService, "Admin")
|
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
wrpc.log.Error("registering admin service", zap.Error(err))
|
2022-05-06 19:47:28 +00:00
|
|
|
}
|
|
|
|
wrpc.adminService = adminService
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 13:18:23 +00:00
|
|
|
filterService := NewFilterService(node, cacheCapacity, log)
|
2021-11-19 14:17:38 +00:00
|
|
|
err = s.RegisterService(filterService, "Filter")
|
2021-11-17 16:04:54 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
wrpc.log.Error("registering filter service", zap.Error(err))
|
2021-11-17 16:04:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 09:54:34 +00:00
|
|
|
listenAddr := fmt.Sprintf("%s:%d", address, port)
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: listenAddr,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:17:38 +00:00
|
|
|
server.RegisterOnShutdown(func() {
|
|
|
|
filterService.Stop()
|
2023-01-03 14:59:23 +00:00
|
|
|
|
|
|
|
if relayService != nil {
|
|
|
|
relayService.Stop()
|
|
|
|
}
|
2021-11-19 14:17:38 +00:00
|
|
|
})
|
|
|
|
|
2022-01-18 18:17:06 +00:00
|
|
|
wrpc.node = node
|
|
|
|
wrpc.server = server
|
|
|
|
wrpc.relayService = relayService
|
|
|
|
wrpc.filterService = filterService
|
|
|
|
|
|
|
|
return wrpc
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *WakuRPC) Start() {
|
2023-01-03 14:59:23 +00:00
|
|
|
if r.relayService != nil {
|
|
|
|
go r.relayService.Start()
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:17:38 +00:00
|
|
|
go r.filterService.Start()
|
2022-12-11 00:08:25 +00:00
|
|
|
|
2021-11-19 14:17:38 +00:00
|
|
|
go func() {
|
|
|
|
_ = r.server.ListenAndServe()
|
|
|
|
}()
|
2022-05-30 15:55:30 +00:00
|
|
|
r.log.Info("server started", zap.String("addr", r.server.Addr))
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *WakuRPC) Stop(ctx context.Context) error {
|
2022-05-30 15:55:30 +00:00
|
|
|
r.log.Info("shutting down server")
|
2021-11-02 09:54:34 +00:00
|
|
|
return r.server.Shutdown(ctx)
|
|
|
|
}
|