2018-05-15 18:27:11 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2018-06-04 21:49:06 +00:00
|
|
|
"context"
|
2018-05-15 18:27:11 +00:00
|
|
|
nethttp "net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
idpctx "github.com/influxdata/platform/context"
|
2018-07-11 23:24:16 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-05-15 18:27:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PlatformHandler is a collection of all the service handlers.
|
|
|
|
type PlatformHandler struct {
|
|
|
|
BucketHandler *BucketHandler
|
|
|
|
UserHandler *UserHandler
|
|
|
|
OrgHandler *OrgHandler
|
|
|
|
AuthorizationHandler *AuthorizationHandler
|
2018-05-31 14:44:23 +00:00
|
|
|
DashboardHandler *DashboardHandler
|
2018-05-15 18:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setCORSResponseHeaders(w nethttp.ResponseWriter, r *nethttp.Request) {
|
|
|
|
if origin := r.Header.Get("Origin"); origin != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP delegates a request to the appropriate subhandler.
|
|
|
|
func (h *PlatformHandler) ServeHTTP(w nethttp.ResponseWriter, r *nethttp.Request) {
|
|
|
|
|
|
|
|
setCORSResponseHeaders(w, r)
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
2018-06-04 21:49:06 +00:00
|
|
|
var err error
|
|
|
|
if ctx, err = extractAuthorization(ctx, r); err != nil {
|
|
|
|
nethttp.Error(w, err.Error(), nethttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2018-05-15 18:27:11 +00:00
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/v1/buckets") {
|
|
|
|
h.BucketHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/v1/users") {
|
|
|
|
h.UserHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/v1/orgs") {
|
|
|
|
h.OrgHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/v1/authorizations") {
|
|
|
|
h.AuthorizationHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:44:23 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/v1/dashboards") {
|
|
|
|
h.DashboardHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:27:11 +00:00
|
|
|
nethttp.NotFound(w, r)
|
|
|
|
}
|
2018-06-04 21:49:06 +00:00
|
|
|
|
2018-07-11 23:24:16 +00:00
|
|
|
// PrometheusCollectors satisfies the prom.PrometheusCollector interface.
|
|
|
|
func (h *PlatformHandler) PrometheusCollectors() []prometheus.Collector {
|
|
|
|
// TODO: collect and return relevant metrics.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:49:06 +00:00
|
|
|
func extractAuthorization(ctx context.Context, r *nethttp.Request) (context.Context, error) {
|
|
|
|
t, err := ParseAuthHeaderToken(r)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
return idpctx.SetToken(ctx, t), nil
|
|
|
|
}
|