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