1
0

Current state of main.go.

This commit is contained in:
Alexander Andreev 2024-05-10 00:11:51 +04:00
parent 670b6ea032
commit 665a8e8c75
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -1,12 +1,14 @@
package main package main
import ( import (
"context"
ihttp "dwelling-radio/internal/http" ihttp "dwelling-radio/internal/http"
"dwelling-radio/internal/radio" "dwelling-radio/internal/radio"
sqlite_stats "dwelling-radio/internal/statistics/db/sqlite"
"dwelling-radio/pkg/utils"
"dwelling-radio/web" "dwelling-radio/web"
"flag" "flag"
"fmt" "fmt"
"io/fs"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -17,12 +19,12 @@ import (
) )
var ( var (
listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)") listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)")
filelistPath = flag.String("filelist", "/mnt/data/appdata/radio/filelist.html", "path to a filelist.html file") filelistPath = flag.String("filelist", "/mnt/data/appdata/radio/filelist.html", "path to a filelist.html file")
playlist = flag.String("playlist", "", "path to a playlist") playlistPath = flag.String("playlist", "", "path to a playlist")
fallbackSong = flag.String("fallback-song", "", "path to a fallback song") fallbackSong = flag.String("fallback-song", "", "path to a fallback song")
mostListenedSongPath = flag.String("mls-file", "/mnt/data/appdata/radio/mostlistenedsong", "path to a file that stores info about the most listened song") statisticsDbPath = flag.String("db", "/mnt/data/appdata/radio/statistics.db3", "path to a statistics database")
songListLen = flag.Int("list-length", 10, "number of songs to show in last N songs table") songListLen = flag.Int64("list-length", 10, "number of songs to show in last N songs table")
showVersion = flag.Bool("v", false, "show version") showVersion = flag.Bool("v", false, "show version")
) )
@ -38,35 +40,52 @@ func main() {
return return
} }
var mostListenedSong radio.MostListenedSong stats, err := sqlite_stats.New(*statisticsDbPath)
if data, err := os.ReadFile(*mostListenedSongPath); err == nil { if err != nil {
if err := mostListenedSong.Load(data); err != nil { log.Fatalln("Statistics:", err)
log.Fatalln(err)
}
} }
defer stats.Close()
songList := radio.NewSongList(*songListLen) currentSong := radio.Song{}
lstnrs := radio.NewListenerCounter() lstnrs := radio.NewListenerCounter()
plylst, err := radio.NewPlaylist(*playlist, true)
plylst, err := radio.NewPlaylist(*playlistPath, true)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
hand := ihttp.NewHandlers(*filelistPath, songList, lstnrs, &mostListenedSong)
r := httpr.New() r := httpr.New()
r.Handler(http.MethodGet, "/", hand.Index) r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {
lst, err := stats.LastNSongs(*songListLen)
if err != nil {
log.Printf("Failed to fetch last N songs: %s\n", err)
}
r.Handler(http.MethodGet, "/playlist", ihttp.ServeAsset("playlist.m3u", "", "radio.arav.su.m3u")) web.Index(&currentSong, lst, *songListLen, lstnrs, r).Render(context.Background(), w)
r.Handler(http.MethodGet, "/filelist", hand.Filelist) })
r.Handler(http.MethodGet, "/robots.txt", ihttp.ServeAsset("robots.txt", "text/plain", "")) r.Handler(http.MethodGet, "/filelist", func(w http.ResponseWriter, r *http.Request) {
r.Handler(http.MethodGet, "/sitemap.xml", ihttp.ServeAsset("sitemap.xml", "application/xml", "")) if *filelistPath == "" {
r.Handler(http.MethodGet, "/favicon.svg", ihttp.ServeAsset("favicon.svg", "image/svg", "")) http.Error(w, "no filelist", http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Link", "<"+utils.Site(r.Host)+"/filelist>; rel=\"canonical\"")
data, _ := os.ReadFile(*filelistPath)
w.Write(data)
})
r.Handler(http.MethodGet, "/playlist", web.ServeAsset("playlist.m3u", "", "radio.arav.su.m3u"))
r.Handler(http.MethodGet, "/robots.txt", web.ServeAsset("robots.txt", "text/plain", ""))
r.Handler(http.MethodGet, "/sitemap.xml", web.ServeAsset("sitemap.xml", "application/xml", ""))
r.Handler(http.MethodGet, "/favicon.svg", web.ServeAsset("favicon.svg", "image/svg", ""))
r.ServeStatic("/assets/*filepath", web.Assets()) r.ServeStatic("/assets/*filepath", web.Assets())
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong, *fallbackSong) djh := ihttp.NewDJHandlers(lstnrs, plylst, stats, &currentSong, *songListLen, *fallbackSong)
s := r.Sub("/api") s := r.Sub("/api")
@ -81,14 +100,6 @@ func main() {
} }
defer func() { defer func() {
fileData := mostListenedSong.Store()
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 { if err := srv.Stop(); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -100,6 +111,11 @@ func main() {
for { for {
switch <-sysSignal { switch <-sysSignal {
case os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT, syscall.SIGSEGV: case os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT, syscall.SIGSEGV:
if currentSong.Artist != "" {
if err := stats.Add(&currentSong); err != nil {
log.Println("failed to save a current song during a shutdown:", err)
}
}
return return
case syscall.SIGHUP: case syscall.SIGHUP:
if err := plylst.Reload(); err != nil { if err := plylst.Reload(); err != nil {