1
0

Compare commits

...

11 Commits

13 changed files with 148 additions and 59 deletions

2
.gitignore vendored
View File

@ -3,4 +3,4 @@ bin/*
.vscode
*.log
*_templ.go
*.db3*
/test

View File

@ -6,7 +6,7 @@ SYSDDIR := ${SYSDDIR_:/%=%}
DESTDIR ?=
PREFIX ?= /usr/local
VERSION ?= 24.25.0
VERSION ?= 24.38.0
GOFLAGS := -buildmode=pie -modcacherw -mod=readonly -trimpath
LDFLAGS := -ldflags "-linkmode=external -extldflags \"${LDFLAGS}\" -s -w -X main.version=${VERSION}" -tags osusergo,netgo
@ -24,9 +24,8 @@ endif
run: | ${TARGET}
bin/dwelling-radio -listen 127.0.0.1:18322 \
-playlist /mnt/data/appdata/radio/playlists/all-rand \
-fallback-song /mnt/data/appdata/radio/fallback.ogg \
-db test.db3
-work-dir test \
-playlist test
install:
install -Dm 0755 bin/${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}

View File

@ -1,6 +1,6 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=dwelling-radio
pkgver=24.25.0
pkgver=24.38.0
pkgrel=1
pkgdesc="Arav's dwelling / Radio"
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')

View File

@ -1,30 +1,30 @@
package main
import (
"context"
ihttp "dwelling-radio/internal/http"
"dwelling-radio/internal/radio"
sqlite_stats "dwelling-radio/internal/statistics/db/sqlite"
"dwelling-radio/pkg/utils"
"dwelling-radio/web"
"dwelling-radio/web/locales"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"git.arav.su/Arav/httpr"
"github.com/invopop/ctxi18n"
)
var (
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")
playlistPath = flag.String("playlist", "", "path to a playlist")
fallbackSong = flag.String("fallback-song", "", "path to a fallback song")
statisticsDbPath = flag.String("db", "/mnt/data/appdata/radio/statistics.db3", "path to a statistics database")
songListLen = flag.Int64("list-length", 10, "number of songs to show in last N songs table")
listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)")
workDirPath = flag.String("work-dir", "/mnt/data/appdata/radio", "path to a working directory")
playlistName = flag.String("playlist", "all-rand", "a playlist name")
songListLen = flag.Int64("list-length", 10, "number of songs to show in last N songs table")
showVersion = flag.Bool("v", false, "show version")
)
@ -40,7 +40,7 @@ func main() {
return
}
stats, err := sqlite_stats.New(*statisticsDbPath)
stats, err := sqlite_stats.New(path.Join(*workDirPath, "statistics.db3"))
if err != nil {
log.Fatalln("Statistics:", err)
}
@ -49,11 +49,15 @@ func main() {
currentSong := radio.Song{}
lstnrs := radio.NewListenerCounter()
plylst, err := radio.NewPlaylist(*playlistPath, true)
plylst, err := radio.NewPlaylist(path.Join(*workDirPath, "playlists", *playlistName), true)
if err != nil {
log.Fatalln(err)
}
if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil {
log.Fatalln(err)
}
r := httpr.New()
r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {
@ -64,18 +68,18 @@ func main() {
lstnrs.RLock()
defer lstnrs.RUnlock()
web.Index(&currentSong, lst, *songListLen, lstnrs, r).Render(context.Background(), w)
web.Index(version, &currentSong, lst, *songListLen, lstnrs, r).Render(r.Context(), w)
})
r.Handler(http.MethodGet, "/filelist", func(w http.ResponseWriter, r *http.Request) {
if *filelistPath == "" {
http.Error(w, "no filelist", http.StatusNotFound)
data, err := os.ReadFile(path.Join(*workDirPath, "filelist.html"))
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), 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)
})
@ -87,7 +91,7 @@ func main() {
r.ServeStatic("/assets/*filepath", web.Assets())
djh := ihttp.NewDJHandlers(lstnrs, plylst, stats, &currentSong, *songListLen, *fallbackSong)
djh := ihttp.NewDJHandlers(lstnrs, plylst, stats, &currentSong, *songListLen, path.Join(*workDirPath, "fallback.ogg"))
s := r.Sub("/api")
@ -95,7 +99,7 @@ func main() {
s.Handler(http.MethodGet, "/playlist", djh.PlaylistNext)
s.Handler(http.MethodGet, "/status", djh.Status)
srv := ihttp.NewHttpServer(r)
srv := ihttp.NewHttpServer(I18nMiddleware(r))
if err := srv.Start(*listenAddress); err != nil {
log.Fatalln(err)
@ -131,3 +135,25 @@ func main() {
}
}
}
func I18nMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lang := "en"
if lq := r.URL.Query().Get("lang"); lq != "" {
lc := http.Cookie{Name: "lang", Value: lq, HttpOnly: false, MaxAge: 0}
http.SetCookie(w, &lc)
lang = lq
} else if l, err := r.Cookie("lang"); err == nil {
lang = l.Value
} else if al := r.Header.Get("Accept-Language"); al != "" {
lang = r.Header.Get("Accept-Language")
}
ctx, err := ctxi18n.WithLocale(r.Context(), lang)
if err != nil {
log.Println("i18nmw:", err)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}

12
go.mod
View File

@ -8,6 +8,14 @@ require github.com/pkg/errors v0.9.1
require git.arav.su/Arav/httpr v0.3.2
require github.com/a-h/templ v0.2.680
require github.com/a-h/templ v0.2.778
require github.com/mattn/go-sqlite3 v1.14.22
require (
github.com/invopop/ctxi18n v0.8.1
github.com/mattn/go-sqlite3 v1.14.23
)
require (
github.com/invopop/yaml v0.3.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

22
go.sum
View File

@ -1,10 +1,24 @@
git.arav.su/Arav/httpr v0.3.2 h1:a+ifu+9+FnQe6p/Kd4kgTDKAFN6zBOJjBTMjbAuHxVk=
git.arav.su/Arav/httpr v0.3.2/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
github.com/a-h/templ v0.2.680 h1:TflYFucxp5rmOxAXB9Xy3+QHTk8s8xG9+nCT/cLzjeE=
github.com/a-h/templ v0.2.680/go.mod h1:NQGQOycaPKBxRB14DmAaeIpcGC1AOBPJEMO4ozS7m90=
github.com/a-h/templ v0.2.778 h1:VzhOuvWECrwOec4790lcLlZpP4Iptt5Q4K9aFxQmtaM=
github.com/a-h/templ v0.2.778/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/invopop/ctxi18n v0.8.1 h1:nfy5Mk6UfvLbGRBwpTi4T1g95+rmRo8bMllUmpCvVwI=
github.com/invopop/ctxi18n v0.8.1/go.mod h1:1Osw+JGYA+anHt0Z4reF36r5FtGHYjGQ+m1X7keIhPc=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -8,10 +8,8 @@ Type=simple
Restart=on-failure
DynamicUser=yes
ExecStart=/usr/bin/dwelling-radio -listen /var/run/dwelling-radio/sock \
-filelist /mnt/data/appdata/radio/filelist.html \
-playlist /mnt/data/appdata/radio/playlists/all-rand \
-fallback-song /mnt/data/appdata/radio/fallback.ogg \
-db /mnt/data/appdata/radio/statistics.db3 \
-work-dir /mnt/data/appdata/radio \
-playlist all-rand \
-lst-len 10
ReadOnlyPaths=/

View File

@ -110,8 +110,6 @@ section { margin-top: 1rem; }
#banner { text-align: center; }
#banner video { max-width: 90%; }
#player {
flex-direction: row;
align-items: center; }
@ -160,14 +158,10 @@ footer {
padding: 1rem 0; }
@media screen and (max-width: 640px) {
header { display: block; }
header {
align-items: center;
flex-direction: column; }
header svg {
margin: 0 auto;
width: 100%; }
nav {
width: 100%;
text-align: center; }
header svg { width: 100%; }
#player { flex-direction: column; } }

View File

Before

Width:  |  Height:  |  Size: 779 B

After

Width:  |  Height:  |  Size: 779 B

View File

@ -3,12 +3,14 @@ package web
import "net/http"
import "strconv"
import "github.com/invopop/ctxi18n/i18n"
import "dwelling-radio/internal/radio"
import "dwelling-radio/pkg/utils"
templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.ListenerCounter, r *http.Request) {
templ Index(prgVer string, curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.ListenerCounter, r *http.Request) {
<!DOCTYPE html>
<html lang="en">
<html lang={ i18n.GetLocale(ctx).Code().String() }>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
@ -16,11 +18,11 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
<meta name="theme-color" content="#cd2682" />
<meta name="color-scheme" content="light dark" />
<title>Arav's dwelling / Radio</title>
<title>Arav's dwelling / { i18n.T(ctx, "title") }</title>
<meta name="author" content={ "Alexander \"Arav\" Andreev" } />
<meta name="description" content="Internet-radio broadcasting from under my desk." />
<meta name="keywords" content="self-host radio home-radio various music" />
<meta name="description" content={ i18n.T(ctx, "description") } />
<meta name="keywords" content={ i18n.T(ctx, "keywords") } />
<link rel="canonical" href={ utils.Site(r.Host) } />
@ -36,8 +38,8 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
<text y="25" textLength="360" lengthAdjust="spacingAndGlyphs">Welcome to my sacred place, wanderer</text>
</svg>
<nav>
<a href={ templ.SafeURL(utils.MainSite(r.Host)) }>Back to home</a>
<h1>Radio</h1>
<a href={ templ.SafeURL(utils.MainSite(r.Host)) }>{ i18n.T(ctx, "back-home") }</a>
<h1>{ i18n.T(ctx, "title") }</h1>
</nav>
</header>
<section id="banner">
@ -47,9 +49,9 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
</section>
<section>
<div class="small player-links">
<a href="/filelist">filelist</a>
<a href="/playlist">playlist</a>
<a href="/live/stream.ogg">direct link</a>
<a href="/filelist">{ i18n.T(ctx, "link.filelist") }</a>
<a href="/playlist">{ i18n.T(ctx, "link.playlist") }</a>
<a href="/live/stream.ogg">{ i18n.T(ctx, "link.direct-link") }</a>
(<a href="http://radio.arav.su:8000/stream.ogg">http</a>
<a href="http://wsmkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion/live/stream.ogg">Tor</a>
<a href="http://radio.arav.i2p/live/stream.ogg">I2P</a>
@ -64,11 +66,11 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
</div>
<audio preload="none" controls playsinline>
<source src="/live/stream.ogg" type="audio/ogg" />
Your browser doesn't support an audio element, it's sad... But you always can take the <a href="/playlist">playlist</a>!
{ i18n.T(ctx, "no-audio-tag") } <a href="/playlist">{ i18n.T(ctx, "link.playlist") }</a>.
</audio>
<div>
<p>
<img src="/assets/img/listener.svg" alt="Listeners" title="Listeners" />
<img src="/assets/img/headphones.svg" alt="Listeners" title="Listeners" />
<span id="radio-listeners">{ strconv.FormatInt(lstnrs.Current(), 10) }</span>
<img src="/assets/img/duration.svg" alt="Duration" title="Duration" />
<span id="radio-duration-estimate"></span>
@ -81,7 +83,7 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
</span>
</p>
<p>
<img src="/assets/img/note.svg" />
<img src="/assets/img/note.svg" alt="Song" title="Song" />
<span id="radio-song">
if curSong != nil && curSong.Artist != "" {
{ curSong.Artist } - { curSong.Title }
@ -92,13 +94,13 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
</div>
</section>
<section>
<h2>Last { strconv.FormatInt(slLen, 10) } songs</h2>
<h2>{ i18n.T(ctx, "last-songs.h", i18n.M{"n": strconv.FormatInt(slLen, 10)}) }</h2>
<table id="last-songs">
<thead class="small">
<tr>
<td>Start</td>
<td><abbr title="Overall/Peak listeners">O/P</abbr></td>
<td>Song</td>
<td>{ i18n.T(ctx, "last-songs.tab-start") }</td>
<td><abbr title={ i18n.T(ctx, "last-songs.tab-stat-tip") }>{ i18n.T(ctx, "last-songs.tab-stat") }</abbr></td>
<td>{ i18n.T(ctx, "last-songs.tab-song") }</td>
</tr>
</thead>
<tbody>
@ -117,7 +119,10 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
</table>
</section>
<footer>
2017&mdash;2024 Alexander &quot;Arav&quot; Andreev &lt;<a href="mailto:me@arav.su">me@arav.su</a>&gt; <a href={ templ.SafeURL(utils.MainSite(r.Host) + "/privacy") }>Privacy statements</a>
<a href="?lang=ru">рус</a>
<a href="?lang=en">eng</a>
<br/>
v{ prgVer } 2017&mdash;2024 { i18n.T(ctx, "footer.author") } &lt;<a href="mailto:me@arav.su">me@arav.su</a>&gt; <a href={ templ.SafeURL(utils.MainSite(r.Host) + "/privacy") }>{ i18n.T(ctx, "footer.privacy") }</a>
</footer>
</body>
</html>

19
web/locales/en/en.yaml Normal file
View File

@ -0,0 +1,19 @@
en:
title: Radio
description: Internet-radio broadcasting from under my desk.
keywords: self-host radio home-radio various music
back-home: Back home
link:
filelist: filelist
playlist: playlist
direct-link: direct link
no-audio-tag: Seems like your browser doesn't support an audio element, but you can grab the
last-songs:
h: Last %{n} songs
tab-start: Start
tab-stat: O/P
tab-stat-tip: Overall/Peak listeners
tab-song: Song
footer:
author: Alexander ❝Arav❞ Andreev
privacy: Privacy statements

7
web/locales/locales.go Normal file
View File

@ -0,0 +1,7 @@
package locales
import "embed"
//go:embed en
//go:embed ru
var Content embed.FS

19
web/locales/ru/ru.yaml Normal file
View File

@ -0,0 +1,19 @@
ru:
title: Радио
description: Интернет-радио вещающееся из-под моего стола.
keywords: само-хост селф-хост радио разное музыка
back-home: Назад домой
link:
filelist: список файлов
playlist: плейлист
direct-link: прямая ссылка
no-audio-tag: Похоже на то, что твой браузер не поддерживает audio элемент, хреновенько, но можешь взять
last-songs:
h: Последние %{n} песен
tab-start: Начало
tab-stat: В
tab-stat-tip: Всего/Пиковое кол-во слушателей
tab-song: Песня
footer:
author: Александр «Arav» Андреев
privacy: О приватности