forked from werkshy/pickup
/
main.go
executable file
·67 lines (57 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//usr/bin/env go run "$0" "$@"; exit
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/werkshy/pickup/config"
"github.com/werkshy/pickup/handlers"
"github.com/werkshy/pickup/player"
flag "launchpad.net/gnuflag"
)
func main() {
t0 := time.Now()
conf := config.Config{}
conf.MusicDir = flag.String("music-dir", "/music", "Music dir")
conf.Port = flag.Int("port", 8080, "Pickup port")
conf.MpdAddress = flag.String("mpd-address", "localhost:6600", "MPD address")
conf.MpdPassword = flag.String("mpd-password", "", "MPD Password")
flag.Parse(true)
log.Printf("Mpd address: '%s' password: '%s'\n", *conf.MpdAddress,
*conf.MpdPassword)
plyr, err := player.NewMpdPlayer(&conf)
if err != nil {
log.Fatalln("Failed to initialize mpd player", err)
}
music, err := plyr.GetCollection()
if err != nil {
log.Fatalln("Failed to retrieve collection", err)
}
log.Printf("Player with %d categories initialized in %v\n", len(music.Categories), time.Since(t0))
serve(&conf, &plyr)
}
func serve(conf *config.Config, plyr player.Player) {
categoryHandler := handlers.CategoryHandler{Player: plyr}
albumHandler := handlers.AlbumHandler{Player: plyr}
artistHandler := handlers.ArtistHandler{Player: plyr}
playlistHandler := handlers.PlaylistHandler{Player: plyr}
controlHandler := handlers.ControlHandler{Player: plyr}
http.Handle("/categories/", categoryHandler)
http.Handle("/albums/", albumHandler)
http.Handle("/artists/", artistHandler)
http.Handle("/playlist/", playlistHandler)
http.Handle("/control/", controlHandler)
staticDir, _ := os.Getwd()
staticDir = staticDir + "/static"
log.Printf("Serving static files from %s\n", staticDir)
// strip '/static' from the url to get the name of the file within the
// static dir.
http.Handle("/static/", http.StripPrefix("/static/",
http.FileServer(http.Dir(staticDir))))
http.HandleFunc("/", handlers.Index)
var bind = fmt.Sprintf(":%d", *conf.Port)
log.Printf("Serving from %s on %s\n", *conf.MusicDir, bind)
http.ListenAndServe(bind, nil)
}