2020-09-22 15:51:34 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-11 10:04:25 +01:00
|
|
|
"errors"
|
2021-01-21 16:52:19 +00:00
|
|
|
"html/template"
|
2021-07-12 16:11:37 +01:00
|
|
|
"io/ioutil"
|
2020-09-22 15:51:34 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2020-11-11 22:29:29 +00:00
|
|
|
|
2021-10-11 10:04:25 +01:00
|
|
|
"storj.io/common/errs2"
|
2021-06-02 13:13:42 +01:00
|
|
|
"storj.io/storj/multinode/bandwidth"
|
2020-11-20 12:40:32 +00:00
|
|
|
"storj.io/storj/multinode/console/controllers"
|
2020-12-09 16:34:37 +00:00
|
|
|
"storj.io/storj/multinode/nodes"
|
2021-06-01 08:50:18 +01:00
|
|
|
"storj.io/storj/multinode/operators"
|
2021-02-05 11:37:59 +00:00
|
|
|
"storj.io/storj/multinode/payouts"
|
2021-06-25 12:09:54 +01:00
|
|
|
"storj.io/storj/multinode/reputation"
|
2021-06-03 12:52:28 +01:00
|
|
|
"storj.io/storj/multinode/storage"
|
2020-09-22 15:51:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Error is an error class for internal Multinode Dashboard http server error.
|
2021-04-28 09:06:17 +01:00
|
|
|
Error = errs.Class("multinode console server")
|
2020-09-22 15:51:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configuration for Multinode Dashboard http server.
|
|
|
|
type Config struct {
|
|
|
|
Address string `json:"address" help:"server address of the api gateway and frontend app" default:"127.0.0.1:15002"`
|
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:52:28 +01:00
|
|
|
// Services contains services utilized by multinode dashboard.
|
|
|
|
type Services struct {
|
2021-06-25 12:09:54 +01:00
|
|
|
Nodes *nodes.Service
|
|
|
|
Payouts *payouts.Service
|
|
|
|
Operators *operators.Service
|
|
|
|
Storage *storage.Service
|
|
|
|
Bandwidth *bandwidth.Service
|
|
|
|
Reputation *reputation.Service
|
2021-06-03 12:52:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 15:51:34 +01:00
|
|
|
// Server represents Multinode Dashboard http server.
|
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
|
|
|
type Server struct {
|
2021-06-03 12:52:28 +01:00
|
|
|
log *zap.Logger
|
|
|
|
listener net.Listener
|
|
|
|
http http.Server
|
2021-07-12 16:11:37 +01:00
|
|
|
assets http.FileSystem
|
2020-09-22 15:51:34 +01:00
|
|
|
|
2021-06-25 12:09:54 +01:00
|
|
|
nodes *nodes.Service
|
|
|
|
payouts *payouts.Service
|
|
|
|
operators *operators.Service
|
|
|
|
bandwidth *bandwidth.Service
|
|
|
|
storage *storage.Service
|
|
|
|
reputation *reputation.Service
|
2021-01-21 16:52:19 +00:00
|
|
|
|
|
|
|
index *template.Template
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns new instance of Multinode Dashboard http server.
|
2021-07-12 16:11:37 +01:00
|
|
|
func NewServer(log *zap.Logger, listener net.Listener, assets http.FileSystem, services Services) (*Server, error) {
|
2020-09-22 15:51:34 +01:00
|
|
|
server := Server{
|
2021-06-25 12:09:54 +01:00
|
|
|
log: log,
|
|
|
|
listener: listener,
|
2021-07-12 16:11:37 +01:00
|
|
|
assets: assets,
|
2021-06-25 12:09:54 +01:00
|
|
|
nodes: services.Nodes,
|
|
|
|
operators: services.Operators,
|
|
|
|
payouts: services.Payouts,
|
|
|
|
storage: services.Storage,
|
|
|
|
bandwidth: services.Bandwidth,
|
|
|
|
reputation: services.Reputation,
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2021-01-21 16:52:19 +00:00
|
|
|
|
2020-11-11 22:29:29 +00:00
|
|
|
apiRouter := router.PathPrefix("/api/v0").Subrouter()
|
2020-11-20 12:40:32 +00:00
|
|
|
apiRouter.NotFoundHandler = controllers.NewNotFound(server.log)
|
2020-12-09 16:34:37 +00:00
|
|
|
|
|
|
|
nodesController := controllers.NewNodes(server.log, server.nodes)
|
|
|
|
nodesRouter := apiRouter.PathPrefix("/nodes").Subrouter()
|
|
|
|
nodesRouter.HandleFunc("", nodesController.Add).Methods(http.MethodPost)
|
2020-12-26 01:16:43 +00:00
|
|
|
nodesRouter.HandleFunc("/infos", nodesController.ListInfos).Methods(http.MethodGet)
|
2021-01-05 07:59:22 +00:00
|
|
|
nodesRouter.HandleFunc("/infos/{satelliteID}", nodesController.ListInfosSatellite).Methods(http.MethodGet)
|
2021-01-07 23:26:31 +00:00
|
|
|
nodesRouter.HandleFunc("/trusted-satellites", nodesController.TrustedSatellites).Methods(http.MethodGet)
|
2020-11-20 12:40:32 +00:00
|
|
|
nodesRouter.HandleFunc("/{id}", nodesController.Get).Methods(http.MethodGet)
|
2020-12-09 16:34:37 +00:00
|
|
|
nodesRouter.HandleFunc("/{id}", nodesController.UpdateName).Methods(http.MethodPatch)
|
2020-11-20 12:40:32 +00:00
|
|
|
nodesRouter.HandleFunc("/{id}", nodesController.Delete).Methods(http.MethodDelete)
|
2020-11-11 22:29:29 +00:00
|
|
|
|
2021-06-01 08:50:18 +01:00
|
|
|
operatorsController := controllers.NewOperators(server.log, server.operators)
|
|
|
|
operatorsRouter := apiRouter.PathPrefix("/operators").Subrouter()
|
|
|
|
operatorsRouter.HandleFunc("", operatorsController.ListPaginated).Methods(http.MethodGet)
|
|
|
|
|
2021-06-02 13:13:42 +01:00
|
|
|
bandwidthController := controllers.NewBandwidth(server.log, server.bandwidth)
|
|
|
|
bandwidthRouter := apiRouter.PathPrefix("/bandwidth").Subrouter()
|
|
|
|
bandwidthRouter.HandleFunc("/", bandwidthController.Monthly).Methods(http.MethodGet)
|
|
|
|
bandwidthRouter.HandleFunc("/{nodeID}", bandwidthController.MonthlyNode).Methods(http.MethodGet)
|
|
|
|
bandwidthRouter.HandleFunc("/satellites/{id}", bandwidthController.MonthlySatellite).Methods(http.MethodGet)
|
|
|
|
bandwidthRouter.HandleFunc("/satellites/{id}/{nodeID}", bandwidthController.MonthlySatelliteNode).Methods(http.MethodGet)
|
|
|
|
|
2021-02-05 11:37:59 +00:00
|
|
|
payoutsController := controllers.NewPayouts(server.log, server.payouts)
|
|
|
|
payoutsRouter := apiRouter.PathPrefix("/payouts").Subrouter()
|
2021-05-26 11:16:24 +01:00
|
|
|
payoutsRouter.HandleFunc("/summaries", payoutsController.Summary).Methods(http.MethodGet)
|
2021-05-25 17:48:45 +01:00
|
|
|
payoutsRouter.HandleFunc("/summaries/{period}", payoutsController.SummaryPeriod).Methods(http.MethodGet)
|
2021-05-24 18:13:47 +01:00
|
|
|
payoutsRouter.HandleFunc("/expectations", payoutsController.Expectations).Methods(http.MethodGet)
|
2021-05-26 11:16:24 +01:00
|
|
|
payoutsRouter.HandleFunc("/expectations/{nodeID}", payoutsController.NodeExpectations).Methods(http.MethodGet)
|
|
|
|
payoutsRouter.HandleFunc("/paystubs/{nodeID}", payoutsController.Paystub).Methods(http.MethodGet)
|
|
|
|
payoutsRouter.HandleFunc("/paystubs/{period}/{nodeID}", payoutsController.PaystubPeriod).Methods(http.MethodGet)
|
2021-05-25 17:48:45 +01:00
|
|
|
payoutsRouter.HandleFunc("/total-earned", payoutsController.Earned).Methods(http.MethodGet)
|
2021-04-22 19:50:42 +01:00
|
|
|
payoutsRouter.HandleFunc("/held-amounts/{nodeID}", payoutsController.HeldAmountSummary).Methods(http.MethodGet)
|
2021-05-25 17:48:45 +01:00
|
|
|
payoutsRouter.HandleFunc("/satellites/{id}/summaries", payoutsController.SummarySatellite).Methods(http.MethodGet)
|
|
|
|
payoutsRouter.HandleFunc("/satellites/{id}/summaries/{period}", payoutsController.SummarySatellitePeriod).Methods(http.MethodGet)
|
|
|
|
payoutsRouter.HandleFunc("/satellites/{id}/paystubs/{nodeID}", payoutsController.PaystubSatellite).Methods(http.MethodGet)
|
|
|
|
payoutsRouter.HandleFunc("/satellites/{id}/paystubs/{period}/{nodeID}", payoutsController.PaystubSatellitePeriod).Methods(http.MethodGet)
|
2021-02-05 11:37:59 +00:00
|
|
|
|
2021-06-03 12:52:28 +01:00
|
|
|
storageController := controllers.NewStorage(server.log, server.storage)
|
|
|
|
storageRouter := apiRouter.PathPrefix("/storage").Subrouter()
|
|
|
|
storageRouter.HandleFunc("/usage", storageController.TotalUsage).Methods(http.MethodGet)
|
|
|
|
storageRouter.HandleFunc("/usage/{nodeID}", storageController.Usage).Methods(http.MethodGet)
|
|
|
|
storageRouter.HandleFunc("/satellites/{satelliteID}/usage", storageController.TotalUsageSatellite).Methods(http.MethodGet)
|
|
|
|
storageRouter.HandleFunc("/satellites/{satelliteID}/usage/{nodeID}", storageController.UsageSatellite).Methods(http.MethodGet)
|
2021-06-14 13:03:21 +01:00
|
|
|
storageRouter.HandleFunc("/disk-space", storageController.TotalDiskSpace).Methods(http.MethodGet)
|
|
|
|
storageRouter.HandleFunc("/disk-space/{nodeID}", storageController.DiskSpace).Methods(http.MethodGet)
|
2021-06-03 12:52:28 +01:00
|
|
|
|
2021-06-25 12:09:54 +01:00
|
|
|
reputationController := controllers.NewReputation(server.log, server.reputation)
|
|
|
|
reputationRouter := apiRouter.PathPrefix("/reputation").Subrouter()
|
|
|
|
reputationRouter.HandleFunc("/satellites/{satelliteID}", reputationController.Stats)
|
|
|
|
|
2021-07-12 16:11:37 +01:00
|
|
|
if server.assets != nil {
|
|
|
|
fs := http.FileServer(server.assets)
|
2021-01-21 16:52:19 +00:00
|
|
|
router.PathPrefix("/static/").Handler(http.StripPrefix("/static", fs))
|
|
|
|
router.PathPrefix("/").HandlerFunc(server.appHandler)
|
|
|
|
}
|
|
|
|
|
2020-11-11 22:29:29 +00:00
|
|
|
server.http = http.Server{
|
2020-09-22 15:51:34 +01:00
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &server, nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:52:19 +00:00
|
|
|
// appHandler is web app http handler function.
|
|
|
|
func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
header := w.Header()
|
|
|
|
|
|
|
|
header.Set("Content-Type", "text/html; charset=UTF-8")
|
|
|
|
header.Set("X-Content-Type-Options", "nosniff")
|
|
|
|
header.Set("Referrer-Policy", "same-origin")
|
|
|
|
|
|
|
|
if server.index == nil {
|
|
|
|
server.log.Error("index template is not set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.index.Execute(w, nil); err != nil {
|
|
|
|
server.log.Error("index template could not be executed", zap.Error(Error.Wrap(err)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 15:51:34 +01:00
|
|
|
// Run starts the server that host webapp and api endpoints.
|
|
|
|
func (server *Server) Run(ctx context.Context) (err error) {
|
2021-01-21 16:52:19 +00:00
|
|
|
err = server.initializeTemplates()
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2020-11-11 22:29:29 +00:00
|
|
|
|
2021-01-21 16:52:19 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-09-22 15:51:34 +01:00
|
|
|
var group errgroup.Group
|
2020-11-11 22:29:29 +00:00
|
|
|
|
2020-09-22 15:51:34 +01:00
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2020-11-11 22:29:29 +00:00
|
|
|
return Error.Wrap(server.http.Shutdown(context.Background()))
|
2020-09-22 15:51:34 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2021-10-11 10:04:25 +01:00
|
|
|
err := Error.Wrap(server.http.Serve(server.listener))
|
|
|
|
if errs2.IsCanceled(err) || errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
2020-09-22 15:51:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return Error.Wrap(group.Wait())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes server and underlying listener.
|
|
|
|
func (server *Server) Close() error {
|
2020-11-11 22:29:29 +00:00
|
|
|
return Error.Wrap(server.http.Close())
|
2020-09-22 15:51:34 +01:00
|
|
|
}
|
2021-01-21 16:52:19 +00:00
|
|
|
|
|
|
|
// initializeTemplates is used to initialize all templates.
|
|
|
|
func (server *Server) initializeTemplates() (err error) {
|
2021-07-12 16:11:37 +01:00
|
|
|
f, err := server.assets.Open("/dist/index.html")
|
2021-01-21 16:52:19 +00:00
|
|
|
if err != nil {
|
|
|
|
server.log.Error("dist folder is not generated. use 'npm run build' command", zap.Error(err))
|
2021-07-12 16:11:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.index, err = template.New("index.html").Parse(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2021-01-21 16:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 18:05:49 +01:00
|
|
|
return nil
|
2021-01-21 16:52:19 +00:00
|
|
|
}
|