2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-05 14:15:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2016-09-19 23:06:41 +00:00
|
|
|
"io/ioutil"
|
2016-09-05 14:15:58 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
assetfs "github.com/elazarl/go-bindata-assetfs"
|
2017-03-04 00:49:55 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2020-03-30 02:22:04 +00:00
|
|
|
"github.com/kolide/fleet/server/bindata"
|
2016-09-05 14:15:58 +00:00
|
|
|
)
|
|
|
|
|
2017-01-16 15:16:50 +00:00
|
|
|
func newBinaryFileSystem(root string) *assetfs.AssetFS {
|
|
|
|
return &assetfs.AssetFS{
|
2020-03-30 02:22:04 +00:00
|
|
|
Asset: bindata.Asset,
|
|
|
|
AssetDir: bindata.AssetDir,
|
|
|
|
AssetInfo: bindata.AssetInfo,
|
2017-01-16 15:16:50 +00:00
|
|
|
Prefix: root,
|
2016-09-05 14:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:40:45 +00:00
|
|
|
func ServeFrontend(urlPrefix string, logger log.Logger) http.Handler {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr := func(w http.ResponseWriter, err string) {
|
|
|
|
logger.Log("err", err)
|
|
|
|
http.Error(w, err, http.StatusInternalServerError)
|
|
|
|
}
|
2016-09-07 19:19:54 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-09-19 23:06:41 +00:00
|
|
|
fs := newBinaryFileSystem("/frontend")
|
|
|
|
file, err := fs.Open("templates/react.tmpl")
|
2016-09-07 19:19:54 +00:00
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "load react template: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "read bindata file: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t, err := template.New("react").Parse(string(data))
|
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "create react template: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-16 23:40:45 +00:00
|
|
|
if err := t.Execute(w, struct{ URLPrefix string }{urlPrefix}); err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "execute react template: "+err.Error())
|
2016-09-07 19:19:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServeStaticAssets(path string) http.Handler {
|
2016-09-12 16:11:18 +00:00
|
|
|
return http.StripPrefix(path, http.FileServer(newBinaryFileSystem("/assets")))
|
2016-09-05 14:15:58 +00:00
|
|
|
}
|