influxdb/http/platform_handler.go

70 lines
2.1 KiB
Go
Raw Normal View History

2018-05-15 18:27:11 +00:00
package http
import (
"net/http"
2018-05-15 18:27:11 +00:00
"strings"
"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 {
AssetHandler *AssetHandler
APIHandler http.Handler
2018-05-15 18:27:11 +00:00
}
func setCORSResponseHeaders(w http.ResponseWriter, r *http.Request) {
2018-05-15 18:27:11 +00:00
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")
}
}
// NewPlatformHandler returns a platform handler that serves the API and associated assets.
func NewPlatformHandler(b *APIBackend) *PlatformHandler {
h := NewAuthenticationHandler()
h.Handler = NewAPIHandler(b)
h.AuthorizationService = b.AuthorizationService
h.SessionService = b.SessionService
h.RegisterNoAuthRoute("GET", "/api/v2")
h.RegisterNoAuthRoute("POST", "/api/v2/signin")
h.RegisterNoAuthRoute("POST", "/api/v2/signout")
h.RegisterNoAuthRoute("POST", "/api/v2/setup")
h.RegisterNoAuthRoute("GET", "/api/v2/setup")
assetHandler := NewAssetHandler()
assetHandler.DeveloperMode = b.DeveloperMode
return &PlatformHandler{
AssetHandler: assetHandler,
APIHandler: h,
}
}
2018-05-15 18:27:11 +00:00
// ServeHTTP delegates a request to the appropriate subhandler.
func (h *PlatformHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2018-05-15 18:27:11 +00:00
setCORSResponseHeaders(w, r)
if r.Method == "OPTIONS" {
return
}
// Serve the chronograf assets for any basepath that does not start with addressable parts
// of the platform API.
if !strings.HasPrefix(r.URL.Path, "/v1") &&
!strings.HasPrefix(r.URL.Path, "/api/v2") &&
2018-09-28 15:47:47 +00:00
!strings.HasPrefix(r.URL.Path, "/chronograf/") {
h.AssetHandler.ServeHTTP(w, r)
return
}
h.APIHandler.ServeHTTP(w, r)
2018-05-15 18:27:11 +00:00
}
2018-06-04 21:49:06 +00:00
// PrometheusCollectors satisfies the prom.PrometheusCollector interface.
func (h *PlatformHandler) PrometheusCollectors() []prometheus.Collector {
// TODO: collect and return relevant metrics.
return nil
}