Add alternative names and cover art for soundtracks
Alternative names (#32): - PUT /api/v1/soundtracks/{uuid}/alternative-names replaces a soundtrack's alternative names (trimmed, deduplicated, without the soundtrack's own name). - alternative_names is in the soundtrack and statistics responses. - /find matches them too and shows which name matched. Covers (#12), stored in COVERS_PATH and off when it is not set: - GET, PUT (upload) and DELETE /api/v1/soundtracks/{uuid}/cover, and PUT .../cover/source to pick a SteamGridDB game or AniList anime by ID. Uploaded and picked covers are never replaced automatically. - With STEAMGRIDDB_API_KEY, a background job searches SteamGridDB, then AniList, by name, alternative names and the name without bracketed parts, at startup, after a sync and after names change. Search results are ranked by matching words, so "Civilization IV" gets "Sid Meier's Civilization IV". - cover_modified (Unix seconds) is in the soundtrack and statistics responses, so clients can cache covers. The new columns are added to migration 000006, which is not released yet. Closes #12, closes #32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f79f51ea4c
commit
bb8de5548d
39 files changed
+2418
-99
No files matched your search
@@ -35,6 +35,11 @@ type Server struct {
|
||||
releases *backend.ReleaseService
|
||||
stats *backend.StatisticsService
|
||||
search *backend.SearchCache
|
||||
// covers is nil when COVERS_PATH is not set.
|
||||
covers *backend.CoverService
|
||||
// stopCovers ends the background cover search; coversDone is closed when it has ended.
|
||||
stopCovers context.CancelFunc
|
||||
coversDone chan struct{}
|
||||
httpServer *http.Server
|
||||
// metrics serves /metrics on its own port; nil when metrics are off.
|
||||
metrics *metrics.Server
|
||||
@@ -57,9 +62,19 @@ func New(cfg config.Config, q repository.Querier, database Database) (*Server, e
|
||||
releases: backend.NewReleaseService(cfg.ReleasesURL),
|
||||
stats: backend.NewStatisticsService(q),
|
||||
}
|
||||
s.search = backend.NewSearchCache(s.music.SoundtrackNames)
|
||||
s.search = backend.NewSearchCache(s.music.SearchSoundtracks)
|
||||
s.sync = backend.NewSyncService(q, cfg.MusicPath, s.afterSync)
|
||||
|
||||
if cfg.CoversPath != "" {
|
||||
s.covers, err = backend.NewCoverService(q, cfg.CoversPath, backend.CoverOptions{
|
||||
SteamGridDBKey: cfg.SteamGridDBAPIKey,
|
||||
SteamGridDBURL: cfg.SteamGridDBURL,
|
||||
AniListURL: cfg.AniListURL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("covers path: %w", err)
|
||||
}
|
||||
}
|
||||
if cfg.MetricsPort > 0 {
|
||||
s.metrics = metrics.NewServer(cfg.MetricsPort)
|
||||
}
|
||||
@@ -73,6 +88,36 @@ func New(cfg config.Config, q repository.Querier, database Database) (*Server, e
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// startCoverSearch runs the background cover search until Shutdown. It does nothing when
|
||||
// covers are off or there is no SteamGridDB API key.
|
||||
func (s *Server) startCoverSearch() {
|
||||
if s.covers == nil || !s.covers.SearchesAutomatically() {
|
||||
logging.GetLogger().Info("Automatic cover search is off",
|
||||
zap.Bool("covers_path_set", s.covers != nil))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
s.stopCovers, s.coversDone = cancel, make(chan struct{})
|
||||
go func() {
|
||||
defer close(s.coversDone)
|
||||
s.covers.Run(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
// soundtracksChanged reloads the cached soundtrack data after soundtracks were edited, and
|
||||
// lets the cover search try the new names.
|
||||
func (s *Server) soundtracksChanged(ctx context.Context) {
|
||||
if err := s.music.ReloadSoundtracks(ctx); err != nil {
|
||||
logging.GetLogger().Error("Failed to reload soundtracks", zap.Error(err))
|
||||
}
|
||||
if err := s.search.Reload(ctx); err != nil {
|
||||
logging.GetLogger().Error("Failed to reload search cache", zap.Error(err))
|
||||
}
|
||||
if s.covers != nil {
|
||||
s.covers.Trigger()
|
||||
}
|
||||
}
|
||||
|
||||
// afterSync refreshes cached data once a sync has finished.
|
||||
func (s *Server) afterSync(ctx context.Context) {
|
||||
if err := s.music.EndAllQueues(ctx); err != nil {
|
||||
@@ -84,6 +129,9 @@ func (s *Server) afterSync(ctx context.Context) {
|
||||
if err := s.search.Reload(ctx); err != nil {
|
||||
logging.GetLogger().Error("Failed to reload search cache after sync", zap.Error(err))
|
||||
}
|
||||
if s.covers != nil {
|
||||
s.covers.Trigger()
|
||||
}
|
||||
}
|
||||
|
||||
// NewServerInstance loads the configuration, connects to and migrates the database and creates the Server.
|
||||
@@ -133,6 +181,7 @@ func (s *Server) ListenAndServe() error {
|
||||
logging.GetLogger().Error("Failed to start the metrics server", zap.Error(err))
|
||||
}
|
||||
}
|
||||
s.startCoverSearch()
|
||||
logging.GetLogger().Info("Server starting", zap.String("address", s.httpServer.Addr))
|
||||
if err := s.httpServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
@@ -146,6 +195,10 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||
if s.metrics != nil {
|
||||
err = errors.Join(err, s.metrics.Shutdown(ctx))
|
||||
}
|
||||
if s.stopCovers != nil {
|
||||
s.stopCovers()
|
||||
<-s.coversDone
|
||||
}
|
||||
s.sync.Wait()
|
||||
s.characters.Close()
|
||||
s.db.Close()
|
||||
|
||||
Reference in new issue
Block a user