2019-06-20 12:52:32 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-16 16:50:22 +01:00
|
|
|
"errors"
|
2022-03-11 13:47:20 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2019-06-20 12:52:32 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2019-11-15 12:36:43 +00:00
|
|
|
"github.com/gorilla/mux"
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-06-20 12:52:32 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2020-04-16 16:50:22 +01:00
|
|
|
"storj.io/common/errs2"
|
2022-03-11 09:19:11 +00:00
|
|
|
"storj.io/storj/private/web"
|
2019-06-20 12:52:32 +01:00
|
|
|
"storj.io/storj/storagenode/console"
|
2020-03-15 18:30:23 +00:00
|
|
|
"storj.io/storj/storagenode/console/consoleapi"
|
2019-12-17 15:38:55 +00:00
|
|
|
"storj.io/storj/storagenode/notifications"
|
2021-01-14 16:41:36 +00:00
|
|
|
"storj.io/storj/storagenode/payouts"
|
2019-06-20 12:52:32 +01:00
|
|
|
)
|
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
var (
|
2020-03-15 18:30:23 +00:00
|
|
|
mon = monkit.Package()
|
|
|
|
// Error is storagenode console web error type.
|
2021-04-28 09:06:17 +01:00
|
|
|
Error = errs.Class("consoleserver")
|
2019-06-24 16:15:31 +01:00
|
|
|
)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// Config contains configuration for storagenode console web server.
|
2019-06-20 12:52:32 +01:00
|
|
|
type Config struct {
|
|
|
|
Address string `help:"server address of the api gateway and frontend app" default:"127.0.0.1:14002"`
|
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// Server represents storagenode console web server.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-06-20 12:52:32 +01:00
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2019-12-17 15:38:55 +00:00
|
|
|
service *console.Service
|
|
|
|
notifications *notifications.Service
|
2021-01-14 16:41:36 +00:00
|
|
|
payout *payouts.Service
|
2019-12-17 15:38:55 +00:00
|
|
|
listener net.Listener
|
2022-03-11 13:47:20 +00:00
|
|
|
assets fs.FS
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
server http.Server
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// NewServer creates new instance of storagenode console web server.
|
2022-03-11 13:47:20 +00:00
|
|
|
func NewServer(logger *zap.Logger, assets fs.FS, notifications *notifications.Service, service *console.Service, payout *payouts.Service, listener net.Listener) *Server {
|
2019-06-20 12:52:32 +01:00
|
|
|
server := Server{
|
2019-12-17 15:38:55 +00:00
|
|
|
log: logger,
|
|
|
|
service: service,
|
|
|
|
listener: listener,
|
2022-03-11 13:47:20 +00:00
|
|
|
assets: assets,
|
2019-12-17 15:38:55 +00:00
|
|
|
notifications: notifications,
|
2020-09-10 15:08:25 +01:00
|
|
|
payout: payout,
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 12:36:43 +00:00
|
|
|
router := mux.NewRouter()
|
2020-03-15 18:30:23 +00:00
|
|
|
|
|
|
|
// handle api endpoints
|
|
|
|
storageNodeController := consoleapi.NewStorageNode(server.log, server.service)
|
|
|
|
storageNodeRouter := router.PathPrefix("/api/sno").Subrouter()
|
|
|
|
storageNodeRouter.StrictSlash(true)
|
|
|
|
storageNodeRouter.HandleFunc("/", storageNodeController.StorageNode).Methods(http.MethodGet)
|
|
|
|
storageNodeRouter.HandleFunc("/satellites", storageNodeController.Satellites).Methods(http.MethodGet)
|
|
|
|
storageNodeRouter.HandleFunc("/satellite/{id}", storageNodeController.Satellite).Methods(http.MethodGet)
|
2023-05-09 10:36:17 +01:00
|
|
|
storageNodeRouter.HandleFunc("/satellites/{id}/pricing", storageNodeController.Pricing).Methods(http.MethodGet)
|
2020-07-09 18:43:06 +01:00
|
|
|
storageNodeRouter.HandleFunc("/estimated-payout", storageNodeController.EstimatedPayout).Methods(http.MethodGet)
|
2020-03-15 18:30:23 +00:00
|
|
|
|
|
|
|
notificationController := consoleapi.NewNotifications(server.log, server.notifications)
|
2019-12-17 15:38:55 +00:00
|
|
|
notificationRouter := router.PathPrefix("/api/notifications").Subrouter()
|
2020-03-15 18:30:23 +00:00
|
|
|
notificationRouter.StrictSlash(true)
|
|
|
|
notificationRouter.HandleFunc("/list", notificationController.ListNotifications).Methods(http.MethodGet)
|
|
|
|
notificationRouter.HandleFunc("/{id}/read", notificationController.ReadNotification).Methods(http.MethodPost)
|
|
|
|
notificationRouter.HandleFunc("/readall", notificationController.ReadAllNotifications).Methods(http.MethodPost)
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
payoutController := consoleapi.NewPayout(server.log, server.payout)
|
|
|
|
payoutRouter := router.PathPrefix("/api/heldamount").Subrouter()
|
|
|
|
payoutRouter.StrictSlash(true)
|
|
|
|
payoutRouter.HandleFunc("/paystubs/{period}", payoutController.PayStubMonthly).Methods(http.MethodGet)
|
|
|
|
payoutRouter.HandleFunc("/paystubs/{start}/{end}", payoutController.PayStubPeriod).Methods(http.MethodGet)
|
|
|
|
payoutRouter.HandleFunc("/held-history", payoutController.HeldHistory).Methods(http.MethodGet)
|
|
|
|
payoutRouter.HandleFunc("/periods", payoutController.HeldAmountPeriods).Methods(http.MethodGet)
|
|
|
|
payoutRouter.HandleFunc("/payout-history/{period}", payoutController.PayoutHistory).Methods(http.MethodGet)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2022-03-11 13:47:20 +00:00
|
|
|
staticServer := http.FileServer(http.FS(server.assets))
|
|
|
|
router.PathPrefix("/static/").Handler(web.CacheHandler(staticServer))
|
|
|
|
router.PathPrefix("/").HandlerFunc(server.appHandler)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
server.server = http.Server{
|
2019-11-15 12:36:43 +00:00
|
|
|
Handler: router,
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &server
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:47:20 +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")
|
|
|
|
|
|
|
|
f, err := server.assets.Open("index.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, `web/storagenode unbuilt, run "npm install && npm run build" in web/storagenode.`, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = f.Close() }()
|
|
|
|
|
|
|
|
_, _ = io.Copy(w, f)
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// Run starts the server that host webapp and api endpoints.
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) Run(ctx context.Context) (err error) {
|
2019-06-24 16:15:31 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2019-08-22 12:40:15 +01:00
|
|
|
return server.server.Shutdown(context.Background())
|
2019-06-20 12:52:32 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2020-04-16 16:50:22 +01:00
|
|
|
err := server.server.Serve(server.listener)
|
|
|
|
if errs2.IsCanceled(err) || errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
2019-06-20 12:52:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// Close closes server and underlying listener.
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) Close() error {
|
|
|
|
return server.server.Close()
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|