mirror of
https://github.com/PerpetualSoftware/pad.git
synced 2026-09-21 01:53:33 +00:00
7cda0d7896
Migrates from xarmian/pad to PerpetualSoftware/pad across the entire
repo and updates the product subtitle to "Collaborate with your AI
agents".
Go module rename
- go.mod: github.com/xarmian/pad → github.com/PerpetualSoftware/pad
- All Go imports updated across cmd/pad, internal/{cli,server,store,
models,collections,items,events,metrics,webhooks} (~130 files)
- Test fixtures with the literal repo slug ("xarmian/pad" in JSON
shapes, SSH/HTTPS git URL strings, workspace_context fixtures)
also updated, including the secondary repo entry
(xarmian/pad-web → PerpetualSoftware/pad-web — pad-web was also
moved to the org per branch context)
Docs / config
- README badges, install instructions, brew tap, Docker image, source
build path, sponsor link (sponsor link kept as personal @xarmian)
- Subtitle: "Project management for developers and AI agents." →
"Collaborate with your AI agents." (README, manifests, web layout
meta, .goreleaser homebrew description)
- CONTRIBUTING.md, SECURITY.md, skills/INSTALL.md
- .goreleaser.yaml: homebrew_casks owner, GHCR image, release github
owner, cosign cert-identity regex, comments
- .github/workflows/release.yml: tap/release comments
- deploy/k8s/deployment.yaml: container image
- docs/deployment.md: clone URL
- web/static/{site.webmanifest,manifest.json}: description
- web/src/routes/+layout.svelte: meta description + og:description
Brew tap path is PerpetualSoftware/tap/pad (CamelCase, matches
GitHub user case). GHCR image is ghcr.io/perpetualsoftware/pad
(lowercased per GHCR's URL normalization). CODEOWNERS @xarmian and
FUNDING.yml github: xarmian intentionally retained — those are the
personal maintainer / sponsor account, separate from the org repo.
Verification: go build ./..., go test ./... (all pkgs pass), web
build, and make install all clean (TASK-844, TASK-845).
365 lines
9.8 KiB
Go
365 lines
9.8 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/PerpetualSoftware/pad/internal/collections"
|
|
"github.com/PerpetualSoftware/pad/internal/events"
|
|
"github.com/PerpetualSoftware/pad/internal/models"
|
|
)
|
|
|
|
func normalizeWorkspaceInput(input *models.WorkspaceCreate) error {
|
|
if input == nil {
|
|
return nil
|
|
}
|
|
|
|
settings, err := models.NormalizeWorkspaceSettings(input.Settings)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid settings JSON: %w", err)
|
|
}
|
|
if input.Context != nil {
|
|
settings, err = models.ApplyWorkspaceContext(settings, input.Context)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid workspace context: %w", err)
|
|
}
|
|
}
|
|
input.Settings = settings
|
|
return nil
|
|
}
|
|
|
|
func normalizeWorkspaceUpdateInput(input *models.WorkspaceUpdate) error {
|
|
if input == nil {
|
|
return nil
|
|
}
|
|
|
|
if input.Settings != nil {
|
|
settings, err := models.NormalizeWorkspaceSettings(*input.Settings)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid settings JSON: %w", err)
|
|
}
|
|
input.Settings = &settings
|
|
}
|
|
|
|
if input.Context != nil {
|
|
base := "{}"
|
|
if input.Settings != nil {
|
|
base = *input.Settings
|
|
}
|
|
settings, err := models.ApplyWorkspaceContext(base, input.Context)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid workspace context: %w", err)
|
|
}
|
|
input.Settings = &settings
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
resp := map[string]interface{}{"status": "ok"}
|
|
if s.version != "" {
|
|
resp["version"] = s.version
|
|
}
|
|
if s.commit != "" {
|
|
resp["commit"] = s.commit
|
|
}
|
|
if s.buildTime != "" {
|
|
resp["build_time"] = s.buildTime
|
|
}
|
|
resp["cloud_mode"] = s.cloudMode
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// handleHealthLive is a lightweight liveness probe — always returns 200 if the
|
|
// process is running. Kubernetes uses this to decide whether to restart the pod.
|
|
func (s *Server) handleHealthLive(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
}
|
|
|
|
// handleHealthReady is a readiness probe — returns 200 only when the service
|
|
// can accept traffic (DB connection healthy). Kubernetes uses this to decide
|
|
// whether to route traffic to the pod.
|
|
func (s *Server) handleHealthReady(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.store.Ping(); err != nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"status": "not ready",
|
|
"error": "database unavailable",
|
|
})
|
|
return
|
|
}
|
|
|
|
resp := map[string]interface{}{
|
|
"status": "ready",
|
|
}
|
|
|
|
// Include connection pool stats (useful for debugging, not required for pass/fail).
|
|
dbStats := s.store.DB().Stats()
|
|
resp["db"] = map[string]interface{}{
|
|
"open_connections": dbStats.OpenConnections,
|
|
"in_use": dbStats.InUse,
|
|
"idle": dbStats.Idle,
|
|
"driver": string(s.store.D().Driver()),
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func (s *Server) handleListTemplates(w http.ResponseWriter, r *http.Request) {
|
|
type templateInfo struct {
|
|
Name string `json:"name"`
|
|
Category string `json:"category"`
|
|
Description string `json:"description"`
|
|
Icon string `json:"icon"`
|
|
Collections []string `json:"collections"`
|
|
}
|
|
templates := collections.ListTemplates()
|
|
result := make([]templateInfo, 0, len(templates))
|
|
for _, t := range templates {
|
|
colls := make([]string, 0, len(t.Collections))
|
|
for _, c := range t.Collections {
|
|
colls = append(colls, c.Icon+" "+c.Name)
|
|
}
|
|
result = append(result, templateInfo{
|
|
Name: t.Name,
|
|
Category: t.Category,
|
|
Description: t.Description,
|
|
Icon: t.Icon,
|
|
Collections: colls,
|
|
})
|
|
}
|
|
writeJSON(w, http.StatusOK, result)
|
|
}
|
|
|
|
func (s *Server) handleListWorkspaces(w http.ResponseWriter, r *http.Request) {
|
|
user := currentUser(r)
|
|
|
|
// If a user is authenticated and is not an admin, scope to their memberships.
|
|
if user != nil && user.Role != "admin" {
|
|
workspaces, err := s.store.GetUserWorkspaces(user.ID)
|
|
if err != nil {
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
if workspaces == nil {
|
|
workspaces = []models.Workspace{}
|
|
}
|
|
writeJSON(w, http.StatusOK, workspaces)
|
|
return
|
|
}
|
|
|
|
// Admin users (or fresh-install with no users) see all workspaces.
|
|
// Pass user ID when available so sort_order is included.
|
|
var workspaces []models.Workspace
|
|
var err error
|
|
if user != nil {
|
|
workspaces, err = s.store.ListWorkspacesForUser(user.ID)
|
|
} else {
|
|
workspaces, err = s.store.ListWorkspaces()
|
|
}
|
|
if err != nil {
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
if workspaces == nil {
|
|
workspaces = []models.Workspace{}
|
|
}
|
|
writeJSON(w, http.StatusOK, workspaces)
|
|
}
|
|
|
|
func (s *Server) handleReorderWorkspaces(w http.ResponseWriter, r *http.Request) {
|
|
userID := currentUserID(r)
|
|
if userID == "" {
|
|
writeError(w, http.StatusUnauthorized, "unauthorized", "Authentication required")
|
|
return
|
|
}
|
|
|
|
var input []struct {
|
|
Slug string `json:"slug"`
|
|
SortOrder int `json:"sort_order"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
for _, item := range input {
|
|
ws, err := s.store.GetWorkspaceBySlug(item.Slug)
|
|
if err != nil {
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
if ws == nil {
|
|
continue
|
|
}
|
|
// Skip silently if user is not a member of this workspace
|
|
// (e.g. admin sees all workspaces but may not be joined to all)
|
|
if err := s.store.UpdateWorkspaceSortOrder(userID, ws.ID, item.SortOrder); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
continue
|
|
}
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
}
|
|
|
|
func (s *Server) handleCreateWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
var input models.WorkspaceCreate
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
if input.Name == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Name is required")
|
|
return
|
|
}
|
|
if err := normalizeWorkspaceInput(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
// Set owner to the authenticated user
|
|
if userID := currentUserID(r); userID != "" {
|
|
input.OwnerID = userID
|
|
}
|
|
|
|
// Enforce workspace count limit (user-scoped)
|
|
if userID := currentUserID(r); userID != "" {
|
|
if !s.enforceUserPlanLimit(w, userID, "workspaces") {
|
|
return
|
|
}
|
|
}
|
|
|
|
ws, err := s.store.CreateWorkspace(input)
|
|
if err != nil {
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
|
|
// Seed collections for the new workspace using the requested template
|
|
if err := s.store.SeedCollectionsFromTemplate(ws.ID, input.Template); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Workspace created but failed to seed collections: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Add the creator as workspace owner
|
|
if userID := currentUserID(r); userID != "" {
|
|
_ = s.store.AddWorkspaceMember(ws.ID, userID, "owner")
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, ws)
|
|
}
|
|
|
|
func (s *Server) handleGetWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
ws, ok := s.getWorkspace(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, ws)
|
|
}
|
|
|
|
func (s *Server) handleUpdateWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
if !requireMinRole(w, r, "owner") {
|
|
return
|
|
}
|
|
existing, ok := s.getWorkspace(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var input models.WorkspaceUpdate
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
if err := normalizeWorkspaceUpdateInput(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
ws, err := s.store.UpdateWorkspace(existing.Slug, input)
|
|
if err != nil {
|
|
writeInternalError(w, err)
|
|
return
|
|
}
|
|
if ws == nil {
|
|
writeError(w, http.StatusNotFound, "not_found", "Workspace not found")
|
|
return
|
|
}
|
|
|
|
s.publishEvent(events.WorkspaceUpdated, ws.ID, "", ws.Name, "", "", "")
|
|
|
|
writeJSON(w, http.StatusOK, ws)
|
|
}
|
|
|
|
func (s *Server) handleDeleteWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
if !requireMinRole(w, r, "owner") {
|
|
return
|
|
}
|
|
ws, ok := s.getWorkspace(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
err := s.store.DeleteWorkspace(ws.Slug)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "not_found", "Workspace not found")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) handleExportWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
if !requireMinRole(w, r, "owner") {
|
|
return
|
|
}
|
|
ws, ok := s.getWorkspace(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
export, err := s.store.ExportWorkspace(ws.Slug)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "not_found", err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s-export.json"`, ws.Slug))
|
|
writeJSON(w, http.StatusOK, export)
|
|
}
|
|
|
|
func (s *Server) handleImportWorkspace(w http.ResponseWriter, r *http.Request) {
|
|
var data models.WorkspaceExport
|
|
// WorkspaceExport contains all collections, items, comments, and item
|
|
// versions for the workspace — even a modest project export blows past
|
|
// the default 2 MiB decodeJSON cap. 64 MiB is well above any realistic
|
|
// single-workspace backup while still far from the heap-exhaustion
|
|
// range the default cap protects against.
|
|
if err := decodeJSONWithLimit(r, &data, 64<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "invalid export data: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Optional: override workspace name via query param
|
|
newName := r.URL.Query().Get("name")
|
|
|
|
// Set the authenticated user as owner so the imported workspace is
|
|
// accessible and has correct owner_username for URL routing.
|
|
userID := currentUserID(r)
|
|
ws, err := s.store.ImportWorkspace(&data, newName, userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "import_failed", err.Error())
|
|
return
|
|
}
|
|
|
|
// Add the importer as workspace owner (mirrors handleCreateWorkspace)
|
|
if userID != "" {
|
|
_ = s.store.AddWorkspaceMember(ws.ID, userID, "owner")
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, ws)
|
|
}
|