func main() { configFile, err := os.Open("conf/patchy.conf") if err != nil { fmt.Println("Couldn't open conf file!") os.Exit(1) } jsonParser := json.NewDecoder(configFile) if err = jsonParser.Decode(&settings); err != nil { fmt.Println("Couldn't parse conf file!", err.Error()) os.Exit(1) } port := flag.String("port", settings.Port, "The port that patchy listens on.") musicDirFlag := flag.String("mdir", settings.MusicDir, "The full filepath to the mpd library location.") flag.Parse() musicDir = *musicDirFlag startUp() h := newHub() go h.run() q := newQueue() l := newLibrary() subset := l.selection() //Control song transitions -- During this time, update the websockets and notify clients utaChan := make(chan string) reChan := make(chan string) go handleSongs(utaChan, reChan, l, h, q) requests := make(chan *request) go handleRequests(requests, utaChan, q, l, h) //Searches for cover image web.Get("/art/(.+)", getCover) //Gets the song -- apparently firefox is a PoS and needs manual header setting web.Get("/queue/(.+)", getSong) //Search for songs similar to a given title web.Get("/search/(.*)", func(ctx *web.Context, req string) string { return getSearchRes(ctx, req, l) }) //Returns the JSON info for the currently playing song web.Get("/np", func(ctx *web.Context) string { return getNowPlaying(ctx, utaChan, reChan, q, len(h.connections)) }) //Handle the websocket web.Websocket("/ws", websocket.Handler(func(ws *websocket.Conn) { handleSocket(ws, h, utaChan, requests) })) //Returns a library sample for initial client display web.Get("/library", func(ctx *web.Context) string { return getLibrary(ctx, subset) }) //Returns the current queue web.Get("/curQueue", func(ctx *web.Context) string { return getQueue(ctx, q) }) //Returns the current queue web.Post("/upload", func(ctx *web.Context) string { return handleUpload(ctx, l) }) web.Run("0.0.0.0:" + *port) }
func main() { conn, err := mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } defer conn.Close() w, err := mpd.NewWatcher("tcp", "127.0.0.1:6600", "", "player") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } defer w.Close() h := newHub() go h.run() // Log errors. go func() { for err := range w.Error { log.Println("Error:", err) } }() //Control song transitions -- During this time, update the websockets go func() { var status mpd.Attrs for _ = range w.Event { status, err = conn.Status() if err != nil { //Connections seem to drop often, so reconnect when this happens fmt.Println("Couldn't get current status! Error: " + err.Error()) conn.Close() fmt.Println("Reconnecting...") conn, err = mpd.Dial("tcp", "127.0.0.1:6600") if err != nil { fmt.Println("Error: could not connect to MPD, exiting") os.Exit(1) } defer conn.Close() status, err = conn.Status() } pos, _ := strconv.ParseFloat(status["elapsed"], 64) fmt.Println(pos) if pos == 0.000 { //Stop us from getting into an infinite loop by waiting 25 ms time.Sleep(25 * time.Millisecond) //updateQueue <- &updateQueueMsg{Song: song["Title"], Artist: song["Artist"]} conn.Pause(true) //Wait 3 seconds then resume next song time.Sleep(3000 * time.Millisecond) conn.Pause(false) song, err := conn.CurrentSong() if err != nil { fmt.Println("Couldn't get current song! Error: " + err.Error()) } else { //Serialize and send info msg := map[string]string{"cmd": "NP", "Title": song["Title"], "Artist": song["Artist"], "Album": song["Album"], "Cover": "/art/" + GetAlbumDir(song["file"])} jsonMsg, _ := json.Marshal(msg) h.broadcast <- []byte(jsonMsg) } } } }() song, err := conn.CurrentSong() if err != nil { fmt.Println("No Song!") } else { fmt.Println(song["Title"]) } songs, err := conn.ListAllInfo("/") shuffle(songs) subset := songs[:20] //Searches for cover image web.Get("/art/(.+)", getCover) //Returns main page with custom selection of songs web.Get("/", func(ctx *web.Context) string { return getIndex(ctx, subset) }) //Returns a raw song web.Get("/song/(.+)", getSong) //Returns the JSON info for the currently playing song web.Get("/np", func(ctx *web.Context) string { song, _ := conn.CurrentSong() jsonMsg, _ := json.Marshal(song) return string(jsonMsg) }) //Handle the websocket web.Websocket("/ws", websocket.Handler(func(ws *websocket.Conn) { handleSocket(ws, h) })) web.Get("/library", func(ctx *web.Context) string { jsonMsg, _ := json.Marshal(subset) return string(jsonMsg) }) web.Run("0.0.0.0:8080") }