UI Customize: add white-label flags (logo, brand, website, email, phone) (#32582)
This commit is contained in:
parent
5e69a6226d
commit
84cf093d3d
20 changed files with 303 additions and 52 deletions
|
|
@ -46,8 +46,19 @@ type HTTPd struct {
|
|||
*http.Server
|
||||
}
|
||||
|
||||
// Customization contains white-label settings for the UI
|
||||
type Customization struct {
|
||||
Css string
|
||||
LogoLight string
|
||||
LogoDark string
|
||||
Brand string
|
||||
Website string
|
||||
Email string
|
||||
Phone string
|
||||
}
|
||||
|
||||
// NewHTTPd creates HTTP server with configured routes for loadpoint
|
||||
func NewHTTPd(addr string, hub *SocketHub, customCssFile string) *HTTPd {
|
||||
func NewHTTPd(addr string, hub *SocketHub, custom Customization) *HTTPd {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
|
||||
log := util.NewLogger("httpd")
|
||||
|
|
@ -79,21 +90,44 @@ func NewHTTPd(addr string, hub *SocketHub, customCssFile string) *HTTPd {
|
|||
})
|
||||
})
|
||||
|
||||
if customCssFile != "" {
|
||||
log.WARN.Printf("❗ using custom CSS: %s", customCssFile)
|
||||
if _, err := os.Stat(customCssFile); os.IsNotExist(err) {
|
||||
log.FATAL.Fatalf("custom CSS file does not exist: %s", customCssFile)
|
||||
}
|
||||
static.HandleFunc("/custom.css", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveCustomFile := func(path, file string) {
|
||||
static.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
// disable caching
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
http.ServeFile(w, r, customCssFile)
|
||||
http.ServeFile(w, r, file)
|
||||
})
|
||||
}
|
||||
|
||||
static.HandleFunc("/", indexHandler(customCssFile != ""))
|
||||
if custom.Css != "" {
|
||||
log.WARN.Printf("❗ using custom CSS: %s", custom.Css)
|
||||
if _, err := os.Stat(custom.Css); os.IsNotExist(err) {
|
||||
log.FATAL.Fatalf("custom CSS file does not exist: %s", custom.Css)
|
||||
}
|
||||
serveCustomFile("/custom.css", custom.Css)
|
||||
}
|
||||
|
||||
if (custom.LogoLight == "") != (custom.LogoDark == "") {
|
||||
log.FATAL.Fatal("custom logo requires both light and dark variants")
|
||||
}
|
||||
|
||||
for path, file := range map[string]string{
|
||||
"/custom-logo-light": custom.LogoLight,
|
||||
"/custom-logo-dark": custom.LogoDark,
|
||||
} {
|
||||
if file == "" {
|
||||
continue
|
||||
}
|
||||
log.WARN.Printf("❗ using custom logo: %s", file)
|
||||
if _, err := os.Stat(file); os.IsNotExist(err) {
|
||||
log.FATAL.Fatalf("custom logo file does not exist: %s", file)
|
||||
}
|
||||
serveCustomFile(path, file)
|
||||
}
|
||||
|
||||
static.HandleFunc("/globals.js", globalsJsHandler(custom))
|
||||
static.HandleFunc("/", indexHandler())
|
||||
for _, dir := range []string{"assets", "meta"} {
|
||||
static.PathPrefix("/" + dir).Handler(http.FileServer(http.FS(assets.Web)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,42 @@ func getPreferredLanguage(header string) string {
|
|||
return base.String()
|
||||
}
|
||||
|
||||
func indexHandler(customCss bool) http.HandlerFunc {
|
||||
// globalsJsHandler serves version and ui customization as window.evcc globals
|
||||
func globalsJsHandler(custom Customization) http.HandlerFunc {
|
||||
globals := struct {
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
CustomCss bool `json:"customCss"`
|
||||
CustomLogo bool `json:"customLogo"`
|
||||
Brand string `json:"customBrand"`
|
||||
Website string `json:"customWebsite"`
|
||||
Email string `json:"customEmail"`
|
||||
Phone string `json:"customPhone"`
|
||||
}{
|
||||
Version: util.Version,
|
||||
Commit: util.Commit,
|
||||
CustomCss: custom.Css != "",
|
||||
CustomLogo: custom.LogoLight != "",
|
||||
Brand: custom.Brand,
|
||||
Website: custom.Website,
|
||||
Email: custom.Email,
|
||||
Phone: custom.Phone,
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/javascript; charset=UTF-8")
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
||||
|
||||
if _, err := w.Write([]byte("window.evcc = ")); err != nil {
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(globals); err != nil {
|
||||
log.ERROR.Println("httpd: failed to render globals:", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func indexHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
||||
|
|
@ -64,9 +99,7 @@ func indexHandler(customCss bool) http.HandlerFunc {
|
|||
|
||||
if err := t.Execute(w, map[string]any{
|
||||
"Version": util.Version,
|
||||
"Commit": util.Commit,
|
||||
"DefaultLang": defaultLang,
|
||||
"CustomCss": customCss,
|
||||
}); err != nil {
|
||||
log.ERROR.Println("httpd: failed to render main page:", err.Error())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
func TestWriteTimeout(t *testing.T) {
|
||||
timeout := 200 * time.Millisecond
|
||||
|
||||
srv := NewHTTPd("", nil, "")
|
||||
srv := NewHTTPd("", nil, Customization{})
|
||||
srv.WriteTimeout = timeout
|
||||
|
||||
srv.Router().HandleFunc("/slow", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue