2018-11-15 12:00:08 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
package satelliteweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/graphql-go/graphql"
|
2018-11-27 14:20:58 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GatewayConfig contains configuration for gateway
|
|
|
|
type GatewayConfig struct {
|
|
|
|
Address string `help:"server address of the graphql api gateway and frontend app" default:"127.0.0.1:8081"`
|
|
|
|
StaticPath string `help:"path to static resources" default:""`
|
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// gateway hosts api endpoints and web app
|
2018-11-14 10:50:15 +00:00
|
|
|
type gateway struct {
|
2018-11-27 14:20:58 +00:00
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
service *satellite.Service
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
schema graphql.Schema
|
|
|
|
config GatewayConfig
|
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// run starts http server
|
2018-11-14 10:50:15 +00:00
|
|
|
func (gw *gateway) run() {
|
|
|
|
mux := http.NewServeMux()
|
2018-11-27 13:14:10 +00:00
|
|
|
//gw.config.StaticPath = "./web/satellite"
|
2018-11-14 10:50:15 +00:00
|
|
|
fs := http.FileServer(http.Dir(gw.config.StaticPath))
|
|
|
|
|
|
|
|
mux.Handle("/api/graphql/v0", http.HandlerFunc(gw.grapqlHandler))
|
|
|
|
|
|
|
|
if gw.config.StaticPath != "" {
|
|
|
|
mux.Handle("/", http.HandlerFunc(gw.appHandler))
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := http.ListenAndServe(gw.config.Address, mux)
|
2018-11-21 15:51:43 +00:00
|
|
|
gw.log.Error("unexpected exit of satellite gateway server: ", zap.Error(err))
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// appHandler is web app http handler function
|
2018-11-14 10:50:15 +00:00
|
|
|
func (gw *gateway) appHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.ServeFile(w, req, filepath.Join(gw.config.StaticPath, "dist", "public", "index.html"))
|
|
|
|
}
|