From 8677af243c6d51d5fed84c9886e5f0d57af6d46b Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 1 Oct 2023 00:00:39 +0400 Subject: [PATCH] Added main for a radiodj service. --- cmd/dwellin-radiodj/main.go | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 cmd/dwellin-radiodj/main.go diff --git a/cmd/dwellin-radiodj/main.go b/cmd/dwellin-radiodj/main.go new file mode 100644 index 0000000..8757c9c --- /dev/null +++ b/cmd/dwellin-radiodj/main.go @@ -0,0 +1,71 @@ +package main + +import ( + ihttp "dwelling-radio/internal/http" + "dwelling-radio/internal/radio" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + + "git.arav.su/Arav/httpr" +) + +var ( + listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)") + playlist = flag.String("playlist", "", "path to a playlist") + icecastUrl = flag.String("ic-url", "", "URL to an Icecast's status-json.xsl") + icecastPlaylistPath = flag.String("ic-playlist", "/var/log/icecast/playlist.log", "path to an Icecast's playlist.log file") + songListLen = flag.Int("list-length", 10, "number of songs to show in last N songs table") + + showVersion = flag.Bool("v", false, "show version") +) + +var version string + +func main() { + flag.Parse() + log.SetFlags(log.Lshortfile) + + if *showVersion { + fmt.Println("dwelling-radio ver.", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev ") + return + } + + r := httpr.New() + + lstnrs := radio.NewListeners() + + djh := ihttp.NewDJHandlers(lstnrs) + + s := r.Sub("/listener") + s.Handler(http.MethodGet, "/", djh.ListenersGet) + s.Handler(http.MethodPost, "/", djh.ListenersInc) + s.Handler(http.MethodDelete, "/", djh.ListenersDec) + + s = r.Sub("/playlist") + s.Handler(http.MethodGet, "/", http.NotFound) + + s = r.Sub("/song") + s.Handler(http.MethodGet, "/", http.NotFound) + + srv := ihttp.NewHttpServer(r) + + if err := srv.Start(*listenAddress); err != nil { + log.Fatalln(err) + } + + defer func() { + if err := srv.Stop(); err != nil { + log.Fatalln(err) + } + }() + + doneSignal := make(chan os.Signal, 1) + signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV) + + <-doneSignal +}