100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
ihttp "dwelling-radio/internal/http"
|
|
"dwelling-radio/internal/radio"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"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")
|
|
songListLen = flag.Int("list-length", 10, "number of songs to show in last N songs table")
|
|
mostListenedSongPath = flag.String("mls-file", "/mnt/data/appdata/radio/mostlistenedsong", "path to a file that stores info about the most listened song")
|
|
|
|
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) 2023 Alexander \"Arav\" Andreev <me@arav.su>")
|
|
return
|
|
}
|
|
|
|
r := httpr.New()
|
|
|
|
lstnrs := radio.NewListeners()
|
|
plylst, err := radio.NewPlaylist(*playlist, true)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if data, err := os.ReadFile(*mostListenedSongPath); err == nil {
|
|
if err := radio.LoadMostListenedSong(data); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|
|
|
|
djh := ihttp.NewDJHandlers(lstnrs, plylst, *songListLen)
|
|
|
|
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, "/", djh.PlaylistNext)
|
|
|
|
s = r.Sub("/song")
|
|
s.Handler(http.MethodGet, "/", djh.Song)
|
|
|
|
srv := ihttp.NewHttpServer(r)
|
|
|
|
if err := srv.Start(*listenAddress); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
defer func() {
|
|
fileData := radio.StoreMostListenedSong()
|
|
if fileData != nil {
|
|
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
|
|
if err != nil {
|
|
log.Println("failed to store a most listened song:", err)
|
|
}
|
|
}
|
|
|
|
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)
|
|
|
|
reloadSignal := make(chan os.Signal, 1)
|
|
signal.Notify(reloadSignal, syscall.SIGHUP)
|
|
|
|
go func() {
|
|
for {
|
|
<-reloadSignal
|
|
plylst.Reload()
|
|
}
|
|
}()
|
|
|
|
<-doneSignal
|
|
}
|