diff --git a/http/assets.go b/http/assets.go new file mode 100644 index 0000000000..6cc57f11ab --- /dev/null +++ b/http/assets.go @@ -0,0 +1,53 @@ +package http + +import ( + "net/http" + + // TODO: use platform version of the code + "github.com/influxdata/chronograf" + "github.com/influxdata/chronograf/dist" +) + +const ( + // Dir is prefix of the assets in the bindata + Dir = "../chronograf/ui/build" + // Default is the default item to load if 404 + Default = "../chronograf/ui/build/index.html" + // DebugDir is the prefix of the assets in development mode + DebugDir = "chronograf/ui/build" + // DebugDefault is the default item to load if 404 + DebugDefault = "chronograf/ui/build/index.html" + // DefaultContentType is the content-type to return for the Default file + DefaultContentType = "text/html; charset=utf-8" +) + +// AssetHandler is an http handler for serving chronograf assets. +type AssetHandler struct { + Develop bool +} + +// NewAssetHandler is the constructor an asset handler. +func NewAssetHandler() *AssetHandler { + return &AssetHandler{ + Develop: true, + } +} + +// ServeHTTP implements the http handler interface for serving assets. +func (h *AssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var assets chronograf.Assets + if h.Develop { + assets = &dist.DebugAssets{ + Dir: DebugDir, + Default: DebugDefault, + } + } else { + assets = &dist.BindataAssets{ + Prefix: Dir, + Default: Default, + DefaultContentType: DefaultContentType, + } + } + + assets.Handler().ServeHTTP(w, r) +} diff --git a/http/platform_handler.go b/http/platform_handler.go index 9eb25436f9..c6ef7ed1e6 100644 --- a/http/platform_handler.go +++ b/http/platform_handler.go @@ -16,6 +16,7 @@ type PlatformHandler struct { OrgHandler *OrgHandler AuthorizationHandler *AuthorizationHandler DashboardHandler *DashboardHandler + AssetHandler *AssetHandler } func setCORSResponseHeaders(w nethttp.ResponseWriter, r *nethttp.Request) { @@ -34,6 +35,13 @@ func (h *PlatformHandler) ServeHTTP(w nethttp.ResponseWriter, r *nethttp.Request return } + // Server the chronograf assets for any basepath that does not start with addressable parts + // of the platform API. + if !strings.HasPrefix(r.URL.Path, "/v1/") { + h.AssetHandler.ServeHTTP(w, r) + return + } + ctx := r.Context() var err error if ctx, err = extractAuthorization(ctx, r); err != nil {