Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/discovery"
|
|
)
|
|
|
|
// statusClientClosed is the status nginx logs for a request the client gave up on.
|
|
const statusClientClosed = 499
|
|
|
|
// discoveryHandlers serve what providers say about artists outside the library.
|
|
type discoveryHandlers struct{ svc discoveryService }
|
|
|
|
type similarItemJSON struct {
|
|
Artist artistRefJSON `json:"artist"`
|
|
InLibrary bool `json:"in_library"`
|
|
Score float64 `json:"score"`
|
|
Rank int `json:"rank"`
|
|
}
|
|
|
|
type similarJSON struct {
|
|
Provider string `json:"provider"`
|
|
ProviderStatus string `json:"provider_status"`
|
|
Stale bool `json:"stale"`
|
|
RetrievedAt *string `json:"retrieved_at"`
|
|
Attribution string `json:"attribution"`
|
|
Items []similarItemJSON `json:"items"`
|
|
}
|
|
|
|
func toSimilarJSON(r discovery.Result) similarJSON {
|
|
out := similarJSON{
|
|
Provider: r.Provider, ProviderStatus: r.ProviderStatus, Stale: r.Stale,
|
|
Attribution: discovery.Attribution, Items: make([]similarItemJSON, len(r.Items)),
|
|
}
|
|
if !r.RetrievedAt.IsZero() {
|
|
out.RetrievedAt = rfc3339(r.RetrievedAt)
|
|
}
|
|
for i, it := range r.Items {
|
|
out.Items[i] = similarItemJSON{
|
|
Artist: artistRefJSON{ID: it.Artist.ID, Name: it.Artist.Name},
|
|
InLibrary: it.InLibrary, Score: it.Score, Rank: it.Rank,
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (h *discoveryHandlers) similar(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return
|
|
}
|
|
res, err := h.svc.Similar(c.Request.Context(), id, clampInt(limit))
|
|
switch {
|
|
case errors.Is(err, discovery.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
|
|
case errors.Is(err, discovery.ErrInvalidQuery):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
case errors.Is(err, context.Canceled):
|
|
// The client left while the provider was being asked. There is nobody to
|
|
// answer and nothing went wrong on our side: 499 is only for the access log.
|
|
c.AbortWithStatus(statusClientClosed)
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
c.JSON(http.StatusOK, toSimilarJSON(res))
|
|
}
|
|
}
|
|
|
|
// tagsHandlers serve the tags that providers gave to an artist.
|
|
type tagsHandlers struct{ svc tagService }
|
|
|
|
type tagJSON struct {
|
|
Name string `json:"name"`
|
|
Weight float64 `json:"weight"`
|
|
Genre bool `json:"genre"`
|
|
}
|
|
|
|
type tagSourceJSON struct {
|
|
Provider string `json:"provider"`
|
|
ProviderStatus string `json:"provider_status"`
|
|
Stale bool `json:"stale"`
|
|
RetrievedAt *string `json:"retrieved_at"`
|
|
Attribution string `json:"attribution"`
|
|
Tags []tagJSON `json:"tags"`
|
|
}
|
|
|
|
func (h *tagsHandlers) tags(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
res, err := h.svc.Tags(c.Request.Context(), id)
|
|
switch {
|
|
case errors.Is(err, discovery.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
|
|
case errors.Is(err, context.Canceled):
|
|
c.AbortWithStatus(statusClientClosed)
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
sources := make([]tagSourceJSON, len(res.Sources))
|
|
for i, s := range res.Sources {
|
|
out := tagSourceJSON{
|
|
Provider: s.Provider, ProviderStatus: s.Status, Stale: s.Stale,
|
|
Attribution: s.Attribution, Tags: make([]tagJSON, len(s.Tags)),
|
|
}
|
|
if !s.RetrievedAt.IsZero() {
|
|
out.RetrievedAt = rfc3339(s.RetrievedAt)
|
|
}
|
|
for j, t := range s.Tags {
|
|
out.Tags[j] = tagJSON{Name: t.Name, Weight: t.Weight, Genre: t.Genre}
|
|
}
|
|
sources[i] = out
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"sources": sources})
|
|
}
|
|
}
|