feat(http): create asset handler for serving chronograf assets

Co-authored-by: Michael Desa <mjdesa@gmail.com>
Co-authored-by: Andrew Watkins <andrew.watkinz@gmail.com>
pull/10616/head
Michael Desa 2018-07-18 13:14:04 -04:00
parent dc7418f7fe
commit 7340b60840
2 changed files with 61 additions and 0 deletions

53
http/assets.go Normal file
View File

@ -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)
}

View File

@ -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 {