bc3bc44c4a
Backend:
- New migration: add shutdown_command TEXT column to machines table
- Machine model updated with ShutdownCommand field (Create/GetAll/GetByID/Update)
- MachineRequest/MachineResponse DTOs updated with shutdown_command field
- New ShutdownResponse DTO
- New POST /api/machines/{id}/shutdown handler via SSH
- Refactor sshmanager/testconn.go: extract dialSSH() helper shared with shutdown.go
- New sshmanager/shutdown.go: RunRemoteCommand with 15s timeout
Frontend:
- New shutdownMachine() API helper and ShutdownResponse type in client.ts
- New shutdown_command field in MachineForm
- Power button (amber) in machines table actions
- Shutdown confirmation modal with WoL warning notice
- shutdown_command input field in machine edit/create form
- Machine interface updated with shutdown_command and last_seen_at fields
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/syncserver/internal/auth"
|
|
"github.com/syncserver/internal/config"
|
|
"github.com/syncserver/internal/sshmanager"
|
|
"github.com/syncserver/internal/syncengine"
|
|
"github.com/syncserver/internal/webui"
|
|
)
|
|
|
|
type Server struct {
|
|
router *chi.Mux
|
|
cfg *config.Config
|
|
engine *syncengine.Engine
|
|
}
|
|
|
|
func NewServer(cfg *config.Config, db *sql.DB, engine *syncengine.Engine) *Server {
|
|
auth.InitJWTManager(cfg.Auth.JWTSecret, cfg.Auth.JWTExpiryH)
|
|
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RequestID)
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.Logger)
|
|
r.Use(recoverer)
|
|
|
|
s := &Server{router: r, cfg: cfg, engine: engine}
|
|
|
|
authHandler := NewAuthHandler(db)
|
|
machineHandler := NewMachineHandler(db, engine, cfg)
|
|
syncPairHandler := NewSyncPairHandler(db)
|
|
jobHandler := NewJobHandler(db, engine)
|
|
sseHandler := NewSSEHandler(engine)
|
|
sshKeyHandler := NewSSHKeyHandler(db, cfg)
|
|
|
|
r.Route("/api", func(r chi.Router) {
|
|
r.Route("/auth", func(r chi.Router) {
|
|
r.Post("/login", authHandler.Login)
|
|
r.Post("/logout", authHandler.Logout)
|
|
r.With(auth.RequireAuth).Get("/me", authHandler.Me)
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Route("/machines", func(r chi.Router) {
|
|
r.Get("/", machineHandler.List)
|
|
r.Post("/", machineHandler.Create)
|
|
r.Post("/refresh", machineHandler.Refresh)
|
|
r.Get("/{id}", machineHandler.Get)
|
|
r.Put("/{id}", machineHandler.Update)
|
|
r.Delete("/{id}", machineHandler.Delete)
|
|
r.Post("/{id}/test-wol", machineHandler.TestWoL)
|
|
r.Post("/{id}/shutdown", machineHandler.Shutdown)
|
|
r.Post("/{id}/test-connection", machineHandler.TestConnection)
|
|
r.Post("/{id}/approve-fingerprint", machineHandler.ApproveFingerprint)
|
|
r.Post("/{id}/deploy-keys", machineHandler.DeployKeys)
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Route("/sync-pairs", func(r chi.Router) {
|
|
r.Get("/", syncPairHandler.List)
|
|
r.Post("/", syncPairHandler.Create)
|
|
r.Get("/{id}", syncPairHandler.Get)
|
|
r.Put("/{id}", syncPairHandler.Update)
|
|
r.Delete("/{id}", syncPairHandler.Delete)
|
|
r.Post("/{id}/run", jobHandler.TriggerRun)
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Route("/jobs", func(r chi.Router) {
|
|
r.Get("/", jobHandler.List)
|
|
r.Get("/{id}", jobHandler.Get)
|
|
r.Post("/{id}/cancel", jobHandler.Cancel)
|
|
r.Get("/{id}/log", jobHandler.GetLog)
|
|
r.Get("/{id}/log/download", jobHandler.DownloadLog)
|
|
r.Get("/{id}/log/stream", sseHandler.StreamJob)
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Get("/jobs/stream", sseHandler.StreamAll)
|
|
|
|
r.With(auth.RequireAuth).Get("/settings/pubkey", func(w http.ResponseWriter, r *http.Request) {
|
|
_, _, pubKey, _ := sshmanager.EnsureServerKey(cfg.SSHDir())
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.Write([]byte(pubKey))
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Get("/settings/info", func(w http.ResponseWriter, r *http.Request) {
|
|
_, _, pubKey, _ := sshmanager.EnsureServerKey(cfg.SSHDir())
|
|
resp := SettingsInfoResponse{
|
|
Version: cfg.Version,
|
|
DataDir: cfg.DataDir,
|
|
SSHPubKey: pubKey,
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(resp)
|
|
})
|
|
|
|
r.With(auth.RequireAuth).Route("/ssh-keys", func(r chi.Router) {
|
|
r.Get("/", sshKeyHandler.List)
|
|
r.Post("/", sshKeyHandler.Create)
|
|
r.Get("/{id}", sshKeyHandler.Get)
|
|
r.Delete("/{id}", sshKeyHandler.Delete)
|
|
r.Get("/{id}/private", sshKeyHandler.DownloadPrivate)
|
|
})
|
|
})
|
|
|
|
r.Get("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
}))
|
|
|
|
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
|
webui.ServeSPA().ServeHTTP(w, r)
|
|
})
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
s.router.ServeHTTP(w, r)
|
|
}
|
|
|
|
func recoverer(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
slog.Error("panic recovered",
|
|
"error", err,
|
|
"stack", string(debug.Stack()),
|
|
"method", r.Method,
|
|
"path", r.URL.Path,
|
|
)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
http.Error(w, `{"error":"internal server error"}`, http.StatusInternalServerError)
|
|
}
|
|
}()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|