Skip to content

Commit

Permalink
#11 - Initial theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Jul 11, 2021
1 parent 9e21307 commit 2f26aa6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Entity struct {
// Config is the global config used through the whole app.
type Config struct {
UseSingleHTTPFrontend bool
ThemeName string
ListenAddressSingleHTTPFrontend string
ListenAddressWebUI string
ListenAddressRestActions string
Expand Down
30 changes: 17 additions & 13 deletions internal/httpservers/webuiServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type webUISettings struct {
Rest string
ThemeName string
}

func findWebuiDir() string {
Expand All @@ -33,26 +34,29 @@ func findWebuiDir() string {
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) {
log.WithFields(log.Fields{
"address": cfg.ListenAddressWebUI,
}).Info("Starting WebUI server")

mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
mux.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
restAddress := ""

if !cfg.UseSingleHTTPFrontend {
restAddress = cfg.ExternalRestAddress
}

jsonRet, _ := json.Marshal(webUISettings{
Rest: restAddress + "/api/",
})

w.Write([]byte(jsonRet))
})
mux.HandleFunc("/webUiSettings.json", generateWebUiSettings)

srv := &http.Server{
Addr: cfg.ListenAddressWebUI,
Expand Down
20 changes: 15 additions & 5 deletions webui/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ function fetchGetButtons() {
})
}

function onInitialLoad (res) {
window.restBaseUrl = res.Rest
function processWebuiSettingsJson (settings) {
window.restBaseUrl = settings.Rest

window.buttonInterval = setInterval(fetchGetButtons, 3000);
fetchGetButtons()
if (settings.ThemeName) {
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 => {
return res.json()
}).then(res => {
onInitialLoad(res)
processWebuiSettingsJson(res)

fetchGetButtons()

window.buttonInterval = setInterval(fetchGetButtons, 3000);
}).catch(err => {
showBigError('fetch-webui-settings', 'getting webui settings', err)
})

0 comments on commit 2f26aa6

Please sign in to comment.