morningalarm/internal/morningalarm.go

57 lines
1.2 KiB
Go

package morningalarm
import (
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"github.com/zmb3/spotify/v2"
)
type alarm struct {
Name string `json:"name" binding:"required"`
Time string `json:"time" binding:"required"`
id cron.EntryID
}
type MorningAlarmConfig struct {
SpotifyClientID string `json:"spotifyClientId"`
SpotifyClientSecret string `json:"spotifyClientSecret"`
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
RedirectURL string `json:"redirectUrl"`
ListenAddr string `json:"listenAddr"`
OAuthProxyURL string `json:"oauthProxyUrl"`
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
}
type MorningAlarm struct {
config MorningAlarmConfig
sp *spotify.Client
cr *cron.Cron
ro *gin.Engine
alarms []alarm
}
func New(config MorningAlarmConfig) *MorningAlarm {
return &MorningAlarm{
config: config,
}
}
func (ma *MorningAlarm) Start() {
ma.cr = cron.New()
ma.loadAlarms()
ma.cr.Start()
ma.ro = gin.Default()
if ma.config.OAuthProxyURL != "" {
ma.setupSpotifyWithProxy()
} else {
ma.setupSpotify()
}
ma.setupWebserver()
ma.ro.Run(ma.config.ListenAddr)
}