64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"dwelling-radio/internal/configuration"
|
|
"dwelling-radio/internal/handlers"
|
|
"dwelling-radio/pkg/logging"
|
|
"dwelling-radio/pkg/server"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
|
|
var logToStdout *bool = flag.Bool("log-stdout", false, "write logs to stdout")
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
config, err := configuration.LoadConfiguration(*configPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if *logToStdout {
|
|
config.Log.ToStdout = true
|
|
}
|
|
|
|
defer func() {
|
|
if nt, addr := config.SplitNetworkAddress(); nt == "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()
|
|
|
|
hand := handlers.NewRadioHandlers(config, logErr)
|
|
srv := server.NewHttpServer()
|
|
|
|
srv.ServeStatic("/assets/*filepath", hand.AssetsFS())
|
|
srv.GET("/", hand.Index)
|
|
srv.GET("/stats", hand.Stats)
|
|
srv.GET("/lastsong", hand.LastSong)
|
|
srv.GET("/playlist", hand.Playlist)
|
|
|
|
if err := srv.Start(config.SplitNetworkAddress()); err != nil {
|
|
logErr.Fatalln(err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|