sonda/internal/api/http/handlers_about.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
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.
2026-11-15 17:30:29 +01:00

65 lines
1.9 KiB
Go

package httpapi
import (
"context"
"errors"
"net/http"
"github.com/gin-gonic/gin"
"git.kmalloc.org/squid/sonda/internal/about"
"git.kmalloc.org/squid/sonda/internal/discovery"
)
type aboutHandlers struct{ svc aboutService }
type descriptionJSON struct {
Text string `json:"text"`
Short string `json:"short"`
Language string `json:"language"`
Title string `json:"title"`
URL string `json:"url"`
License string `json:"license"`
LicenseURL string `json:"license_url"`
}
type aboutJSON struct {
Provider string `json:"provider"`
ProviderStatus string `json:"provider_status"`
Stale bool `json:"stale"`
RetrievedAt *string `json:"retrieved_at"`
Attribution string `json:"attribution"`
Description *descriptionJSON `json:"description"`
}
// description shows what Wikipedia says about an artist.
func (h *aboutHandlers) description(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
res, err := h.svc.Description(c.Request.Context(), id)
switch {
case errors.Is(err, discovery.ErrNotFound):
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
return
case errors.Is(err, context.Canceled):
c.AbortWithStatus(statusClientClosed) // the client left; nobody is waiting
return
case err != nil:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
return
}
out := aboutJSON{Provider: about.Provider, ProviderStatus: res.Status, Stale: res.Stale, Attribution: about.Attribution}
if !res.RetrievedAt.IsZero() {
out.RetrievedAt = rfc3339(res.RetrievedAt)
}
if d := res.Description; d != nil {
out.Description = &descriptionJSON{
Text: d.Text, Short: d.Short, Language: d.Language, Title: d.Title, URL: d.URL,
License: about.LicenseName, LicenseURL: about.LicenseURL,
}
}
c.JSON(http.StatusOK, out)
}