2019-06-20 12:52:32 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-24 16:15:31 +01:00
|
|
|
"encoding/json"
|
2019-06-20 12:52:32 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2019-06-24 16:15:31 +01:00
|
|
|
"time"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-24 16:15:31 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
"storj.io/storj/internal/version"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-20 12:52:32 +01:00
|
|
|
"storj.io/storj/storagenode/console"
|
|
|
|
)
|
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
const (
|
|
|
|
contentType = "Content-Type"
|
|
|
|
|
|
|
|
applicationJSON = "application/json"
|
|
|
|
)
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
// Error is storagenode console web error type
|
2019-06-24 16:15:31 +01:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
Error = errs.Class("storagenode console web error")
|
|
|
|
)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
// Config contains configuration for storagenode console web server
|
|
|
|
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:""`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server represents storagenode console web server
|
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
config Config
|
|
|
|
service *console.Service
|
|
|
|
listener net.Listener
|
|
|
|
|
|
|
|
server http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates new instance of storagenode console web server
|
|
|
|
func NewServer(logger *zap.Logger, config Config, service *console.Service, listener net.Listener) *Server {
|
|
|
|
server := Server{
|
|
|
|
log: logger,
|
|
|
|
service: service,
|
|
|
|
config: config,
|
|
|
|
listener: listener,
|
|
|
|
}
|
|
|
|
|
|
|
|
var fs http.Handler
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// handle static pages
|
|
|
|
if config.StaticDir != "" {
|
|
|
|
fs = http.FileServer(http.Dir(server.config.StaticDir))
|
|
|
|
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs))
|
|
|
|
mux.Handle("/", http.HandlerFunc(server.appHandler))
|
2019-06-24 16:15:31 +01:00
|
|
|
mux.Handle("/api/dashboard/", http.HandlerFunc(server.dashboardHandler))
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
server.server = http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &server
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the server that host webapp and api endpoints
|
|
|
|
func (s *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()
|
|
|
|
return s.server.Shutdown(nil)
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
|
|
|
return s.server.Serve(s.listener)
|
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes server and underlying listener
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.server.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// appHandler is web app http handler function
|
|
|
|
func (s *Server) appHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.ServeFile(w, req, filepath.Join(s.config.StaticDir, "dist", "index.html"))
|
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
|
|
|
// appHandler is web app http handler function
|
|
|
|
func (s *Server) dashboardHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Data struct {
|
|
|
|
Bandwidth console.BandwidthInfo `json:"bandwidth"`
|
|
|
|
DiskSpace console.DiskSpaceInfo `json:"diskSpace"`
|
|
|
|
WalletAddress string `json:"walletAddress"`
|
|
|
|
VersionInfo version.Info `json:"versionInfo"`
|
|
|
|
IsLastVersion bool `json:"isLastVersion"`
|
|
|
|
Uptime time.Duration `json:"uptime"`
|
|
|
|
NodeID string `json:"nodeId"`
|
|
|
|
Satellites storj.NodeIDList `json:"satellites"`
|
|
|
|
} `json:"data"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := json.NewEncoder(w).Encode(&response)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-26 14:36:47 +01:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
satelliteIDParam := req.URL.Query().Get("satelliteId")
|
|
|
|
satelliteID, err := s.parseSatelliteIDParam(satelliteIDParam)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("satellite id is not valid", zap.Error(err))
|
|
|
|
response.Error = "satellite id is not valid"
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
satellites, err := s.service.GetSatellites(ctx)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can not get satellites list", zap.Error(err))
|
|
|
|
response.Error = "can not get satellites list"
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:36:47 +01:00
|
|
|
// checks if current satellite id is related to current storage node
|
2019-06-24 16:15:31 +01:00
|
|
|
if satelliteID != nil {
|
|
|
|
if err = s.checkSatelliteID(satellites, *satelliteID); err != nil {
|
|
|
|
s.log.Error(err.Error())
|
|
|
|
response.Error = err.Error()
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
space, err := s.getStorage(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can not get disk space usage", zap.Error(err))
|
|
|
|
response.Error = "can not get disk space usage"
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usage, err := s.getBandwidth(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can not get bandwidth usage", zap.Error(err))
|
|
|
|
response.Error = "can not get bandwidth usage"
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
walletAddress := s.service.GetWalletAddress(ctx)
|
|
|
|
|
|
|
|
versionInfo := s.service.GetVersion(ctx)
|
|
|
|
|
|
|
|
err = s.service.CheckVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can not check latest storage node version", zap.Error(err))
|
|
|
|
response.Error = "can not check latest storage node version"
|
2019-06-26 14:36:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uptime := s.service.GetUptime(ctx)
|
|
|
|
nodeID := s.service.GetNodeID(ctx)
|
|
|
|
|
|
|
|
response.Data.DiskSpace = *space
|
|
|
|
response.Data.Bandwidth = *usage
|
|
|
|
response.Data.WalletAddress = walletAddress
|
|
|
|
response.Data.VersionInfo = versionInfo
|
|
|
|
response.Data.IsLastVersion = true
|
|
|
|
response.Data.Uptime = uptime
|
|
|
|
response.Data.NodeID = nodeID.String()
|
|
|
|
response.Data.Satellites = satellites
|
2019-06-26 14:36:47 +01:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getBandwidth(ctx context.Context, satelliteID *storj.NodeID) (_ *console.BandwidthInfo, err error) {
|
|
|
|
if satelliteID != nil {
|
|
|
|
return s.service.GetBandwidthBySatellite(ctx, *satelliteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.service.GetUsedBandwidthTotal(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getStorage(ctx context.Context, satelliteID *storj.NodeID) (_ *console.DiskSpaceInfo, err error) {
|
|
|
|
if satelliteID != nil {
|
|
|
|
return s.service.GetUsedStorageBySatellite(ctx, *satelliteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.service.GetUsedStorageTotal(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) checkSatelliteID(satelliteIDs storj.NodeIDList, satelliteID storj.NodeID) error {
|
|
|
|
for _, id := range satelliteIDs {
|
|
|
|
if satelliteID == id {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs.New("satellite id in not found in the available satellite list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) parseSatelliteIDParam(satelliteID string) (*storj.NodeID, error) {
|
|
|
|
if satelliteID != "" {
|
|
|
|
id, err := storj.NodeIDFromString(satelliteID)
|
|
|
|
return &id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|