Restructure backend into services and fix security, crash and sync issues
Security - Serve character images through os.Root so names like ../ can't escape the characters folder - Sync start and /sync/reset are POST only (legacy and /api/v1) - /api/v1/token/cleanup requires a token, password compared in constant time, tokens no longer logged - Echo's rate limiter replaces the custom one; TRUSTED_PROXIES enables X-Forwarded-For for reverse proxies Crashes and races - Sync state lives in SyncService with atomics and a mutex instead of unsynchronised globals (fixes concurrent map writes during full sync) - Only one sync can run; /api/v1 sync and music routes are locked during sync like the legacy ones - No log.Fatal outside startup: errors become 4xx/5xx responses - Search uses an in-order character match instead of unescaped regexes - Empty queue / no songs return 404 instead of panicking or looping Behaviour - Sync result is computed once and stays the same between polls - Swagger and /openapi serve the embedded spec - Shutdown stops HTTP before closing the database - Expired tokens are deleted with a context that outlives the request - Files are served with Range support and correct content types; audio has no write deadline Performance - Migration 000006 adds soundtrack.fingerprint (names, sizes, mtimes) so unchanged folders are skipped without reading any files - Songs are reconciled per soundtrack with one query plus needed writes; renames keep play counts - Moved MUSIC_PATH updates soundtrack and song paths - .id cleanup only runs for changed soundtracks or a full sync - Classic random uses one SQL query; Gitea release lookups are cached with a timeout Structure - internal/config reads all environment variables once - sqlc emits a Querier interface; backend globals become services injected into handlers - One route registration shared by legacy and /api/v1 - Removed dead code (dbHelper.go, unused middleware, duplicate Gitea functions, embedded Swagger UI copy) and lib/pq - templ and swag pinned as go tools
This commit is contained in:
1 parent
75b6f0c1cf
commit
090e794460
78 files changed
+2919
-6303
No files matched your search
+68
-188
@@ -1,12 +1,11 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"music-server/internal/logging"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"music-server/internal/db/repository"
|
||||
)
|
||||
|
||||
// SoundtrackWithSongs represents a soundtrack with its songs for statistics
|
||||
@@ -39,239 +38,120 @@ type StatisticsSummary struct {
|
||||
MinSoundtrackPlays int64 `json:"min_soundtrack_plays"`
|
||||
}
|
||||
|
||||
// StatisticsHandler manages statistics operations
|
||||
type StatisticsHandler struct {
|
||||
// Uses the global backend repo initialized via InitBackend
|
||||
// StatisticsService reads play statistics.
|
||||
type StatisticsService struct {
|
||||
q repository.Querier
|
||||
}
|
||||
|
||||
// NewStatisticsHandler creates a new StatisticsHandler
|
||||
func NewStatisticsHandler() *StatisticsHandler {
|
||||
return &StatisticsHandler{}
|
||||
// NewStatisticsService creates a StatisticsService.
|
||||
func NewStatisticsService(q repository.Querier) *StatisticsService {
|
||||
return &StatisticsService{q: q}
|
||||
}
|
||||
|
||||
// GetMostPlayedSoundtracksWithSongs returns the top N most played soundtracks with their songs
|
||||
func (h *StatisticsHandler) GetMostPlayedSoundtracksWithSongs(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
// soundtrackRow covers the generated rows that share the soundtrack-with-songs shape.
|
||||
type soundtrackRow interface {
|
||||
repository.GetMostPlayedSoundtracksWithSongsRow |
|
||||
repository.GetLeastPlayedSoundtracksWithSongsRow |
|
||||
repository.GetLastPlayedSoundtracksRow |
|
||||
repository.GetOldestPlayedSoundtracksRow
|
||||
}
|
||||
|
||||
// Get raw results
|
||||
rows, err := queries.GetMostPlayedSoundtracksWithSongs(ctx, limit)
|
||||
func toSoundtracks[R soundtrackRow](rows []R, err error) ([]SoundtrackWithSongs, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert to SoundtrackWithSongs
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
// Parse JSON songs array
|
||||
if err := json.Unmarshal(row.Songs, &songs); err != nil {
|
||||
// Fallback: if JSON parsing fails, create empty song entries
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
for _, r := range rows {
|
||||
row := repository.GetMostPlayedSoundtracksWithSongsRow(r)
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
Songs: parseSongs(row.Songs),
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLeastPlayedSoundtracksWithSongs returns the top N least played soundtracks with their songs
|
||||
func (h *StatisticsHandler) GetLeastPlayedSoundtracksWithSongs(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLeastPlayedSoundtracksWithSongs(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
if err := json.Unmarshal(row.Songs, &songs); err != nil {
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
type songRow interface {
|
||||
repository.GetMostPlayedSongsWithSoundtrackRow | repository.GetLeastPlayedSongsWithSoundtrackRow
|
||||
}
|
||||
|
||||
// GetMostPlayedSongsWithSoundtrack returns the top N most played songs with their soundtrack info
|
||||
func (h *StatisticsHandler) GetMostPlayedSongsWithSoundtrack(limit int32) ([]SongInfoForStats, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetMostPlayedSongsWithSoundtrack(ctx, limit)
|
||||
func toSongs[R songRow](rows []R, err error) ([]SongInfoForStats, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []SongInfoForStats
|
||||
for _, row := range rows {
|
||||
result = append(result, SongInfoForStats{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
})
|
||||
for _, r := range rows {
|
||||
result = append(result, SongInfoForStats(repository.GetMostPlayedSongsWithSoundtrackRow(r)))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLeastPlayedSongsWithSoundtrack returns the top N least played songs with their soundtrack info
|
||||
func (h *StatisticsHandler) GetLeastPlayedSongsWithSoundtrack(limit int32) ([]SongInfoForStats, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLeastPlayedSongsWithSoundtrack(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func parseSongs(data []byte) []SongInfoForStats {
|
||||
songs := []SongInfoForStats{}
|
||||
if data != nil {
|
||||
if err := json.Unmarshal(data, &songs); err != nil {
|
||||
return []SongInfoForStats{}
|
||||
}
|
||||
}
|
||||
|
||||
var result []SongInfoForStats
|
||||
for _, row := range rows {
|
||||
result = append(result, SongInfoForStats{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
return songs
|
||||
}
|
||||
|
||||
// GetNeverPlayedSoundtracks returns soundtracks that have never been played
|
||||
func (h *StatisticsHandler) GetNeverPlayedSoundtracks() ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
// MostPlayedSoundtracks returns the top N most played soundtracks with their songs
|
||||
func (s *StatisticsService) MostPlayedSoundtracks(ctx context.Context, limit int32) ([]SoundtrackWithSongs, error) {
|
||||
return toSoundtracks(s.q.GetMostPlayedSoundtracksWithSongs(ctx, limit))
|
||||
}
|
||||
|
||||
rows, err := queries.GetNeverPlayedSoundtracks(ctx)
|
||||
// LeastPlayedSoundtracks returns the top N least played soundtracks with their songs
|
||||
func (s *StatisticsService) LeastPlayedSoundtracks(ctx context.Context, limit int32) ([]SoundtrackWithSongs, error) {
|
||||
return toSoundtracks(s.q.GetLeastPlayedSoundtracksWithSongs(ctx, limit))
|
||||
}
|
||||
|
||||
// LastPlayedSoundtracks returns the most recently played soundtracks
|
||||
func (s *StatisticsService) LastPlayedSoundtracks(ctx context.Context, limit int32) ([]SoundtrackWithSongs, error) {
|
||||
return toSoundtracks(s.q.GetLastPlayedSoundtracks(ctx, limit))
|
||||
}
|
||||
|
||||
// OldestPlayedSoundtracks returns the least recently played soundtracks
|
||||
func (s *StatisticsService) OldestPlayedSoundtracks(ctx context.Context, limit int32) ([]SoundtrackWithSongs, error) {
|
||||
return toSoundtracks(s.q.GetOldestPlayedSoundtracks(ctx, limit))
|
||||
}
|
||||
|
||||
// NeverPlayedSoundtracks returns soundtracks that have never been played
|
||||
func (s *StatisticsService) NeverPlayedSoundtracks(ctx context.Context) ([]SoundtrackWithSongs, error) {
|
||||
rows, err := s.q.GetNeverPlayedSoundtracks(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
if err := json.Unmarshal(row.Songs, &songs); err != nil {
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: nil,
|
||||
Songs: songs,
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
Songs: parseSongs(row.Songs),
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLastPlayedSoundtracks returns the most recently played soundtracks
|
||||
func (h *StatisticsHandler) GetLastPlayedSoundtracks(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLastPlayedSoundtracks(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
if err := json.Unmarshal(row.Songs, &songs); err != nil {
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
// MostPlayedSongs returns the top N most played songs with their soundtrack info
|
||||
func (s *StatisticsService) MostPlayedSongs(ctx context.Context, limit int32) ([]SongInfoForStats, error) {
|
||||
return toSongs(s.q.GetMostPlayedSongsWithSoundtrack(ctx, limit))
|
||||
}
|
||||
|
||||
// GetOldestPlayedSoundtracks returns the least recently played soundtracks
|
||||
func (h *StatisticsHandler) GetOldestPlayedSoundtracks(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetOldestPlayedSoundtracks(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
if err := json.Unmarshal(row.Songs, &songs); err != nil {
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
// LeastPlayedSongs returns the top N least played songs with their soundtrack info
|
||||
func (s *StatisticsService) LeastPlayedSongs(ctx context.Context, limit int32) ([]SongInfoForStats, error) {
|
||||
return toSongs(s.q.GetLeastPlayedSongsWithSoundtrack(ctx, limit))
|
||||
}
|
||||
|
||||
// GetStatisticsSummary returns overall statistics
|
||||
func (h *StatisticsHandler) GetStatisticsSummary() (*StatisticsSummary, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
row, err := queries.GetStatisticsSummary(ctx)
|
||||
// Summary returns overall statistics
|
||||
func (s *StatisticsService) Summary(ctx context.Context) (StatisticsSummary, error) {
|
||||
row, err := s.q.GetStatisticsSummary(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &StatisticsSummary{
|
||||
TotalSoundtracks: int64(row.TotalSoundtracks),
|
||||
PlayedSoundtracks: int64(row.PlayedSoundtracks),
|
||||
NeverPlayedSoundtracks: int64(row.NeverPlayedSoundtracks),
|
||||
TotalSoundtrackPlays: int64(row.TotalSoundtrackPlays),
|
||||
AvgSoundtrackPlays: float64(row.AvgSoundtrackPlays),
|
||||
MaxSoundtrackPlays: int64(row.MaxSoundtrackPlays),
|
||||
MinSoundtrackPlays: int64(row.MinSoundtrackPlays),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Log helper for statistics operations
|
||||
func logStatisticsError(err error, operation string) {
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Statistics error",
|
||||
zap.String("operation", operation),
|
||||
zap.String("error", err.Error()))
|
||||
return StatisticsSummary{}, err
|
||||
}
|
||||
return StatisticsSummary(row), nil
|
||||
}
|
||||
Reference in new issue
Block a user