2022-07-24 20:51:42 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-12-07 17:34:56 +00:00
|
|
|
"sync"
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-02-16 20:05:24 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2022-07-24 20:51:42 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WakuRest struct {
|
|
|
|
node *node.WakuNode
|
|
|
|
server *http.Server
|
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
|
2023-11-04 07:24:20 +00:00
|
|
|
relayService *RelayService
|
|
|
|
filterService *FilterService
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 14:56:48 +00:00
|
|
|
type RestConfig struct {
|
|
|
|
Address string
|
|
|
|
Port uint
|
|
|
|
EnablePProf bool
|
|
|
|
EnableAdmin bool
|
|
|
|
RelayCacheCapacity uint
|
|
|
|
FilterCacheCapacity uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWakuRest(node *node.WakuNode, config RestConfig, log *zap.Logger) *WakuRest {
|
2022-07-24 20:51:42 +00:00
|
|
|
wrpc := new(WakuRest)
|
|
|
|
wrpc.log = log.Named("rest")
|
|
|
|
|
2023-02-16 20:05:24 +00:00
|
|
|
mux := chi.NewRouter()
|
|
|
|
mux.Use(middleware.Logger)
|
|
|
|
mux.Use(middleware.NoCache)
|
2024-03-26 13:43:50 +00:00
|
|
|
mux.Use(func(h http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
})
|
2023-11-07 14:56:48 +00:00
|
|
|
if config.EnablePProf {
|
2023-02-16 20:05:24 +00:00
|
|
|
mux.Mount("/debug", middleware.Profiler())
|
2022-12-11 00:08:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
_ = NewDebugService(node, mux)
|
2023-09-11 21:34:56 +00:00
|
|
|
_ = NewHealthService(node, mux)
|
2023-04-10 20:39:49 +00:00
|
|
|
_ = NewStoreService(node, mux)
|
2023-10-23 23:23:33 +00:00
|
|
|
_ = NewLightpushService(node, mux, log)
|
2023-04-10 20:39:49 +00:00
|
|
|
|
2023-11-07 14:56:48 +00:00
|
|
|
listenAddr := fmt.Sprintf("%s:%d", config.Address, config.Port)
|
2022-07-24 20:51:42 +00:00
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: listenAddr,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
wrpc.node = node
|
|
|
|
wrpc.server = server
|
2023-01-03 14:59:23 +00:00
|
|
|
|
|
|
|
if node.Relay() != nil {
|
2023-11-07 14:56:48 +00:00
|
|
|
relayService := NewRelayService(node, mux, config.RelayCacheCapacity, log)
|
2023-01-03 14:59:23 +00:00
|
|
|
wrpc.relayService = relayService
|
|
|
|
}
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-11-07 14:56:48 +00:00
|
|
|
if config.EnableAdmin {
|
2023-10-24 19:25:04 +00:00
|
|
|
_ = NewAdminService(node, mux, wrpc.log)
|
|
|
|
}
|
|
|
|
|
2023-10-26 23:21:50 +00:00
|
|
|
if node.FilterLightnode() != nil {
|
2023-11-07 14:56:48 +00:00
|
|
|
filterService := NewFilterService(node, mux, int(config.FilterCacheCapacity), log)
|
2023-11-04 07:24:20 +00:00
|
|
|
server.RegisterOnShutdown(func() {
|
|
|
|
filterService.Stop()
|
|
|
|
})
|
|
|
|
wrpc.filterService = filterService
|
2023-10-26 23:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
return wrpc
|
|
|
|
}
|
|
|
|
|
2022-12-10 16:21:22 +00:00
|
|
|
func (r *WakuRest) Start(ctx context.Context, wg *sync.WaitGroup) {
|
2022-12-07 17:34:56 +00:00
|
|
|
defer wg.Done()
|
2023-01-03 14:59:23 +00:00
|
|
|
|
2023-11-04 07:24:20 +00:00
|
|
|
if r.node.FilterLightnode() != nil {
|
|
|
|
go r.filterService.Start(ctx)
|
|
|
|
}
|
2023-01-03 14:59:23 +00:00
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
go func() {
|
|
|
|
_ = r.server.ListenAndServe()
|
|
|
|
}()
|
|
|
|
r.log.Info("server started", zap.String("addr", r.server.Addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WakuRest) Stop(ctx context.Context) error {
|
|
|
|
r.log.Info("shutting down server")
|
|
|
|
return r.server.Shutdown(ctx)
|
|
|
|
}
|