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
189 lines
7.3 KiB
Go
189 lines
7.3 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"music-server/internal/backend"
|
|
"music-server/internal/logging"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
defaultStatsLimit = 10
|
|
maxStatsLimit = 100
|
|
)
|
|
|
|
// StatisticsHandler handles statistics-related HTTP requests
|
|
type StatisticsHandler struct {
|
|
stats *backend.StatisticsService
|
|
}
|
|
|
|
// NewStatisticsHandler creates a new StatisticsHandler
|
|
func NewStatisticsHandler(stats *backend.StatisticsService) *StatisticsHandler {
|
|
return &StatisticsHandler{stats: stats}
|
|
}
|
|
|
|
// parseLimit reads the "limit" query parameter, defaulting to 10 and capping at 100.
|
|
func parseLimit(ctx *echo.Context) (int32, bool) {
|
|
limitStr := ctx.QueryParam("limit")
|
|
if limitStr == "" {
|
|
return defaultStatsLimit, true
|
|
}
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil || limit <= 0 {
|
|
return 0, false
|
|
}
|
|
return int32(min(limit, maxStatsLimit)), true
|
|
}
|
|
|
|
// withLimit runs a statistics query that takes a limit and writes the result as JSON.
|
|
func withLimit[T any](ctx *echo.Context, name string, query func(*echo.Context, int32) (T, error)) error {
|
|
limit, ok := parseLimit(ctx)
|
|
if !ok {
|
|
return ctx.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid limit parameter"})
|
|
}
|
|
result, err := query(ctx, limit)
|
|
return statsResult(ctx, name, result, err)
|
|
}
|
|
|
|
func statsResult[T any](ctx *echo.Context, name string, result T, err error) error {
|
|
if err != nil {
|
|
logging.GetLogger().Error("Failed to get statistics", zap.String("statistic", name), zap.Error(err))
|
|
return ctx.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to get statistics"})
|
|
}
|
|
return ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GetMostPlayedSoundtracks returns top N most played soundtracks with songs
|
|
//
|
|
// @Summary Get most played soundtracks
|
|
// @Description Returns the top N most played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/most-played [get]
|
|
func (h *StatisticsHandler) GetMostPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "most played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.MostPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLeastPlayedSoundtracks returns top N least played soundtracks with songs
|
|
//
|
|
// @Summary Get least played soundtracks
|
|
// @Description Returns the top N least played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/least-played [get]
|
|
func (h *StatisticsHandler) GetLeastPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "least played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.LeastPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetMostPlayedSongs returns top N most played songs
|
|
//
|
|
// @Summary Get most played songs
|
|
// @Description Returns the top N most played songs with their soundtrack info
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SongInfoForStats
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/songs/most-played [get]
|
|
func (h *StatisticsHandler) GetMostPlayedSongs(ctx *echo.Context) error {
|
|
return withLimit(ctx, "most played songs", func(c *echo.Context, limit int32) ([]backend.SongInfoForStats, error) {
|
|
return h.stats.MostPlayedSongs(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLeastPlayedSongs returns top N least played songs
|
|
//
|
|
// @Summary Get least played songs
|
|
// @Description Returns the top N least played songs with their soundtrack info
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SongInfoForStats
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/songs/least-played [get]
|
|
func (h *StatisticsHandler) GetLeastPlayedSongs(ctx *echo.Context) error {
|
|
return withLimit(ctx, "least played songs", func(c *echo.Context, limit int32) ([]backend.SongInfoForStats, error) {
|
|
return h.stats.LeastPlayedSongs(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLastPlayedSoundtracks returns the most recently played soundtracks
|
|
//
|
|
// @Summary Get last played soundtracks
|
|
// @Description Returns the most recently played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/last-played [get]
|
|
func (h *StatisticsHandler) GetLastPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "last played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.LastPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetOldestPlayedSoundtracks returns the least recently played soundtracks
|
|
//
|
|
// @Summary Get oldest played soundtracks
|
|
// @Description Returns the least recently played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/oldest-played [get]
|
|
func (h *StatisticsHandler) GetOldestPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "oldest played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.OldestPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetNeverPlayedSoundtracks returns soundtracks that have never been played
|
|
//
|
|
// @Summary Get never played soundtracks
|
|
// @Description Returns all soundtracks that have never been played
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/never-played [get]
|
|
func (h *StatisticsHandler) GetNeverPlayedSoundtracks(ctx *echo.Context) error {
|
|
result, err := h.stats.NeverPlayedSoundtracks(ctx.Request().Context())
|
|
return statsResult(ctx, "never played soundtracks", result, err)
|
|
}
|
|
|
|
// GetStatisticsSummary returns overall statistics
|
|
//
|
|
// @Summary Get statistics summary
|
|
// @Description Returns overall play statistics
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Success 200 {object} backend.StatisticsSummary
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/summary [get]
|
|
func (h *StatisticsHandler) GetStatisticsSummary(ctx *echo.Context) error {
|
|
result, err := h.stats.Summary(ctx.Request().Context())
|
|
return statsResult(ctx, "summary", result, err)
|
|
}
|