#11 - Initial theme support

This commit is contained in:
jamesread 2021-07-11 21:01:00 +01:00
parent 9e21307dfd
commit 2f26aa65a6
3 changed files with 33 additions and 18 deletions

View File

@ -24,6 +24,7 @@ type Entity struct {
// Config is the global config used through the whole app. // Config is the global config used through the whole app.
type Config struct { type Config struct {
UseSingleHTTPFrontend bool UseSingleHTTPFrontend bool
ThemeName string
ListenAddressSingleHTTPFrontend string ListenAddressSingleHTTPFrontend string
ListenAddressWebUI string ListenAddressWebUI string
ListenAddressRestActions string ListenAddressRestActions string

View File

@ -12,6 +12,7 @@ import (
type webUISettings struct { type webUISettings struct {
Rest string Rest string
ThemeName string
} }
func findWebuiDir() string { func findWebuiDir() string {
@ -33,6 +34,21 @@ func findWebuiDir() string {
return "./webui" // Should not exist return "./webui" // Should not exist
} }
func generateWebUiSettings(w http.ResponseWriter, r *http.Request) {
restAddress := ""
if !cfg.UseSingleHTTPFrontend {
restAddress = cfg.ExternalRestAddress
}
jsonRet, _ := json.Marshal(webUISettings{
Rest: restAddress + "/api/",
ThemeName: cfg.ThemeName,
})
w.Write([]byte(jsonRet))
}
func startWebUIServer(cfg *config.Config) { func startWebUIServer(cfg *config.Config) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"address": cfg.ListenAddressWebUI, "address": cfg.ListenAddressWebUI,
@ -40,19 +56,7 @@ func startWebUIServer(cfg *config.Config) {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(findWebuiDir()))) mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
mux.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/webUiSettings.json", generateWebUiSettings)
restAddress := ""
if !cfg.UseSingleHTTPFrontend {
restAddress = cfg.ExternalRestAddress
}
jsonRet, _ := json.Marshal(webUISettings{
Rest: restAddress + "/api/",
})
w.Write([]byte(jsonRet))
})
srv := &http.Server{ srv := &http.Server{
Addr: cfg.ListenAddressWebUI, Addr: cfg.ListenAddressWebUI,

View File

@ -27,17 +27,27 @@ function fetchGetButtons() {
}) })
} }
function onInitialLoad (res) { function processWebuiSettingsJson (settings) {
window.restBaseUrl = res.Rest window.restBaseUrl = settings.Rest
window.buttonInterval = setInterval(fetchGetButtons, 3000); if (settings.ThemeName) {
fetchGetButtons() var themeCss = document.createElement('link')
themeCss.setAttribute('rel', 'stylesheet');
themeCss.setAttribute('type', 'text/css')
themeCss.setAttribute('href', '/themes/' + settings.ThemeName + '/theme.css');
document.head.appendChild(themeCss);
}
} }
window.fetch('webUiSettings.json').then(res => { window.fetch('webUiSettings.json').then(res => {
return res.json() return res.json()
}).then(res => { }).then(res => {
onInitialLoad(res) processWebuiSettingsJson(res)
fetchGetButtons()
window.buttonInterval = setInterval(fetchGetButtons, 3000);
}).catch(err => { }).catch(err => {
showBigError('fetch-webui-settings', 'getting webui settings', err) showBigError('fetch-webui-settings', 'getting webui settings', err)
}) })