2016-09-16 22:50:49 +00:00
|
|
|
package dist
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
//go:generate go-bindata -o dist_gen.go -ignore 'map|go' -pkg dist ../ui/build/...
|
|
|
|
|
2016-09-19 16:36:48 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DebugAssets serves assets via a specified directory
|
|
|
|
type DebugAssets struct {
|
2016-09-20 02:08:32 +00:00
|
|
|
Dir string // Dir is a directory location of asset files
|
|
|
|
Default string // Default is the file to serve if file is not found.
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 20:41:51 +00:00
|
|
|
// Handler is an http.FileServer for the Dir
|
|
|
|
func (d *DebugAssets) Handler() http.Handler {
|
2016-09-20 02:08:32 +00:00
|
|
|
return http.FileServer(NewDir(d.Dir, d.Default))
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BindataAssets serves assets from go-bindata
|
|
|
|
type BindataAssets struct {
|
2016-09-20 02:08:32 +00:00
|
|
|
Prefix string // Prefix is prepended to the http file request
|
|
|
|
Default string // Default is the file to serve if the file is not found
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 20:41:51 +00:00
|
|
|
// Handler serves go-bindata using a go-bindata-assetfs façade
|
|
|
|
func (b *BindataAssets) Handler() http.Handler {
|
2016-09-20 02:08:32 +00:00
|
|
|
// def wraps the assets to return the default file if the file doesn't exist
|
|
|
|
def := func(name string) ([]byte, error) {
|
2016-10-10 20:14:33 +00:00
|
|
|
// If the named asset exists, then return it directly.
|
2016-09-20 02:08:32 +00:00
|
|
|
octets, err := Asset(name)
|
|
|
|
if err != nil {
|
2016-10-10 20:14:33 +00:00
|
|
|
// If this is at / then we just error out so we can return a Directory
|
|
|
|
// This directory will then be redirected by go to the /index.html
|
|
|
|
if name == b.Prefix {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If this is anything other than slash, we just return the default
|
|
|
|
// asset. This default asset will handle the routing.
|
|
|
|
return Asset(b.Default)
|
2016-09-20 02:08:32 +00:00
|
|
|
}
|
|
|
|
return octets, nil
|
|
|
|
}
|
2016-09-19 16:36:48 +00:00
|
|
|
var dir http.FileSystem = &assetfs.AssetFS{
|
2016-09-20 02:08:32 +00:00
|
|
|
Asset: def,
|
2016-09-19 16:36:48 +00:00
|
|
|
AssetDir: AssetDir,
|
|
|
|
AssetInfo: AssetInfo,
|
2016-09-19 20:41:51 +00:00
|
|
|
Prefix: b.Prefix,
|
|
|
|
}
|
2016-09-19 16:36:48 +00:00
|
|
|
return http.FileServer(dir)
|
|
|
|
}
|