2021-06-13 18:46:36 +02:00
|
|
|
package server
|
2020-02-08 19:43:14 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
2021-07-21 02:00:58 +02:00
|
|
|
"io"
|
2021-03-12 17:06:51 +01:00
|
|
|
"io/fs"
|
2020-02-08 19:43:14 +01:00
|
|
|
"net/http"
|
2022-11-29 20:40:44 +01:00
|
|
|
"path"
|
2020-04-03 23:50:42 +02:00
|
|
|
"strings"
|
2020-02-08 19:43:14 +01:00
|
|
|
|
2020-04-03 23:50:42 +02:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2020-03-22 06:04:10 +01:00
|
|
|
"github.com/navidrome/navidrome/consts"
|
2020-02-08 19:43:14 +01:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2021-10-27 01:33:21 +02:00
|
|
|
"github.com/navidrome/navidrome/utils"
|
2020-02-08 19:43:14 +01:00
|
|
|
)
|
|
|
|
|
2020-05-02 00:29:50 +02:00
|
|
|
// Injects the config in the `index.html` template
|
2021-03-12 17:06:51 +01:00
|
|
|
func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
|
2020-02-08 19:43:14 +01:00
|
|
|
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
|
|
|
}
|
2020-02-08 19:43:14 +01:00
|
|
|
appConfig := map[string]interface{}{
|
2022-09-15 03:09:39 +02:00
|
|
|
"version": consts.Version,
|
2020-04-28 03:23:15 +02:00
|
|
|
"firstTime": firstTime,
|
2021-09-27 17:52:23 +02:00
|
|
|
"variousArtistsId": consts.VariousArtistsID,
|
2021-10-27 01:33:21 +02:00
|
|
|
"baseURL": utils.SanitizeText(strings.TrimSuffix(conf.Server.BaseURL, "/")),
|
|
|
|
"loginBackgroundURL": utils.SanitizeText(conf.Server.UILoginBackgroundURL),
|
|
|
|
"welcomeMessage": utils.SanitizeText(conf.Server.UIWelcomeMessage),
|
2020-04-28 03:23:15 +02:00
|
|
|
"enableTranscodingConfig": conf.Server.EnableTranscodingConfig,
|
2020-11-10 22:14:43 +01:00
|
|
|
"enableDownloads": conf.Server.EnableDownloads,
|
2021-03-29 02:35:49 +02:00
|
|
|
"enableFavourites": conf.Server.EnableFavourites,
|
2021-04-07 22:02:52 +02:00
|
|
|
"enableStarRating": conf.Server.EnableStarRating,
|
2021-04-18 19:51:00 +02:00
|
|
|
"defaultTheme": conf.Server.DefaultTheme,
|
2022-10-18 17:44:16 +02:00
|
|
|
"defaultLanguage": conf.Server.DefaultLanguage,
|
2022-11-11 22:31:28 +01:00
|
|
|
"defaultUIVolume": conf.Server.DefaultUIVolume,
|
2021-06-28 23:10:58 +02:00
|
|
|
"enableCoverAnimation": conf.Server.EnableCoverAnimation,
|
2021-04-18 19:51:00 +02:00
|
|
|
"gaTrackingId": conf.Server.GATrackingID,
|
|
|
|
"losslessFormats": strings.ToUpper(strings.Join(consts.LosslessFormats, ",")),
|
2020-11-25 21:29:46 +01:00
|
|
|
"devActivityPanel": conf.Server.DevActivityPanel,
|
2021-05-02 23:10:56 +02:00
|
|
|
"enableUserEditing": conf.Server.EnableUserEditing,
|
2021-05-30 21:36:10 +02:00
|
|
|
"devEnableShare": conf.Server.DevEnableShare,
|
2021-09-11 19:11:15 +02:00
|
|
|
"devSidebarPlaylists": conf.Server.DevSidebarPlaylists,
|
2021-07-02 15:50:07 +02:00
|
|
|
"lastFMEnabled": conf.Server.LastFM.Enabled,
|
2021-06-21 23:09:34 +02:00
|
|
|
"lastFMApiKey": conf.Server.LastFM.ApiKey,
|
2021-09-26 21:32:40 +02:00
|
|
|
"devShowArtistPage": conf.Server.DevShowArtistPage,
|
2021-11-18 03:11:53 +01:00
|
|
|
"listenBrainzEnabled": conf.Server.ListenBrainz.Enabled,
|
2020-02-08 19:43:14 +01:00
|
|
|
}
|
2022-11-29 20:40:44 +01:00
|
|
|
if strings.HasPrefix(conf.Server.UILoginBackgroundURL, "/") {
|
|
|
|
appConfig["loginBackgroundURL"] = path.Join(conf.Server.BaseURL, conf.Server.UILoginBackgroundURL)
|
|
|
|
}
|
2021-06-12 05:17:21 +02:00
|
|
|
auth := handleLoginFromHeaders(ds, r)
|
|
|
|
if auth != nil {
|
2021-06-13 18:46:36 +02:00
|
|
|
appConfig["auth"] = auth
|
2021-06-12 05:17:21 +02:00
|
|
|
}
|
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))
|
|
|
|
}
|
2020-04-08 17:00:30 +02:00
|
|
|
|
2020-07-03 17:51:15 +02:00
|
|
|
log.Debug("UI configuration", "appConfig", appConfig)
|
2022-09-15 03:09:39 +02:00
|
|
|
version := consts.Version
|
2020-07-26 16:52:20 +02:00
|
|
|
if version != "dev" {
|
|
|
|
version = "v" + version
|
|
|
|
}
|
2020-02-08 19:43:14 +01:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"AppConfig": string(j),
|
2020-07-26 16:52:20 +02:00
|
|
|
"Version": version,
|
2020-02-08 19:43:14 +01:00
|
|
|
}
|
2022-11-26 19:13:05 +01:00
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2020-02-08 19:43:14 +01:00
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(r, "Could not execute `index.html` template", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 17:00:30 +02:00
|
|
|
|
2021-03-12 17:06:51 +01:00
|
|
|
func getIndexTemplate(r *http.Request, fs fs.FS) (*template.Template, error) {
|
2020-04-08 17:00:30 +02:00
|
|
|
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
|
2020-04-08 17:00:30 +02:00
|
|
|
}
|
2021-07-21 02:00:58 +02:00
|
|
|
indexStr, err := io.ReadAll(indexHtml)
|
2020-04-08 17:00:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(r, "Could not read from `index.html`", err)
|
2020-07-24 19:59:41 +02:00
|
|
|
return nil, err
|
2020-04-08 17:00:30 +02:00
|
|
|
}
|
|
|
|
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-04-08 17:00:30 +02:00
|
|
|
}
|
2020-07-24 19:59:41 +02:00
|
|
|
return t, nil
|
2020-04-08 17:00:30 +02:00
|
|
|
}
|