fleet/server/service/frontend.go
ferhat elmas 9e0912e2f3 General simplification in go part (#1658)
* don't check if error is nil, return it
 * don't compare bool to bool, use it
 * don't supply capacity to make for slice when len
   is equal to cap
2017-12-04 09:43:43 -05:00

53 lines
1.2 KiB
Go

package service
import (
"html/template"
"io/ioutil"
"net/http"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/go-kit/kit/log"
)
func newBinaryFileSystem(root string) *assetfs.AssetFS {
return &assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
Prefix: root,
}
}
func ServeFrontend(logger log.Logger) http.Handler {
herr := func(w http.ResponseWriter, err string) {
logger.Log("err", err)
http.Error(w, err, http.StatusInternalServerError)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs := newBinaryFileSystem("/frontend")
file, err := fs.Open("templates/react.tmpl")
if err != nil {
herr(w, "load react template: "+err.Error())
return
}
data, err := ioutil.ReadAll(file)
if err != nil {
herr(w, "read bindata file: "+err.Error())
return
}
t, err := template.New("react").Parse(string(data))
if err != nil {
herr(w, "create react template: "+err.Error())
return
}
if err := t.Execute(w, nil); err != nil {
herr(w, "execute react template: "+err.Error())
return
}
})
}
func ServeStaticAssets(path string) http.Handler {
return http.StripPrefix(path, http.FileServer(newBinaryFileSystem("/assets")))
}