navidrome/server/app/serve_index.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.4 KiB
Go
Raw Normal View History

package app
import (
"encoding/json"
"html/template"
"io/fs"
"io/ioutil"
"net/http"
2020-04-03 23:50:42 +02:00
"strings"
"github.com/microcosm-cc/bluemonday"
2020-04-03 23:50:42 +02:00
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
)
2020-05-02 00:29:50 +02:00
// Injects the config in the `index.html` template
func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
policy := bluemonday.UGCPolicy()
return func(w http.ResponseWriter, r *http.Request) {
c, err := ds.User(r.Context()).CountAll()
firstTime := c == 0 && err == nil
2020-07-24 19:59:41 +02:00
t, err := getIndexTemplate(r, fs)
2020-05-02 00:29:50 +02:00
if err != nil {
2020-07-24 19:59:41 +02:00
http.NotFound(w, r)
return
2020-05-02 00:29:50 +02:00
}
appConfig := map[string]interface{}{
"version": consts.Version(),
"firstTime": firstTime,
"baseURL": policy.Sanitize(strings.TrimSuffix(conf.Server.BaseURL, "/")),
"loginBackgroundURL": policy.Sanitize(conf.Server.UILoginBackgroundURL),
"welcomeMessage": policy.Sanitize(conf.Server.UIWelcomeMessage),
"enableTranscodingConfig": conf.Server.EnableTranscodingConfig,
"gaTrackingId": conf.Server.GATrackingID,
"enableDownloads": conf.Server.EnableDownloads,
"devActivityPanel": conf.Server.DevActivityPanel,
"devFastAccessCoverArt": conf.Server.DevFastAccessCoverArt,
}
2020-05-02 05:07:23 +02:00
j, err := json.Marshal(appConfig)
if err != nil {
log.Error(r, "Error converting config to JSON", "config", appConfig, err)
} else {
log.Trace(r, "Injecting config in index.html", "config", string(j))
}
log.Debug("UI configuration", "appConfig", appConfig)
version := consts.Version()
if version != "dev" {
version = "v" + version
}
data := map[string]interface{}{
"AppConfig": string(j),
"Version": version,
}
err = t.Execute(w, data)
if err != nil {
log.Error(r, "Could not execute `index.html` template", err)
}
}
}
func getIndexTemplate(r *http.Request, fs fs.FS) (*template.Template, error) {
t := template.New("initial state")
indexHtml, err := fs.Open("index.html")
if err != nil {
log.Error(r, "Could not find `index.html` template", err)
2020-07-24 19:59:41 +02:00
return nil, err
}
indexStr, err := ioutil.ReadAll(indexHtml)
if err != nil {
log.Error(r, "Could not read from `index.html`", err)
2020-07-24 19:59:41 +02:00
return nil, err
}
t, err = t.Parse(string(indexStr))
if err != nil {
log.Error(r, "Error parsing `index.html`", err)
2020-07-24 19:59:41 +02:00
return nil, err
}
2020-07-24 19:59:41 +02:00
return t, nil
}