added option to use proxy for oauth
This commit is contained in:
@@ -19,6 +19,8 @@ type MorningAlarmConfig struct {
|
||||
PlaylistID string `json:"wakeupContext"`
|
||||
RedirectURL string `json:"redirectUrl"`
|
||||
ListenAddr string `json:"listenAddr"`
|
||||
OAuthProxyURL string `json:"oauthProxyUrl"`
|
||||
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
|
||||
}
|
||||
|
||||
type MorningAlarm struct {
|
||||
@@ -42,7 +44,11 @@ func (ma *MorningAlarm) Start() {
|
||||
|
||||
ma.ro = gin.Default()
|
||||
|
||||
ma.setupSpotify()
|
||||
if ma.config.OAuthProxyURL != "" {
|
||||
ma.setupSpotifyWithProxy()
|
||||
} else {
|
||||
ma.setupSpotify()
|
||||
}
|
||||
|
||||
ma.setupWebserver()
|
||||
|
||||
|
||||
@@ -16,6 +16,32 @@ import (
|
||||
|
||||
const redirectURI = "/api/spotifycb"
|
||||
|
||||
type headerTransport struct {
|
||||
baseTransport http.RoundTripper
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
for key, value := range t.headers {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
|
||||
return t.baseTransport.RoundTrip(req)
|
||||
}
|
||||
|
||||
func (ma *MorningAlarm) setupSpotifyWithProxy() {
|
||||
client := &http.Client{
|
||||
Transport: &headerTransport{
|
||||
baseTransport: http.DefaultTransport,
|
||||
headers: map[string]string{
|
||||
"X-API-KEY": ma.config.OAuthProxyAPIKey,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ma.sp = spotify.New(client, spotify.WithBaseURL(ma.config.OAuthProxyURL))
|
||||
}
|
||||
|
||||
func (ma *MorningAlarm) setupSpotify() error {
|
||||
auth := spotifyauth.New(
|
||||
spotifyauth.WithRedirectURL(ma.config.RedirectURL+redirectURI),
|
||||
|
||||
Reference in New Issue
Block a user