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-08-14 13:17:11 +01:00
|
|
|
"strings"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-07-17 12:42:00 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
"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-08-14 13:17:11 +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
|
|
|
|
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-06-20 12:52:32 +01:00
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
config Config
|
|
|
|
service *console.Service
|
|
|
|
listener net.Listener
|
|
|
|
|
|
|
|
server http.Server
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// NewServer creates new instance of storagenode console web server.
|
2019-06-20 12:52:32 +01:00
|
|
|
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-08-14 13:17:11 +01:00
|
|
|
// handle api endpoints
|
|
|
|
mux.Handle("/api/dashboard", http.HandlerFunc(server.dashboardHandler))
|
|
|
|
mux.Handle("/api/satellites", http.HandlerFunc(server.satellitesHandler))
|
|
|
|
mux.Handle("/api/satellite/", http.HandlerFunc(server.satelliteHandler))
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
server.server = http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-17 12:42:00 +01:00
|
|
|
return server.server.Shutdown(nil)
|
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-08-14 13:17:11 +01:00
|
|
|
// appHandler is web app http handler function.
|
|
|
|
func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, filepath.Join(server.config.StaticDir, "dist", "index.html"))
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// dashboardHandler handles dashboard API requests.
|
|
|
|
func (server *Server) dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2019-06-24 16:15:31 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-06-26 14:36:47 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
data, err := server.service.GetDashboardData(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeError(w, http.StatusInternalServerError, Error.Wrap(err))
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeData(w, data)
|
2019-07-17 12:42:00 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// satelliteHandler handles satellites API request.
|
|
|
|
func (server *Server) satellitesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
data, err := server.service.GetAllSatellitesData(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeError(w, http.StatusInternalServerError, Error.Wrap(err))
|
|
|
|
return
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeData(w, data)
|
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// satelliteHandler handles satellite API requests.
|
|
|
|
func (server *Server) satelliteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
satelliteID, err := storj.NodeIDFromString(strings.TrimLeft(r.URL.Path, "/api/satellite/"))
|
2019-07-17 12:42:00 +01:00
|
|
|
if err != nil {
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeError(w, http.StatusBadRequest, Error.Wrap(err))
|
|
|
|
return
|
2019-07-17 12:42:00 +01:00
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if err = server.service.VerifySatelliteID(ctx, satelliteID); err != nil {
|
|
|
|
server.writeError(w, http.StatusNotFound, Error.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
2019-06-26 14:36:47 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
data, err := server.service.GetSatelliteData(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
server.writeError(w, http.StatusInternalServerError, Error.Wrap(err))
|
|
|
|
return
|
2019-07-17 12:42:00 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
server.writeData(w, data)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// jsonOutput defines json structure of api response data.
|
|
|
|
type jsonOutput struct {
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
2019-07-17 12:42:00 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// writeData is helper method to write JSON to http.ResponseWriter and log encoding error.
|
|
|
|
func (server *Server) writeData(w http.ResponseWriter, data interface{}) {
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
output := jsonOutput{Data: data}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(output); err != nil {
|
|
|
|
server.log.Error("json encoder error", zap.Error(err))
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
// writeError writes a JSON error payload to http.ResponseWriter log encoding error.
|
|
|
|
func (server *Server) writeError(w http.ResponseWriter, status int, err error) {
|
|
|
|
if status >= http.StatusInternalServerError {
|
|
|
|
server.log.Error("api handler server error", zap.Int("status code", status), zap.Error(err))
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
w.WriteHeader(status)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
output := jsonOutput{Error: err.Error()}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-08-14 13:17:11 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(output); err != nil {
|
|
|
|
server.log.Error("json encoder error", zap.Error(err))
|
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|