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

91 lines
1.9 KiB
Go

package main
import (
"dwelling-radio/internal/configuration"
"dwelling-radio/internal/handlers"
"dwelling-radio/internal/radio"
"dwelling-radio/pkg/logging"
"dwelling-radio/pkg/server"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
var version string
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
var logToStdout *bool = flag.Bool("log-stdout", false, "write logs to stdout")
var showVersion *bool = flag.Bool("v", false, "show version")
func main() {
flag.Parse()
if *showVersion {
fmt.Println("dwelling-radio Ver.", version, "\nCopyright (c) 2022 Alexander \"Arav\" Andreev <me@arav.top>")
return
}
config, err := configuration.LoadConfiguration(*configPath)
if err != nil {
log.Fatalln(err)
}
if *logToStdout {
config.Log.ToStdout = true
}
defer func() {
if typ, addr := config.SplitNetworkAddress(); typ == "unix" {
os.Remove(addr)
}
}()
logErr, err := logging.NewLogger(config.Log.Error, config.Log.ToStdout)
if err != nil {
log.Fatalln("error logger:", err)
}
defer logErr.Close()
if err := radio.IcecastWatchPlaylist(config.Icecast.Playlist, config.ListLastNSongs); err != nil {
logErr.Fatalln(err)
}
defer radio.IcecastWatchClose()
hand := handlers.NewRadioHandlers(config, logErr)
srv := server.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)
if err := srv.Start(config.SplitNetworkAddress()); err != nil {
logErr.Fatalln(err)
}
logReload := make(chan os.Signal, 1)
signal.Notify(logReload, syscall.SIGHUP)
go func() {
for {
select {
case <-logReload:
logErr.Reopen(config.Log.Error)
}
}
}()
doneSignal := make(chan os.Signal, 1)
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-doneSignal
if err := srv.Stop(); err != nil {
logErr.Fatalln(err)
}
}