From 9d295bf903d163d4755a25a63705b7de5c47e84f Mon Sep 17 00:00:00 2001 From: Harmen Date: Thu, 8 Feb 2018 22:16:59 +0100 Subject: [PATCH] support for MPD_HOST --- siren/main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/siren/main.go b/siren/main.go index 8d5fa49..e4049a7 100644 --- a/siren/main.go +++ b/siren/main.go @@ -4,16 +4,17 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" ) var ( version = "master" - mpdURL = flag.String("mpd", "localhost:6600", "mpd URL") - listen = flag.String("listen", ":6601", "http listen") - static = flag.String("docroot", "", "use dir as docroot. Uses build-in files if empty") - showVersion = flag.Bool("version", false, "show version") + mpdURL = flag.String("mpd", "localhost:6600", "MPD address. Uses MPD_HOST by default, port optional") + listen = flag.String("listen", ":6601", "http listen address") + static = flag.String("docroot", "", "for development: use directory as docroot, not the build-in files") + showVersion = flag.Bool("version", false, "show version and exit") ) func main() { @@ -24,7 +25,8 @@ func main() { os.Exit(0) } - c, err := NewMPD(*mpdURL) + u := url(*mpdURL) + c, err := NewMPD(u) if err != nil { log.Fatal(err) } @@ -35,7 +37,8 @@ func main() { } else { fs = FS(false) } - log.Printf("listening on %s...\n", *listen) + log.Printf("MPD used: %s\n", u) + log.Printf("listening on: %s\n", *listen) log.Fatal(http.ListenAndServe(*listen, mux(c, fs))) } @@ -45,3 +48,13 @@ func mux(c *MPD, root http.FileSystem) *http.ServeMux { r.Handle("/", http.FileServer(root)) return r } + +func url(u string) string { + if h, _ := os.LookupEnv("MPD_HOST"); h != "" { + u = h + } + if _, _, err := net.SplitHostPort(u); err != nil { + u = u + ":6600" + } + return u +}