1
0
dwelling-radio/cmd/dwelling-radio/main.go

84 lines
1.9 KiB
Go

package main
import (
"dwelling-radio/internal/configuration"
"dwelling-radio/internal/http"
"dwelling-radio/internal/radio"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
var version string
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
var noLiquidsoap *bool = flag.Bool("no-liquidsoap", false, "don't run liquidsoap")
var showVersion *bool = flag.Bool("v", false, "show version")
func main() {
flag.Parse()
if *showVersion {
fmt.Println("dwelling-radio ver.", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev <me@arav.su>")
return
}
config, err := configuration.Load(*configPath)
if err != nil {
log.Fatalln(err)
}
if typ, addr := config.SplitNetworkAddress(); typ == "unix" {
defer os.Remove(addr)
}
playlistWatcher := radio.NewPlaylistLogWatcher()
if err := playlistWatcher.Watch(config.Icecast.Playlist, config.ListLastNSongs); err != nil {
log.Fatalln(err)
}
defer playlistWatcher.Close()
hand := http.NewHandlers(config)
srv := http.NewHttpServer()
srv.ServeStatic("/assets/*filepath", hand.AssetsFS())
srv.GET("/", hand.Index)
srv.GET("/status", hand.Status)
srv.GET("/lastsong", hand.LastSong)
srv.GET("/playlist", hand.Playlist)
srv.GET("/filelist", hand.Filelist)
srv.GET("/robots.txt", hand.RobotsTxt)
if !*noLiquidsoap {
liquid, err := radio.NewLiquidsoap(config.Liquidsoap.ExecPath, config.Liquidsoap.ScriptPath)
if err != nil {
log.Fatalln("liquidsoap:", err)
}
defer func() {
if err := liquid.Stop(); err != nil {
if !errors.Is(err, radio.ErrLiquidsoapNotRunning) {
log.Println(err)
}
}
}()
}
if err := srv.Start(config.SplitNetworkAddress()); err != nil {
log.Fatalln(err)
}
doneSignal := make(chan os.Signal, 1)
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-doneSignal
if err := srv.Stop(); err != nil {
log.Fatalln(err)
}
}