Expose Last.fm's ApiKey to UI

This commit is contained in:
Deluan 2021-06-21 17:09:34 -04:00 committed by Deluan Quintão
parent 143cde37e5
commit 1f997357a9
8 changed files with 30 additions and 22 deletions

View File

@ -214,8 +214,8 @@ func init() {
viper.SetDefault("agents", "lastfm,spotify")
viper.SetDefault("lastfm.enabled", true)
viper.SetDefault("lastfm.language", "en")
viper.SetDefault("lastfm.apikey", "")
viper.SetDefault("lastfm.secret", "")
viper.SetDefault("lastfm.apikey", consts.LastFMAPIKey)
viper.SetDefault("lastfm.secret", consts.LastFMAPISecret)
viper.SetDefault("spotify.id", "")
viper.SetDefault("spotify.secret", "")

View File

@ -61,6 +61,12 @@ const (
DefaultCacheCleanUpInterval = 10 * time.Minute
)
// Shared secrets (only add here "secrets" that can be public)
const (
LastFMAPIKey = "9b94a5515ea66b2da3ec03c12300327e"
LastFMAPISecret = "74cb6557cec7171d921af5d7d887c587" // nolint:gosec
)
var (
DefaultTranscodings = []map[string]interface{}{
{

View File

@ -13,8 +13,6 @@ import (
const (
lastFMAgentName = "lastfm"
lastFMAPIKey = "9b94a5515ea66b2da3ec03c12300327e"
lastFMAPISecret = "74cb6557cec7171d921af5d7d887c587" // nolint:gosec
)
type lastfmAgent struct {
@ -29,12 +27,8 @@ func lastFMConstructor(ctx context.Context) agents.Interface {
l := &lastfmAgent{
ctx: ctx,
lang: conf.Server.LastFM.Language,
apiKey: lastFMAPIKey,
secret: lastFMAPISecret,
}
if conf.Server.LastFM.ApiKey != "" {
l.apiKey = conf.Server.LastFM.ApiKey
l.secret = conf.Server.LastFM.Secret
apiKey: conf.Server.LastFM.ApiKey,
secret: conf.Server.LastFM.Secret,
}
hc := utils.NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)
l.client = NewClient(l.apiKey, l.secret, l.lang, hc)

View File

@ -22,18 +22,13 @@ const (
var _ = Describe("lastfmAgent", func() {
Describe("lastFMConstructor", func() {
It("uses default api key and language if not configured", func() {
conf.Server.LastFM.ApiKey = ""
agent := lastFMConstructor(context.Background())
Expect(agent.(*lastfmAgent).apiKey).To(Equal(lastFMAPIKey))
Expect(agent.(*lastfmAgent).lang).To(Equal("en"))
})
It("uses configured api key and language", func() {
conf.Server.LastFM.ApiKey = "123"
conf.Server.LastFM.Secret = "secret"
conf.Server.LastFM.Language = "pt"
agent := lastFMConstructor(context.Background())
Expect(agent.(*lastfmAgent).apiKey).To(Equal("123"))
Expect(agent.(*lastfmAgent).secret).To(Equal("secret"))
Expect(agent.(*lastfmAgent).lang).To(Equal("pt"))
})
})

View File

@ -30,13 +30,13 @@ type Router struct {
}
func NewRouter(ds model.DataStore) *Router {
r := &Router{ds: ds, apiKey: lastFMAPIKey, secret: lastFMAPISecret}
r := &Router{
ds: ds,
apiKey: conf.Server.LastFM.ApiKey,
secret: conf.Server.LastFM.Secret,
}
r.sessionKeys = &sessionKeys{ds: ds}
r.Handler = r.routes()
if conf.Server.LastFM.ApiKey != "" {
r.apiKey = conf.Server.LastFM.ApiKey
r.secret = conf.Server.LastFM.Secret
}
r.client = NewClient(r.apiKey, r.secret, "en", http.DefaultClient)
return r
}

View File

@ -45,6 +45,7 @@ func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
"enableUserEditing": conf.Server.EnableUserEditing,
"devEnableShare": conf.Server.DevEnableShare,
"devEnableScrobble": conf.Server.DevEnableScrobble,
"lastFMApiKey": conf.Server.LastFM.ApiKey,
}
auth := handleLoginFromHeaders(ds, r)
if auth != nil {

View File

@ -209,6 +209,17 @@ var _ = Describe("serveIndex", func() {
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("devEnableScrobble", false))
})
It("sets the lastFMApiKey", func() {
conf.Server.LastFM.ApiKey = "APIKEY-123"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("lastFMApiKey", "APIKEY-123"))
})
})
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)

View File

@ -20,6 +20,7 @@ const defaultConfig = {
enableUserEditing: true,
devEnableShare: true,
devEnableScrobble: true,
lastFMApiKey: '9b94a5515ea66b2da3ec03c12300327e',
}
let config