2019-06-20 12:52:32 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-02-03 13:32:09 +00:00
|
|
|
"path/filepath"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
"storj.io/storj/storagenode/console"
|
2020-03-15 18:30:23 +00:00
|
|
|
"storj.io/storj/storagenode/console/consoleapi"
|
2020-03-13 14:01:12 +00:00
|
|
|
"storj.io/storj/storagenode/heldamount"
|
2019-12-17 15:38:55 +00:00
|
|
|
"storj.io/storj/storagenode/notifications"
|
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.
|
2019-06-24 16:15:31 +01:00
|
|
|
Error = errs.Class("storagenode console web error")
|
|
|
|
)
|
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
|
2020-03-13 14:01:12 +00:00
|
|
|
heldAmount *heldamount.Service
|
2019-12-17 15:38:55 +00:00
|
|
|
listener net.Listener
|
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.
|
2020-03-13 14:01:12 +00:00
|
|
|
func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifications.Service, service *console.Service, heldAmount *heldamount.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,
|
|
|
|
notifications: notifications,
|
2020-03-13 14:01:12 +00:00
|
|
|
heldAmount: heldAmount,
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
heldAmountController := consoleapi.NewHeldAmount(server.log, server.heldAmount)
|
|
|
|
heldAmountRouter := router.PathPrefix("/api/heldamount").Subrouter()
|
|
|
|
heldAmountRouter.StrictSlash(true)
|
2020-03-16 01:32:52 +00:00
|
|
|
heldAmountRouter.HandleFunc("/paystub/{period}/{satelliteID}", heldAmountController.SatellitePayStubMonthly).Methods(http.MethodGet)
|
|
|
|
heldAmountRouter.HandleFunc("/paystub/{period}", heldAmountController.AllPayStubsMonthly).Methods(http.MethodGet)
|
2020-03-15 18:30:23 +00:00
|
|
|
heldAmountRouter.HandleFunc("/payment/{period}/{satelliteID}", heldAmountController.GetMonthlyPayment).Methods(http.MethodGet)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-10-08 09:52:19 +01:00
|
|
|
if assets != nil {
|
|
|
|
fs := http.FileServer(assets)
|
2019-11-15 12:36:43 +00:00
|
|
|
router.PathPrefix("/static/").Handler(server.cacheMiddleware(http.StripPrefix("/static", fs)))
|
|
|
|
router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-10-08 09:52:19 +01:00
|
|
|
req := r.Clone(r.Context())
|
|
|
|
req.URL.Path = "/dist/"
|
|
|
|
fs.ServeHTTP(w, req)
|
|
|
|
}))
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.server.Serve(server.listener)
|
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
|
|
|
}
|
|
|
|
|
2019-11-15 12:36:43 +00:00
|
|
|
// cacheMiddleware is a middleware for caching static files.
|
|
|
|
func (server *Server) cacheMiddleware(fn http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-02-03 13:32:09 +00:00
|
|
|
// "mime" package, which http.FileServer uses, depends on Operating System
|
|
|
|
// configuration for mime-types. When a system has hardcoded mime-types to
|
|
|
|
// something else, they might serve ".js" as a "plain/text".
|
|
|
|
//
|
|
|
|
// Override any of that default behavior to ensure we get the correct types for
|
|
|
|
// common files.
|
|
|
|
if contentType, ok := CommonContentType(filepath.Ext(r.URL.Path)); ok {
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:36:43 +00:00
|
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
|
|
|
fn.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|