Pass version to UI through AppConfig, instead of login payload.master
This makes the version info updated with a browser refresh (no need to logout and login again)
This commit is contained in:
parent
db246900a6
commit
089a92157f
|
@ -63,7 +63,6 @@ func handleLogin(ds model.DataStore, username string, password string, w http.Re
|
||||||
"name": user.Name,
|
"name": user.Name,
|
||||||
"username": username,
|
"username": username,
|
||||||
"isAdmin": user.IsAdmin,
|
"isAdmin": user.IsAdmin,
|
||||||
"version": consts.Version(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,22 +19,16 @@ func ServeIndex(ds model.DataStore, fs http.FileSystem) http.HandlerFunc {
|
||||||
c, err := ds.User(r.Context()).CountAll()
|
c, err := ds.User(r.Context()).CountAll()
|
||||||
firstTime := c == 0 && err == nil
|
firstTime := c == 0 && err == nil
|
||||||
|
|
||||||
t := template.New("initial state")
|
t := getIndexTemplate(r, fs)
|
||||||
indexHtml, err := fs.Open("index.html")
|
|
||||||
if err != nil {
|
|
||||||
log.Error(r, "Could not find `index.html` template", err)
|
|
||||||
}
|
|
||||||
indexStr, err := ioutil.ReadAll(indexHtml)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(r, "Could not read from `index.html`", err)
|
|
||||||
}
|
|
||||||
t, _ = t.Parse(string(indexStr))
|
|
||||||
appConfig := map[string]interface{}{
|
appConfig := map[string]interface{}{
|
||||||
|
"version": consts.Version(),
|
||||||
"firstTime": firstTime,
|
"firstTime": firstTime,
|
||||||
"baseURL": strings.TrimSuffix(conf.Server.BaseURL, "/"),
|
"baseURL": strings.TrimSuffix(conf.Server.BaseURL, "/"),
|
||||||
"loginBackgroundURL": conf.Server.UILoginBackgroundURL,
|
"loginBackgroundURL": conf.Server.UILoginBackgroundURL,
|
||||||
}
|
}
|
||||||
j, _ := json.Marshal(appConfig)
|
j, _ := json.Marshal(appConfig)
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"AppConfig": string(j),
|
"AppConfig": string(j),
|
||||||
"Version": consts.Version(),
|
"Version": consts.Version(),
|
||||||
|
@ -45,3 +39,20 @@ func ServeIndex(ds model.DataStore, fs http.FileSystem) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getIndexTemplate(r *http.Request, fs http.FileSystem) *template.Template {
|
||||||
|
t := template.New("initial state")
|
||||||
|
indexHtml, err := fs.Open("index.html")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(r, "Could not find `index.html` template", err)
|
||||||
|
}
|
||||||
|
indexStr, err := ioutil.ReadAll(indexHtml)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(r, "Could not read from `index.html`", err)
|
||||||
|
}
|
||||||
|
t, err = t.Parse(string(indexStr))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(r, "Error parsing `index.html`", err)
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/conf"
|
"github.com/deluan/navidrome/conf"
|
||||||
|
"github.com/deluan/navidrome/consts"
|
||||||
"github.com/deluan/navidrome/model"
|
"github.com/deluan/navidrome/model"
|
||||||
"github.com/deluan/navidrome/persistence"
|
"github.com/deluan/navidrome/persistence"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
|
@ -79,6 +80,16 @@ var _ = Describe("ServeIndex", func() {
|
||||||
config := extractAppConfig(w.Body.String())
|
config := extractAppConfig(w.Body.String())
|
||||||
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "my_background_url"))
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "my_background_url"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("sets the version", func() {
|
||||||
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
ServeIndex(ds, fs)(w, r)
|
||||||
|
|
||||||
|
config := extractAppConfig(w.Body.String())
|
||||||
|
Expect(config).To(HaveKeyWithValue("version", consts.Version()))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)
|
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)
|
||||||
|
|
|
@ -26,7 +26,6 @@ const authProvider = {
|
||||||
jwtDecode(response.token)
|
jwtDecode(response.token)
|
||||||
localStorage.removeItem('initialAccountCreation')
|
localStorage.removeItem('initialAccountCreation')
|
||||||
localStorage.setItem('token', response.token)
|
localStorage.setItem('token', response.token)
|
||||||
localStorage.setItem('version', response.version)
|
|
||||||
localStorage.setItem('name', response.name)
|
localStorage.setItem('name', response.name)
|
||||||
localStorage.setItem('username', response.username)
|
localStorage.setItem('username', response.username)
|
||||||
localStorage.setItem('role', response.isAdmin ? 'admin' : 'regular')
|
localStorage.setItem('role', response.isAdmin ? 'admin' : 'regular')
|
||||||
|
@ -77,7 +76,6 @@ const removeItems = () => {
|
||||||
localStorage.removeItem('name')
|
localStorage.removeItem('name')
|
||||||
localStorage.removeItem('username')
|
localStorage.removeItem('username')
|
||||||
localStorage.removeItem('role')
|
localStorage.removeItem('role')
|
||||||
localStorage.removeItem('version')
|
|
||||||
localStorage.removeItem('subsonic-salt')
|
localStorage.removeItem('subsonic-salt')
|
||||||
localStorage.removeItem('subsonic-token')
|
localStorage.removeItem('subsonic-token')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
|
version: 'dev',
|
||||||
firstTime: false,
|
firstTime: false,
|
||||||
baseURL: '',
|
baseURL: '',
|
||||||
loginBackgroundURL: 'https://source.unsplash.com/random/1600x900?music'
|
loginBackgroundURL: 'https://source.unsplash.com/random/1600x900?music'
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
} from 'react-admin'
|
} from 'react-admin'
|
||||||
import { makeStyles } from '@material-ui/core'
|
import { makeStyles } from '@material-ui/core'
|
||||||
import InfoIcon from '@material-ui/icons/Info'
|
import InfoIcon from '@material-ui/icons/Info'
|
||||||
|
import config from '../config'
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
menuItem: {
|
menuItem: {
|
||||||
|
@ -22,7 +23,7 @@ const VersionMenu = forwardRef((props, ref) => {
|
||||||
ref={ref}
|
ref={ref}
|
||||||
to="#"
|
to="#"
|
||||||
primaryText={translate('menu.version', {
|
primaryText={translate('menu.version', {
|
||||||
version: localStorage.getItem('version')
|
version: config.version
|
||||||
})}
|
})}
|
||||||
leftIcon={<InfoIcon />}
|
leftIcon={<InfoIcon />}
|
||||||
className={classes.menuItem}
|
className={classes.menuItem}
|
||||||
|
|
Loading…
Reference in New Issue