2018-05-15 18:27:11 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2018-10-02 16:00:29 +00:00
|
|
|
"net/http"
|
2018-05-15 18:27:11 +00:00
|
|
|
"strings"
|
|
|
|
|
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 {
|
2018-10-02 16:00:29 +00:00
|
|
|
AssetHandler *AssetHandler
|
|
|
|
APIHandler http.Handler
|
2018-05-15 18:27:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
// NewPlatformHandler returns a platform handler that serves the API and associated assets.
|
|
|
|
func NewPlatformHandler(b *APIBackend) *PlatformHandler {
|
|
|
|
h := NewAuthenticationHandler()
|
|
|
|
h.Handler = NewAPIHandler(b)
|
2018-10-02 20:10:41 +00:00
|
|
|
h.AuthorizationService = b.AuthorizationService
|
|
|
|
h.SessionService = b.SessionService
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
h.RegisterNoAuthRoute("GET", "/api/v2")
|
2018-10-02 17:12:07 +00:00
|
|
|
h.RegisterNoAuthRoute("POST", "/api/v2/signin")
|
|
|
|
h.RegisterNoAuthRoute("POST", "/api/v2/signout")
|
|
|
|
h.RegisterNoAuthRoute("POST", "/api/v2/setup")
|
|
|
|
h.RegisterNoAuthRoute("GET", "/api/v2/setup")
|
2018-07-31 22:50:02 +00:00
|
|
|
|
2018-12-21 21:17:18 +00:00
|
|
|
assetHandler := NewAssetHandler()
|
|
|
|
assetHandler.DeveloperMode = b.DeveloperMode
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
return &PlatformHandler{
|
2018-12-21 21:17:18 +00:00
|
|
|
AssetHandler: assetHandler,
|
2018-10-02 16:00:29 +00:00
|
|
|
APIHandler: h,
|
2018-07-31 22:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:27:11 +00:00
|
|
|
// ServeHTTP delegates a request to the appropriate subhandler.
|
2018-10-02 16:00:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-02 13:17:19 +00:00
|
|
|
// Serve the chronograf assets for any basepath that does not start with addressable parts
|
2018-07-18 17:14:04 +00:00
|
|
|
// of the platform API.
|
2018-07-31 22:50:02 +00:00
|
|
|
if !strings.HasPrefix(r.URL.Path, "/v1") &&
|
2018-09-26 08:49:19 +00:00
|
|
|
!strings.HasPrefix(r.URL.Path, "/api/v2") &&
|
2018-09-28 15:47:47 +00:00
|
|
|
!strings.HasPrefix(r.URL.Path, "/chronograf/") {
|
2018-07-18 17:14:04 +00:00
|
|
|
h.AssetHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
h.APIHandler.ServeHTTP(w, r)
|
2018-05-15 18:27:11 +00:00
|
|
|
}
|
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
|
|
|
|
}
|