Add webui to binary package

This commit is contained in:
Kevin Kandlbinder 2022-09-05 18:41:42 +02:00
parent 52b426cb6a
commit e0965b92e2
6 changed files with 141 additions and 4 deletions

58
webui/webui.go Normal file
View file

@ -0,0 +1,58 @@
//go:build withUI
// +build withUI
package webui
//go:generate yarn
//go:generate yarn build
import (
"embed"
"io/fs"
"log"
"net/http"
"path"
"strings"
)
//go:embed build/*
var content embed.FS
func ServeUI() (http.Handler, error) {
fSys, err := fs.Sub(content, "build")
if err != nil {
return nil, err
}
staticServer := http.FileServer(http.FS(fSys))
serveIndex, err := ServeIndex()
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fSys.Open(strings.TrimPrefix(path.Clean(r.URL.Path), "/"))
if err != nil {
log.Println(err)
serveIndex.ServeHTTP(w, r)
return
}
log.Println("serving static")
staticServer.ServeHTTP(w, r)
}), nil
}
func ServeIndex() (http.HandlerFunc, error) {
indexFile, err := content.ReadFile("build/index.html")
if err != nil {
return nil, err
}
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write(indexFile)
}, nil
}