1f452e67dc
* Adding dockerfile for running the web UI for Satellite * Updating to work with Makefile and from root directory of repo * Updating satellite ui build process to run in a more production like mode by generating the assets the pulling those into the satellite container * Updates to allow external traffic to UI, updates to storagenode for identity creation, and logging for bug tracking * Adding auto cert generation for storagenode * removing satellite-ui-image from main images flow in Makefile and adding latest tag to docker build for it * Adding solid defaults, tuning dockerfiles, and moving to standard logging methods * Updating logging to be more standard * Updating to logger.Debug * Removing unused library and unused identity creation code Change-Id: I956453037e303693ea37f94318180af0ab7984d5
153 lines
3.5 KiB
Go
153 lines
3.5 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package consoleweb
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"storj.io/storj/pkg/auth"
|
|
"storj.io/storj/satellite/console"
|
|
"storj.io/storj/satellite/console/consoleweb/consoleql"
|
|
)
|
|
|
|
const (
|
|
authorization = "Authorization"
|
|
contentType = "Content-Type"
|
|
|
|
authorizationBearer = "Bearer "
|
|
|
|
applicationJSON = "application/json"
|
|
applicationGraphql = "application/graphql"
|
|
)
|
|
|
|
// Error is satellite console error type
|
|
var Error = errs.Class("satellite console error")
|
|
|
|
// Config contains configuration for console web server
|
|
type Config struct {
|
|
Address string `help:"server address of the graphql api gateway and frontend app" default:"127.0.0.1:8081"`
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
PasswordCost int `internal:"true" help:"password hashing cost (0=automatic)" default:"0"`
|
|
}
|
|
|
|
// Server represents console web server
|
|
type Server struct {
|
|
log *zap.Logger
|
|
|
|
config Config
|
|
service *console.Service
|
|
listener net.Listener
|
|
|
|
schema graphql.Schema
|
|
server http.Server
|
|
}
|
|
|
|
// NewServer creates new instance of console 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,
|
|
}
|
|
|
|
logger.Debug("Starting Satellite UI...")
|
|
|
|
mux := http.NewServeMux()
|
|
fs := http.FileServer(http.Dir(server.config.StaticDir))
|
|
|
|
mux.Handle("/api/graphql/v0", http.HandlerFunc(server.grapqlHandler))
|
|
|
|
if server.config.StaticDir != "" {
|
|
mux.Handle("/", http.HandlerFunc(server.appHandler))
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs))
|
|
}
|
|
|
|
server.server = http.Server{
|
|
Handler: mux,
|
|
}
|
|
|
|
return &server
|
|
}
|
|
|
|
// 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", "public", "index.html"))
|
|
}
|
|
|
|
// grapqlHandler is graphql endpoint http handler function
|
|
func (s *Server) grapqlHandler(w http.ResponseWriter, req *http.Request) {
|
|
w.Header().Set(contentType, applicationJSON)
|
|
|
|
token := getToken(req)
|
|
query, err := getQuery(req)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := auth.WithAPIKey(context.Background(), []byte(token))
|
|
auth, err := s.service.Authorize(ctx)
|
|
if err != nil {
|
|
ctx = console.WithAuthFailure(ctx, err)
|
|
} else {
|
|
ctx = console.WithAuth(ctx, auth)
|
|
}
|
|
|
|
result := graphql.Do(graphql.Params{
|
|
Schema: s.schema,
|
|
Context: ctx,
|
|
RequestString: query.Query,
|
|
VariableValues: query.Variables,
|
|
OperationName: query.OperationName,
|
|
RootObject: make(map[string]interface{}),
|
|
})
|
|
|
|
err = json.NewEncoder(w).Encode(result)
|
|
if err != nil {
|
|
s.log.Error(err.Error())
|
|
return
|
|
}
|
|
|
|
sugar := s.log.Sugar()
|
|
sugar.Debug(result)
|
|
}
|
|
|
|
// Run starts the server that host webapp and api endpoint
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
var err error
|
|
s.schema, err = consoleql.CreateSchema(s.service)
|
|
if err != nil {
|
|
return Error.Wrap(err)
|
|
}
|
|
|
|
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()
|
|
}
|