Files
artifactapi/internal/api/v2/probe.go
T
benvin b46c116f6b
ci/woodpecker/tag/docker Pipeline was successful
Feat/v3 go rewrite (#47)
Complete rewrite of ArtifactAPI from Python/FastAPI to Go as a single binary.

Core engine:
- 10 package providers: generic, docker, helm, pypi, npm, rpm, alpine,
  puppet, terraform, goproxy — each with built-in mutable patterns
- Content-addressable storage (SHA256 dedup across all remotes)
- Three-tier caching: Redis (TTL/locks) → S3/MinIO (blobs) → upstream
- Classifier with allowlist/blocklist per-remote (empty = allow all)
- Circuit breaker, conditional revalidation, stale-on-error
- Background garbage collection for orphaned blobs
- Access logging to PostgreSQL

API:
- v1 proxy endpoints (backwards compatible)
- v2 management API: CRUD remotes/virtuals, object browser, stats,
  health, SSE events, probe/test endpoint
- Virtual repos with index merging (Helm YAML + PyPI HTML)

Frontend (React + Vite, separate Dockerfile):
- Dashboard with stats, health indicators, top remotes
- Remotes list with type filter, remote detail with config/patterns
- Object browser with pagination and evict
- Test Remote page: probe any remote path, see headers/size/timing
- Virtuals page with expandable member lists

TUI (Bubble Tea):
- Dashboard, remotes list/detail, object browser, virtuals
- Vim-style navigation, artifactapi tui --endpoint <url>

Infrastructure:
- S3 client supports MinIO, Ceph RGW, AWS S3 (minio-go)
- PostgreSQL schema with migrations
- Docker Compose: API + UI + Postgres 17 + Redis 7 + MinIO
- Makefile with Go version check, build/test/lint/fmt/e2e targets
- Distroless Docker image (~15MB)

Testing:
- Unit tests for models, classifier, providers, mergers
- E2E tests with testcontainers-go (real Postgres/Redis/MinIO)

Terraform config:
- All 40 production remotes + helm virtual as HCL
- Provider repo: terraform-provider-artifactapi v0.0.1 (separate)

---------

Co-authored-by: Ben Vincent <ben@unkin.net>
Reviewed-on: #47
2026-06-07 19:30:35 +10:00

110 lines
2.6 KiB
Go

package v2
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"git.unkin.net/unkin/artifactapi/internal/database"
"git.unkin.net/unkin/artifactapi/internal/provider"
"git.unkin.net/unkin/artifactapi/internal/proxy"
)
type ProbeHandler struct {
engine *proxy.Engine
db *database.DB
}
func NewProbeHandler(engine *proxy.Engine, db *database.DB) *ProbeHandler {
return &ProbeHandler{engine: engine, db: db}
}
func (h *ProbeHandler) Routes() chi.Router {
r := chi.NewRouter()
r.Post("/", h.probe)
return r
}
type probeRequest struct {
Remote string `json:"remote"`
Path string `json:"path"`
}
type probeResponse struct {
Status int `json:"status"`
Source string `json:"source,omitempty"`
ContentType string `json:"content_type,omitempty"`
SizeBytes int64 `json:"size_bytes"`
Headers map[string]string `json:"headers,omitempty"`
DurationMS int64 `json:"duration_ms"`
Error string `json:"error,omitempty"`
}
func (h *ProbeHandler) probe(w http.ResponseWriter, r *http.Request) {
var req probeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid json", http.StatusBadRequest)
return
}
if req.Remote == "" || req.Path == "" {
http.Error(w, "remote and path are required", http.StatusBadRequest)
return
}
remote, err := h.db.GetRemote(r.Context(), req.Remote)
if err != nil {
writeJSON(w, http.StatusOK, probeResponse{
Status: 404,
Error: fmt.Sprintf("remote %q not found", req.Remote),
})
return
}
prov, err := provider.Get(remote.PackageType)
if err != nil {
writeJSON(w, http.StatusOK, probeResponse{
Status: 500,
Error: fmt.Sprintf("no provider for %q", remote.PackageType),
})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
start := time.Now()
result, err := h.engine.Fetch(ctx, *remote, req.Path, prov)
duration := time.Since(start).Milliseconds()
if err != nil {
writeJSON(w, http.StatusOK, probeResponse{
Status: 502,
DurationMS: duration,
Error: err.Error(),
})
return
}
io.Copy(io.Discard, result.Reader)
result.Reader.Close()
writeJSON(w, http.StatusOK, probeResponse{
Status: 200,
Source: result.Source,
ContentType: result.ContentType,
SizeBytes: result.Size,
DurationMS: duration,
Headers: map[string]string{
"X-Artifact-Source": result.Source,
"X-Artifact-Size": fmt.Sprintf("%d", result.Size),
"Content-Type": result.ContentType,
},
})
}