package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
)
type musicCache struct {
expires time.Time
content PlayingInfo
}
var music_handler_Cache musicCache
func music_cache_handler(music_handler_Cache *musicCache, expiry_time time.Duration) PlayingInfo {
e := music_handler_Cache.content == PlayingInfo{}
if time.Now().After(music_handler_Cache.expires) || e{
log.Println("cache expired")
music_handler_Cache.content = getInfo()
music_handler_Cache.expires = time.Now().Add(expiry_time)
}
log.Println("returning cached result")
return music_handler_Cache.content
}
func nowPlayingHandler(w http.ResponseWriter, r *http.Request) {
playingInfo := music_cache_handler(&music_handler_Cache, time.Second * 60)
// v := reflect.ValueOf(playingInfo)
// k := v.Type()
// for i := range v.NumField() {
// log.Printf("%s: %s\n", k.Field(i).Name, v.Field(i))
// }
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
var response string
if playingInfo.playing {
response = fmt.Sprintf(
`
Now Playing: