Forked clean from public hackathon repo (Starfire-AgentTeam, BSL 1.1) with full rebrand to Molecule AI under github.com/Molecule-AI/molecule-monorepo. Brand: Starfire → Molecule AI. Slug: starfire / agent-molecule → molecule. Env vars: STARFIRE_* → MOLECULE_*. Go module: github.com/agent-molecule/platform → github.com/Molecule-AI/molecule-monorepo/platform. Python packages: starfire_plugin → molecule_plugin, starfire_agent → molecule_agent. DB: agentmolecule → molecule. History truncated; see public repo for prior commits and contributor attribution. Verified green: go test -race ./... (platform), pytest (workspace-template 1129 + sdk 132), vitest (canvas 352), build (mcp). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ViewportHandler struct{}
|
|
|
|
func NewViewportHandler() *ViewportHandler {
|
|
return &ViewportHandler{}
|
|
}
|
|
|
|
// Get handles GET /canvas/viewport
|
|
func (h *ViewportHandler) Get(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
var x, y, zoom float64
|
|
err := db.DB.QueryRowContext(ctx,
|
|
`SELECT x, y, zoom FROM canvas_viewport ORDER BY saved_at DESC LIMIT 1`,
|
|
).Scan(&x, &y, &zoom)
|
|
if err != nil {
|
|
// No saved viewport — return defaults
|
|
c.JSON(http.StatusOK, gin.H{"x": 0, "y": 0, "zoom": 1})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"x": x, "y": y, "zoom": zoom})
|
|
}
|
|
|
|
// Save handles PUT /canvas/viewport
|
|
func (h *ViewportHandler) Save(c *gin.Context) {
|
|
var body struct {
|
|
X float64 `json:"x"`
|
|
Y float64 `json:"y"`
|
|
Zoom float64 `json:"zoom"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
// Upsert — keep only one viewport record
|
|
_, err := db.DB.ExecContext(ctx, `
|
|
INSERT INTO canvas_viewport (id, x, y, zoom, saved_at)
|
|
VALUES ('00000000-0000-0000-0000-000000000001', $1, $2, $3, now())
|
|
ON CONFLICT (id) DO UPDATE SET x = $1, y = $2, zoom = $3, saved_at = now()
|
|
`, body.X, body.Y, body.Zoom)
|
|
if err != nil {
|
|
log.Printf("Save viewport error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save viewport"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "saved"})
|
|
}
|