-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathassets.go
58 lines (52 loc) · 1.54 KB
/
assets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package server
import (
"net/http"
"github.com/influxdata/influxdb/v2/chronograf"
"github.com/influxdata/influxdb/v2/chronograf/dist"
)
const (
// Dir is prefix of the assets in the bindata
Dir = "../ui/build"
// Default is the default item to load if 404
Default = "../ui/build/index.html"
// DebugDir is the prefix of the assets in development mode
DebugDir = "ui/build"
// DebugDefault is the default item to load if 404
DebugDefault = "ui/build/index.html"
// DefaultContentType is the content-type to return for the Default file
DefaultContentType = "text/html; charset=utf-8"
)
// AssetsOpts configures the asset middleware
type AssetsOpts struct {
// Develop when true serves assets from ui/build directory directly; false will use internal bindata.
Develop bool
// Logger will log the asset served
Logger chronograf.Logger
}
// Assets creates a middleware that will serve a single page app.
func Assets(opts AssetsOpts) http.Handler {
var assets chronograf.Assets
if opts.Develop {
assets = &dist.DebugAssets{
Dir: DebugDir,
Default: DebugDefault,
}
} else {
assets = &dist.BindataAssets{
Prefix: Dir,
Default: Default,
DefaultContentType: DefaultContentType,
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if opts.Logger != nil {
opts.Logger.
WithField("component", "server").
WithField("remote_addr", r.RemoteAddr).
WithField("method", r.Method).
WithField("url", r.URL).
Info("Serving assets")
}
assets.Handler().ServeHTTP(w, r)
})
}