Files
TradarrV2/backend/internal/api/handlers/admin.go

604 lines
17 KiB
Go

package handlers
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/tradarr/backend/internal/auth"
"github.com/tradarr/backend/internal/sourcefeeds"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/ai"
"github.com/tradarr/backend/internal/httputil"
"github.com/tradarr/backend/internal/models"
)
// ── Credentials ────────────────────────────────────────────────────────────
type credentialsRequest struct {
SourceID string `json:"source_id" binding:"required"`
Username string `json:"username" binding:"required"`
Password string `json:"password"`
}
func (h *Handler) GetCredentials(c *gin.Context) {
sources, err := h.repo.ListSources()
if err != nil {
httputil.InternalError(c, err)
return
}
type credResponse struct {
SourceID string `json:"source_id"`
SourceName string `json:"source_name"`
Username string `json:"username"`
HasPassword bool `json:"has_password"`
}
var result []credResponse
for _, src := range sources {
if src.Type != "bloomberg" {
continue
}
cred, _ := h.repo.GetCredentials(src.ID)
r := credResponse{SourceID: src.ID, SourceName: src.Name}
if cred != nil {
r.Username = cred.Username
r.HasPassword = cred.PasswordEncrypted != ""
}
result = append(result, r)
}
httputil.OK(c, result)
}
func (h *Handler) UpsertCredentials(c *gin.Context) {
var req credentialsRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
encPwd := ""
if req.Password != "" {
var err error
encPwd, err = h.enc.Encrypt(req.Password)
if err != nil {
httputil.InternalError(c, err)
return
}
}
if err := h.repo.UpsertCredentials(req.SourceID, req.Username, encPwd); err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, gin.H{"ok": true})
}
// ── AI Providers ───────────────────────────────────────────────────────────
type aiProviderRequest struct {
Name string `json:"name" binding:"required"`
APIKey string `json:"api_key"`
Model string `json:"model"`
Endpoint string `json:"endpoint"`
}
func (h *Handler) ListAIProviders(c *gin.Context) {
providers, err := h.repo.ListAIProviders()
if err != nil {
httputil.InternalError(c, err)
return
}
// Ne pas exposer les clés chiffrées — juste indiquer si elle existe
type safeProvider struct {
ID string `json:"id"`
Name string `json:"name"`
Model string `json:"model"`
Endpoint string `json:"endpoint"`
IsActive bool `json:"is_active"`
HasKey bool `json:"has_key"`
}
var result []safeProvider
for _, p := range providers {
result = append(result, safeProvider{
ID: p.ID,
Name: p.Name,
Model: p.Model,
Endpoint: p.Endpoint,
IsActive: p.IsActive,
HasKey: p.APIKeyEncrypted != "",
})
}
httputil.OK(c, result)
}
func (h *Handler) CreateAIProvider(c *gin.Context) {
var req aiProviderRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
encKey := ""
if req.APIKey != "" {
var err error
encKey, err = h.enc.Encrypt(req.APIKey)
if err != nil {
httputil.InternalError(c, err)
return
}
}
p, err := h.repo.CreateAIProvider(req.Name, encKey, req.Model, req.Endpoint)
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.Created(c, p)
}
func (h *Handler) UpdateAIProvider(c *gin.Context) {
id := c.Param("id")
var req aiProviderRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
existing, err := h.repo.GetAIProviderByID(id)
if err != nil || existing == nil {
httputil.NotFound(c)
return
}
encKey := existing.APIKeyEncrypted
if req.APIKey != "" {
encKey, err = h.enc.Encrypt(req.APIKey)
if err != nil {
httputil.InternalError(c, err)
return
}
}
if err := h.repo.UpdateAIProvider(id, encKey, req.Model, req.Endpoint); err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, gin.H{"ok": true})
}
func (h *Handler) SetActiveAIProvider(c *gin.Context) {
id := c.Param("id")
if err := h.repo.SetActiveAIProvider(id); err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, gin.H{"ok": true})
}
func (h *Handler) DeleteAIProvider(c *gin.Context) {
if err := h.repo.DeleteAIProvider(c.Param("id")); err != nil {
if errors.Is(err, models.ErrCatalogConflict) {
c.JSON(409, gin.H{"error": "provider_in_use"})
return
}
httputil.InternalError(c, err)
return
}
httputil.NoContent(c)
}
func (h *Handler) ProbeAIModels(c *gin.Context) {
var req aiProviderRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
provider, err := h.pipeline.BuildProvider(req.Name, req.APIKey, req.Endpoint)
if err != nil {
httputil.InternalError(c, err)
return
}
models, err := provider.ListModels(c.Request.Context())
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, models)
}
func (h *Handler) ListAIModels(c *gin.Context) {
id := c.Param("id")
p, err := h.repo.GetAIProviderByID(id)
if err != nil || p == nil {
httputil.NotFound(c)
return
}
apiKey := ""
if p.APIKeyEncrypted != "" {
apiKey, err = h.enc.Decrypt(p.APIKeyEncrypted)
if err != nil {
httputil.InternalError(c, err)
return
}
}
provider, err := h.pipeline.BuildProvider(p.Name, apiKey, p.Endpoint)
if err != nil {
httputil.InternalError(c, err)
return
}
models, err := provider.ListModels(c.Request.Context())
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, models)
}
// ── Sources ────────────────────────────────────────────────────────────────
func (h *Handler) ListSources(c *gin.Context) {
sources, err := h.repo.ListSources()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
type sourceResponse struct {
models.Source
Supported bool `json:"supported"`
Operational bool `json:"operational"`
}
result := make([]sourceResponse, 0, len(sources))
for _, source := range sources {
if source.SourceKey != nil && sourcefeeds.Supported(*source.SourceKey) {
source.FeedURLs, err = h.repo.SourceFeedURLs(c.Request.Context(), *source.SourceKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
}
result = append(result, sourceResponse{Source: source, Supported: source.Supported(), Operational: source.Operational()})
}
httputil.OK(c, result)
}
type updateSourceRequest struct {
FeedURLs json.RawMessage `json:"feed_urls"`
Enabled bool `json:"enabled"`
ArticleLimit *int `json:"article_limit" binding:"omitempty,min=1,max=200"`
}
func (h *Handler) UpdateSource(c *gin.Context) {
var req *updateSourceRequest
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 8192)
decoder := json.NewDecoder(c.Request.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&req); err != nil || req == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_configuration_invalid"})
return
}
if err := decoder.Decode(&struct{}{}); err != io.EOF || (req.ArticleLimit != nil && (*req.ArticleLimit < 1 || *req.ArticleLimit > 200)) {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_configuration_invalid"})
return
}
var feeds *[]string
if len(req.FeedURLs) > 0 {
var values []string
if err := json.Unmarshal(req.FeedURLs, &values); err != nil || values == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_feeds_invalid"})
return
}
feeds = &values
}
if err := h.repo.UpdateSourceConfig(c.Param("id"), req.Enabled, req.ArticleLimit, feeds); err != nil {
if errors.Is(err, sourcefeeds.ErrInvalid) || errors.Is(err, sourcefeeds.ErrUnsupported) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if errors.Is(err, models.ErrSourceNotSupported) {
c.JSON(http.StatusConflict, gin.H{"error": "source_not_supported"})
return
}
if errors.Is(err, sql.ErrNoRows) {
httputil.NotFound(c)
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
httputil.OK(c, gin.H{"ok": true})
}
// ── Settings ───────────────────────────────────────────────────────────────
func (h *Handler) ListSettings(c *gin.Context) {
settings, err := h.repo.ListSettings()
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, settings)
}
type updateSettingsRequest struct {
Settings []models.Setting `json:"settings"`
}
func (h *Handler) UpdateSettings(c *gin.Context) {
var req updateSettingsRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
for _, s := range req.Settings {
if s.Key == "summary_max_articles" {
limit, err := strconv.Atoi(s.Value)
if err != nil || limit < 1 || limit > 500 {
httputil.BadRequest(c, fmt.Errorf("summary_max_articles must be between 1 and 500"))
return
}
}
}
if err := h.repo.SetSettings(req.Settings); err != nil {
httputil.InternalError(c, err)
return
}
if h.scheduler != nil {
if err := h.scheduler.Reload(); err != nil {
fmt.Printf("settings reload scheduler: %v\n", err)
}
}
httputil.OK(c, gin.H{"ok": true})
}
// ── Schedule ───────────────────────────────────────────────────────────────
func (h *Handler) GetSchedule(c *gin.Context) {
slots, err := h.repo.ListScheduleSlots()
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, slots)
}
type scheduleRequest struct {
Slots []struct {
DayOfWeek int `json:"day_of_week"`
Hour int `json:"hour"`
Minute int `json:"minute"`
} `json:"slots"`
}
func (h *Handler) UpdateSchedule(c *gin.Context) {
var req scheduleRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
slots := make([]models.ScheduleSlot, len(req.Slots))
for i, s := range req.Slots {
slots[i] = models.ScheduleSlot{DayOfWeek: s.DayOfWeek, Hour: s.Hour, Minute: s.Minute}
}
if err := h.repo.ReplaceSchedule(slots); err != nil {
httputil.InternalError(c, err)
return
}
if err := h.scheduler.Reload(); err != nil {
fmt.Printf("schedule reload: %v\n", err)
}
httputil.OK(c, gin.H{"ok": true})
}
func (h *Handler) GetDefaultSystemPrompt(c *gin.Context) {
httputil.OK(c, gin.H{"prompt": ai.DefaultSystemPrompt})
}
// ── AI Roles ───────────────────────────────────────────────────────────────
func (h *Handler) GetAIRoles(c *gin.Context) {
roles := []string{"summary"}
resp := gin.H{}
for _, role := range roles {
providerID, _ := h.repo.GetSetting("ai_role_" + role + "_provider")
model, _ := h.repo.GetSetting("ai_role_" + role + "_model")
resp[role] = gin.H{"provider_id": providerID, "model": model}
}
httputil.OK(c, resp)
}
type updateAIRoleRequest struct {
ProviderID string `json:"provider_id"`
Model string `json:"model"`
}
func (h *Handler) UpdateAIRole(c *gin.Context) {
role := c.Param("role")
if role != "summary" {
httputil.BadRequest(c, fmt.Errorf("legacy role is not configurable"))
return
}
var req updateAIRoleRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
if err := h.repo.SetRoleProvider(role, req.ProviderID, req.Model); err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, gin.H{"ok": true})
}
// ── Ollama Model Management ────────────────────────────────────────────────
func (h *Handler) getOllamaManager() (*ai.OllamaProvider, error) {
providers, err := h.repo.ListAIProviders()
if err != nil {
return nil, err
}
endpoint := ""
for _, p := range providers {
if p.Name == "ollama" {
endpoint = p.Endpoint
break
}
}
if endpoint == "" {
endpoint = "http://ollama:11434"
}
return ai.NewOllamaManager(endpoint), nil
}
func (h *Handler) ListOllamaModels(c *gin.Context) {
mgr, err := h.getOllamaManager()
if err != nil {
httputil.InternalError(c, err)
return
}
models, err := mgr.ListModelsDetailed(c.Request.Context())
if err != nil {
httputil.InternalError(c, err)
return
}
if models == nil {
models = []ai.OllamaModelInfo{}
}
httputil.OK(c, models)
}
type pullModelRequest struct {
Name string `json:"name" binding:"required"`
}
func (h *Handler) PullOllamaModel(c *gin.Context) {
var req pullModelRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
mgr, err := h.getOllamaManager()
if err != nil {
httputil.InternalError(c, err)
return
}
go func() {
if err := mgr.PullModel(context.Background(), req.Name); err != nil {
fmt.Printf("[ollama] pull %s failed: %v\n", req.Name, err)
} else {
fmt.Printf("[ollama] pull %s completed\n", req.Name)
}
}()
c.JSON(202, gin.H{"ok": true, "message": "pull started"})
}
func (h *Handler) DeleteOllamaModel(c *gin.Context) {
name := c.Param("name")
mgr, err := h.getOllamaManager()
if err != nil {
httputil.InternalError(c, err)
return
}
if err := mgr.DeleteModel(c.Request.Context(), name); err != nil {
httputil.InternalError(c, err)
return
}
httputil.NoContent(c)
}
// ── Admin Users ────────────────────────────────────────────────────────────
func (h *Handler) ListUsers(c *gin.Context) {
users, err := h.repo.ListUsers()
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, users)
}
type updateUserRequest struct {
Email string `json:"email" binding:"required,email"`
Role string `json:"role" binding:"required"`
}
func (h *Handler) UpdateAdminUser(c *gin.Context) {
id := c.Param("id")
var req updateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
return
}
if req.Role != "admin" && req.Role != "user" {
httputil.BadRequest(c, fmt.Errorf("role must be admin or user"))
return
}
user, err := h.repo.UpdateUser(id, req.Email, models.Role(req.Role))
if err != nil {
accountError(c, err)
return
}
httputil.OK(c, user)
}
func accountError(c *gin.Context, err error) {
switch {
case errors.Is(err, models.ErrLastAdmin):
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot remove the last admin"})
case errors.Is(err, sql.ErrNoRows):
httputil.NotFound(c)
case models.IsDuplicateEmail(err):
c.JSON(http.StatusConflict, gin.H{"error": "email already in use"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "account operation failed"})
}
}
func (h *Handler) DeleteAdminUser(c *gin.Context) {
if err := h.repo.DeleteUser(c.Param("id")); err != nil {
accountError(c, err)
return
}
httputil.NoContent(c)
}
type createUserRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=6,max=72"`
Role string `json:"role" binding:"required,oneof=admin user"`
}
func (h *Handler) CreateAdminUser(c *gin.Context) {
var req createUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account fields"})
return
}
hash, err := auth.HashPassword(req.Password)
if err != nil {
accountError(c, err)
return
}
user, err := h.repo.CreateUser(req.Email, hash, models.Role(req.Role))
if err != nil {
accountError(c, err)
return
}
httputil.Created(c, user)
}
// TestAIProvider returns only bounded outcomes from saved configuration.
func (h *Handler) TestAIProvider(c *gin.Context) {
err := h.connectionTester.Test(c.Request.Context(), c.Param("id"))
switch {
case err == nil:
c.JSON(200, gin.H{"status": "success"})
case errors.Is(err, ai.ErrConnectionAuth):
c.JSON(200, gin.H{"status": "auth_rejected"})
case errors.Is(err, ai.ErrConnectionUnavailable):
c.JSON(200, gin.H{"status": "unavailable"})
case errors.Is(err, ai.ErrConnectionUnsupported):
c.JSON(200, gin.H{"status": "auth_check_unsupported"})
case errors.Is(err, ai.ErrConnectionNotFound):
c.JSON(404, gin.H{"error": "not found"})
default:
c.JSON(500, gin.H{"error": "internal server error"})
}
}