Files
minstrel/internal/api/admin_provider_sources.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

133 lines
4.3 KiB
Go

package api
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/Fabled-Sword/minstrel/internal/apierror"
"github.com/Fabled-Sword/minstrel/internal/providers"
)
// The admin cards for outside sources, Cover sources and Tag sources, run
// on these. Each family's handlers pass their own providers.Service, so the
// two cards answer in one shape and share no rows.
// providerSourceResp is one provider in a sources list.
type providerSourceResp struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
RequiresAPIKey bool `json:"requires_api_key"`
Supports []string `json:"supports"`
Enabled bool `json:"enabled"`
APIKeySet bool `json:"api_key_set"`
DisplayOrder int32 `json:"display_order"`
Testable bool `json:"testable"`
}
func providerSourceRespFrom(info providers.Info) providerSourceResp {
supports := info.Supports
if supports == nil {
supports = []string{}
}
return providerSourceResp{
ID: info.ID,
DisplayName: info.DisplayName,
RequiresAPIKey: info.RequiresAPIKey,
Supports: supports,
Enabled: info.Enabled,
APIKeySet: info.APIKeySet,
DisplayOrder: info.DisplayOrder,
Testable: info.Testable,
}
}
type providerSourcesListResp struct {
Providers []providerSourceResp `json:"providers"`
SourcesVersion int32 `json:"sources_version"`
}
// updateProviderSourceReq is the PATCH body. A nil field is left as it
// is; an empty api_key clears the key.
type updateProviderSourceReq struct {
Enabled *bool `json:"enabled,omitempty"`
APIKey *string `json:"api_key,omitempty"`
}
type updateProviderSourceResp struct {
providerSourceResp
VersionBumped bool `json:"version_bumped"`
}
// testProviderSourceResp is a Test-button result. It answers 200 whether
// or not the test passed: the result is data, not an error.
type testProviderSourceResp struct {
OK bool `json:"ok"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
}
var errNoSuchProvider = apierror.New(http.StatusNotFound, "not_found", "no such provider")
// listProviderSources answers GET /api/admin/{family}-sources.
func listProviderSources(w http.ResponseWriter, svc *providers.Service) {
infos := svc.ListProviderInfo()
out := providerSourcesListResp{
Providers: make([]providerSourceResp, 0, len(infos)),
SourcesVersion: svc.CurrentVersion(),
}
for _, info := range infos {
out.Providers = append(out.Providers, providerSourceRespFrom(info))
}
writeJSON(w, http.StatusOK, out)
}
// updateProviderSource answers PATCH /api/admin/{family}-sources/{provider_id}
// with the provider as it now stands.
func (h *handlers) updateProviderSource(w http.ResponseWriter, r *http.Request, svc *providers.Service) {
id := chi.URLParam(r, "provider_id")
var body updateProviderSourceReq
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, apierror.BadRequest("bad_body", "invalid JSON"))
return
}
bumped, err := svc.UpdateProvider(r.Context(), id, providers.UpdatePatch{Enabled: body.Enabled, APIKey: body.APIKey})
if err != nil {
if errors.Is(err, providers.ErrProviderNotFound) {
writeErr(w, errNoSuchProvider)
return
}
h.logger.Error("admin: update provider source", "id", id, "err", err)
writeErr(w, apierror.InternalMsg("update failed", err))
return
}
var found providerSourceResp
for _, info := range svc.ListProviderInfo() {
if info.ID == id {
found = providerSourceRespFrom(info)
break
}
}
writeJSON(w, http.StatusOK, updateProviderSourceResp{providerSourceResp: found, VersionBumped: bumped})
}
// testProviderSource answers POST /api/admin/{family}-sources/{provider_id}/test.
func testProviderSource(w http.ResponseWriter, r *http.Request, svc *providers.Service) {
id := chi.URLParam(r, "provider_id")
start := time.Now()
err := svc.TestProvider(r.Context(), id)
duration := time.Since(start).Milliseconds()
switch {
case err == nil:
writeJSON(w, http.StatusOK, testProviderSourceResp{OK: true, DurationMs: duration})
case errors.Is(err, providers.ErrProviderNotFound):
writeErr(w, errNoSuchProvider)
default:
// Not testable, or the test failed.
writeJSON(w, http.StatusOK, testProviderSourceResp{OK: false, DurationMs: duration, Error: err.Error()})
}
}