2023-10-01 00:00:39 +04:00
|
|
|
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 <me@arav.su>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httpr.New()
|
|
|
|
|
|
|
|
lstnrs := radio.NewListeners()
|
2023-10-01 01:33:55 +04:00
|
|
|
plylst, err := radio.NewPlaylist(*playlist, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2023-10-01 00:00:39 +04:00
|
|
|
|
2023-10-01 01:33:55 +04:00
|
|
|
djh := ihttp.NewDJHandlers(lstnrs, plylst)
|
2023-10-01 00:00:39 +04:00
|
|
|
|
|
|
|
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")
|
2023-10-01 01:33:55 +04:00
|
|
|
s.Handler(http.MethodGet, "/", djh.PlaylistNext)
|
2023-10-01 00:00:39 +04:00
|
|
|
|
|
|
|
s = r.Sub("/song")
|
2023-10-01 03:35:33 +04:00
|
|
|
s.Handler(http.MethodGet, "/", djh.Song)
|
2023-10-01 00:00:39 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|