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
|
|
|
|
|
|
|
"github.com/gorilla/rpc/v2"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("wakurpc")
|
|
|
|
|
|
|
|
type WakuRpc struct {
|
|
|
|
node *node.WakuNode
|
|
|
|
server *http.Server
|
2021-11-18 14:20:58 +00:00
|
|
|
|
2021-11-19 14:17:38 +00:00
|
|
|
relayService *RelayService
|
|
|
|
filterService *FilterService
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
|
|
|
s := rpc.NewServer()
|
|
|
|
s.RegisterCodec(NewSnakeCaseCodec(), "application/json")
|
|
|
|
s.RegisterCodec(NewSnakeCaseCodec(), "application/json;charset=UTF-8")
|
|
|
|
|
2021-11-02 14:42:22 +00:00
|
|
|
err := s.RegisterService(&DebugService{node}, "Debug")
|
2021-11-02 09:54:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
relayService := NewRelayService(node)
|
|
|
|
err = s.RegisterService(relayService, "Relay")
|
2021-11-06 10:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:15:45 +00:00
|
|
|
err = s.RegisterService(&StoreService{node}, "Store")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:36:51 +00:00
|
|
|
err = s.RegisterService(&AdminService{node}, "Admin")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:17:38 +00:00
|
|
|
filterService := NewFilterService(node)
|
|
|
|
err = s.RegisterService(filterService, "Filter")
|
2021-11-17 16:04:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:21:36 +00:00
|
|
|
err = s.RegisterService(&PrivateService{node}, "Private")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:54:34 +00:00
|
|
|
mux := http.NewServeMux()
|
2021-11-06 10:49:47 +00:00
|
|
|
mux.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t := time.Now()
|
|
|
|
s.ServeHTTP(w, r)
|
|
|
|
log.Infof("RPC request at %s took %s", r.URL.Path, time.Since(t))
|
|
|
|
})
|
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()
|
|
|
|
})
|
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
return &WakuRpc{
|
2021-11-19 14:17:38 +00:00
|
|
|
node: node,
|
|
|
|
server: server,
|
|
|
|
relayService: relayService,
|
|
|
|
filterService: filterService,
|
2021-11-18 14:20:58 +00:00
|
|
|
}
|
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()
|
|
|
|
go func() {
|
|
|
|
_ = r.server.ListenAndServe()
|
|
|
|
}()
|
2021-11-02 09:54:34 +00:00
|
|
|
log.Info("Rpc server started at ", r.server.Addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WakuRpc) Stop(ctx context.Context) error {
|
2021-11-19 14:17:38 +00:00
|
|
|
log.Info("Shutting down rpc server")
|
2021-11-02 09:54:34 +00:00
|
|
|
return r.server.Shutdown(ctx)
|
|
|
|
}
|