Files
music-agregator/internal/api/router.go
T
Alexander b08a0b1646 feat: add refresh and delete artist endpoints (sections 1.2, 1.3)
- Add POST /api/artists/{id}/refresh to re-fetch metadata from gRPC service
- Add DELETE /api/artists/{id} with cascade delete via PostgreSQL
- Add e2e tests for both flows covering happy path, not found, idempotency
- Extend testutil with GetArtistUpdatedAt, CountAlbumsByArtist, DELETE helper
2026-04-29 13:08:53 +02:00

60 lines
1.4 KiB
Go

package api
import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)
func NewRouter(h *Handlers) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: true,
MaxAge: 300,
}))
r.Get("/health", h.Health)
r.Route("/api", func(r chi.Router) {
r.Route("/indexers", func(r chi.Router) {
r.Get("/", h.ListIndexers)
r.Post("/search", h.SearchIndexers)
})
r.Route("/torrents", func(r chi.Router) {
r.Get("/", h.ListTorrents)
r.Post("/", h.AddTorrent)
r.Get("/{hash}", h.GetTorrent)
r.Delete("/{hash}", h.RemoveTorrent)
r.Post("/{hash}/pause", h.PauseTorrent)
r.Post("/{hash}/resume", h.ResumeTorrent)
})
r.Route("/metadata", func(r chi.Router) {
r.Post("/artists/search", h.SearchArtists)
r.Get("/artists/{id}/albums", h.GetArtistAlbums)
})
r.Post("/sync", h.Sync)
r.Route("/artists", func(r chi.Router) {
r.Post("/{id}/refresh", h.RefreshArtist)
r.Delete("/{id}", h.DeleteArtist)
})
r.Route("/library", func(r chi.Router) {
r.Get("/artists", h.ListLibraryArtists)
r.Get("/albums", h.ListLibraryAlbums)
r.Get("/stats", h.LibraryStats)
})
})
return r
}