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"
|
|
|
|
|
2022-10-20 13:24:05 +00:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2020-08-18 21:04:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/http/legacy"
|
2020-04-30 15:29:43 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/feature"
|
2020-04-03 17:39:20 +00:00
|
|
|
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
|
2021-07-06 17:54:01 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/static"
|
2018-05-15 18:27:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PlatformHandler is a collection of all the service handlers.
|
|
|
|
type PlatformHandler struct {
|
2021-07-06 17:54:01 +00:00
|
|
|
AssetHandler http.Handler
|
2020-08-14 19:37:30 +00:00
|
|
|
DocsHandler http.HandlerFunc
|
|
|
|
APIHandler http.Handler
|
|
|
|
LegacyHandler http.Handler
|
2018-05-15 18:27:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
// NewPlatformHandler returns a platform handler that serves the API and associated assets.
|
2020-04-29 20:04:26 +00:00
|
|
|
func NewPlatformHandler(b *APIBackend, opts ...APIHandlerOptFn) *PlatformHandler {
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewAuthenticationHandler(b.Logger, b.HTTPErrorHandler)
|
2020-04-30 15:29:43 +00:00
|
|
|
h.Handler = feature.NewHandler(b.Logger, b.Flagger, feature.Flags(), NewAPIHandler(b, opts...))
|
2018-10-02 20:10:41 +00:00
|
|
|
h.AuthorizationService = b.AuthorizationService
|
|
|
|
h.SessionService = b.SessionService
|
2019-05-15 17:16:47 +00:00
|
|
|
h.SessionRenewDisabled = b.SessionRenewDisabled
|
2020-04-29 20:04:26 +00:00
|
|
|
h.UserService = b.UserService
|
2018-10-02 20:10:41 +00:00
|
|
|
|
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")
|
2019-01-22 17:16:27 +00:00
|
|
|
h.RegisterNoAuthRoute("GET", "/api/v2/swagger.json")
|
2018-07-31 22:50:02 +00:00
|
|
|
|
2021-07-06 17:54:01 +00:00
|
|
|
assetHandler := static.NewAssetHandler(b.AssetsPath)
|
2021-07-22 01:52:57 +00:00
|
|
|
if b.UIDisabled {
|
|
|
|
b.Logger.Debug("http server running with UI disabled")
|
|
|
|
assetHandler = http.NotFoundHandler()
|
|
|
|
}
|
2018-12-21 21:17:18 +00:00
|
|
|
|
2020-01-08 19:19:18 +00:00
|
|
|
wrappedHandler := kithttp.SetCORS(h)
|
|
|
|
wrappedHandler = kithttp.SkipOptions(wrappedHandler)
|
2019-12-09 23:54:16 +00:00
|
|
|
|
2020-08-18 21:04:35 +00:00
|
|
|
legacyBackend := newLegacyBackend(b)
|
2021-06-24 16:33:54 +00:00
|
|
|
lh := newLegacyHandler(legacyBackend, *legacy.NewHandlerConfig())
|
2022-10-20 13:24:05 +00:00
|
|
|
// legacy reponses can optionally be gzip encoded
|
|
|
|
gh := gziphandler.GzipHandler(lh)
|
2020-08-14 19:37:30 +00:00
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
return &PlatformHandler{
|
2020-08-18 21:04:35 +00:00
|
|
|
AssetHandler: assetHandler,
|
|
|
|
DocsHandler: Redoc("/api/v2/swagger.json"),
|
|
|
|
APIHandler: wrappedHandler,
|
2022-10-20 13:24:05 +00:00
|
|
|
LegacyHandler: legacy.NewInflux1xAuthenticationHandler(gh, b.AuthorizerV1, b.HTTPErrorHandler),
|
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) {
|
2020-08-14 19:37:30 +00:00
|
|
|
// TODO(affo): change this to be mounted prefixes: https://github.com/influxdata/idpe/issues/6689.
|
2020-08-18 21:04:35 +00:00
|
|
|
if r.URL.Path == "/write" ||
|
2020-08-14 19:37:30 +00:00
|
|
|
r.URL.Path == "/query" ||
|
|
|
|
r.URL.Path == "/ping" {
|
|
|
|
h.LegacyHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-08-18 21:04:35 +00:00
|
|
|
|
2019-01-22 17:16:27 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/docs") {
|
|
|
|
h.DocsHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-08 14:14:03 +00:00
|
|
|
// Serve the static UI assets for any basepath that does not start with
|
|
|
|
// addressable parts 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") &&
|
2020-10-24 00:24:17 +00:00
|
|
|
!strings.HasPrefix(r.URL.Path, "/private/") {
|
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
|
|
|
}
|