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 (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/pkg/satellite/satelliteweb/satelliteql"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authorization = "Authorization"
|
|
|
|
contentType = "Content-Type"
|
|
|
|
|
|
|
|
authorizationBearer = "Bearer "
|
|
|
|
|
|
|
|
applicationJSON = "application/json"
|
|
|
|
applicationGraphql = "application/graphql"
|
|
|
|
)
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// JSON request from graphql clients
|
|
|
|
type graphqlJSON struct {
|
|
|
|
Query string
|
|
|
|
OperationName string
|
|
|
|
Variables map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
func (gw *gateway) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
result := graphql.Do(graphql.Params{
|
2018-11-16 14:34:35 +00:00
|
|
|
Schema: gw.schema,
|
|
|
|
Context: auth.WithAPIKey(context.Background(), []byte(token)),
|
|
|
|
RequestString: query.Query,
|
|
|
|
VariableValues: query.Variables,
|
|
|
|
OperationName: query.OperationName,
|
2018-11-14 10:50:15 +00:00
|
|
|
})
|
|
|
|
|
2018-11-14 15:13:13 +00:00
|
|
|
err = json.NewEncoder(w).Encode(result)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
gw.logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gw.logger.Debug(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getToken retrieves token from request
|
|
|
|
func getToken(req *http.Request) string {
|
|
|
|
value := req.Header.Get(authorization)
|
|
|
|
if value == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(value, authorizationBearer) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return value[len(authorizationBearer):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQuery retrieves graphql query from request
|
2018-11-16 14:34:35 +00:00
|
|
|
func getQuery(req *http.Request) (query graphqlJSON, err error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
2018-11-16 14:34:35 +00:00
|
|
|
query.Query = req.URL.Query().Get(satelliteql.Query)
|
|
|
|
return query, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
case http.MethodPost:
|
|
|
|
return queryPOST(req)
|
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("wrong http request type")
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// queryPOST retrieves graphql query from POST request
|
|
|
|
func queryPOST(req *http.Request) (query graphqlJSON, err error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
switch typ := req.Header.Get(contentType); typ {
|
|
|
|
case applicationGraphql:
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
2018-11-16 14:34:35 +00:00
|
|
|
query.Query = string(body)
|
|
|
|
return query, utils.CombineErrors(err, req.Body.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
case applicationJSON:
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&query)
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, utils.CombineErrors(err, req.Body.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("can't parse request body of type %s", typ)
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|