sonda/internal/api/http/handlers_libraries.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

120 lines
3.1 KiB
Go

package httpapi
import (
"errors"
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.kmalloc.org/squid/sonda/internal/library"
)
// librariesHandlers are the admin-only routes that manage libraries.
type librariesHandlers struct{ svc libraryService }
type libraryJSON struct {
ID int64 `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Shared bool `json:"shared"`
CreatedAt string `json:"created_at"`
}
func toLibraryJSON(l library.Library) libraryJSON {
return libraryJSON{ID: l.ID, Name: l.Name, Path: l.Path, Shared: l.Shared, CreatedAt: l.CreatedAt.UTC().Format(time.RFC3339)}
}
func writeLibraryError(c *gin.Context, err error) {
switch {
case errors.Is(err, library.ErrInvalidLibrary):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
case errors.Is(err, library.ErrLibraryExists):
writeError(c, http.StatusConflict, "library_exists", err.Error())
case errors.Is(err, library.ErrLibraryNotFound):
writeError(c, http.StatusNotFound, "library_not_found", err.Error())
case errors.Is(err, library.ErrScanRunning):
writeError(c, http.StatusConflict, "scan_running", "wait for the running scan to finish")
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}
func (h *librariesHandlers) list(c *gin.Context) {
libs, err := h.svc.List(c.Request.Context())
if err != nil {
writeLibraryError(c, err)
return
}
out := make([]libraryJSON, len(libs))
for i, l := range libs {
out[i] = toLibraryJSON(l)
}
c.JSON(http.StatusOK, gin.H{"libraries": out})
}
type createLibraryRequest struct {
Name string `json:"name"`
Path string `json:"path"`
}
func (h *librariesHandlers) create(c *gin.Context) {
var req createLibraryRequest
if !bindJSON(c, &req) {
return
}
lib, err := h.svc.Create(c.Request.Context(), req.Name, req.Path)
if err != nil {
writeLibraryError(c, err)
return
}
c.JSON(http.StatusCreated, toLibraryJSON(lib))
}
type updateLibraryRequest struct {
Name *string `json:"name"` // pointers: an absent field keeps the current value
Path *string `json:"path"`
// Shared says whether friends (federation) may read the library.
Shared *bool `json:"shared"`
}
func (h *librariesHandlers) update(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
var req updateLibraryRequest
if !bindJSON(c, &req) {
return
}
if req.Name == nil && req.Path == nil && req.Shared == nil {
writeError(c, http.StatusBadRequest, "invalid_input", "send name, path, shared or any of them")
return
}
var lib library.Library
var err error
if req.Name != nil || req.Path != nil {
lib, err = h.svc.Update(c.Request.Context(), id, req.Name, req.Path)
}
if err == nil && req.Shared != nil {
lib, err = h.svc.SetShared(c.Request.Context(), id, *req.Shared)
}
if err != nil {
writeLibraryError(c, err)
return
}
c.JSON(http.StatusOK, toLibraryJSON(lib))
}
func (h *librariesHandlers) remove(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
writeLibraryError(c, err)
return
}
c.Status(http.StatusNoContent)
}