2021-11-02 09:54:34 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
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"
|
|
|
|
"github.com/status-im/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
|
|
|
)
|
|
|
|
|
|
|
|
type WakuRpc struct {
|
|
|
|
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
|
|
|
|
2021-12-08 08:52:27 +00:00
|
|
|
relayService *RelayService
|
|
|
|
filterService *FilterService
|
|
|
|
privateService *PrivateService
|
2022-05-06 19:47:28 +00:00
|
|
|
adminService *AdminService
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
func NewWakuRpc(node *node.WakuNode, address string, port int, enableAdmin bool, enablePrivate bool, log *zap.Logger) *WakuRpc {
|
2022-01-18 18:17:06 +00:00
|
|
|
wrpc := new(WakuRpc)
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
relayService := NewRelayService(node, log)
|
2021-11-18 14:20:58 +00:00
|
|
|
err = s.RegisterService(relayService, "Relay")
|
2021-11-06 10:49:47 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
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-01-18 18:17:06 +00:00
|
|
|
filterService := NewFilterService(node, 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
|
|
|
}
|
|
|
|
|
2022-05-06 19:47:28 +00:00
|
|
|
if enablePrivate {
|
|
|
|
privateService := NewPrivateService(node, log)
|
|
|
|
err = s.RegisterService(privateService, "Private")
|
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
wrpc.log.Error("registering private service", zap.Error(err))
|
2022-05-06 19:47:28 +00:00
|
|
|
}
|
|
|
|
wrpc.privateService = privateService
|
2021-11-18 13:21:36 +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()
|
|
|
|
relayService.Stop()
|
2022-06-13 18:30:35 +00:00
|
|
|
if wrpc.privateService != nil {
|
|
|
|
wrpc.privateService.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
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WakuRpc) Start() {
|
2021-11-18 14:20:58 +00:00
|
|
|
go r.relayService.Start()
|
2021-11-19 14:17:38 +00:00
|
|
|
go r.filterService.Start()
|
2022-06-13 18:30:35 +00:00
|
|
|
if r.privateService != nil {
|
|
|
|
go r.privateService.Start()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|