feat: complete SyncServer implementation
Full-stack Go monolith with embedded React frontend for orchestrating rsync-over-SSH file synchronization with Wake-on-LAN support. Features: - JWT auth (HS256) with bcrypt password hashing - CRUD for machines (with WoL config) and sync_pairs - Ed25519 SSH key generation and known_hosts management - WoL magic packet sender + TCP-connect waiter with backoff - Sync engine: rsync subprocess, per-pair job queue, progress parsing - Homebrew cron parser for scheduled syncs - SSE stream for live job status (queued/waking_up/running/success/failed) - React+TS+Vite+Tailwind SPA embedded via embed.FS - Debian packaging with systemd unit, postinst/prerm/postrm Tech stack: - Go 1.22+ (CGO_ENABLED=0, pure SQLite via modernc.org/sqlite) - chi router for HTTP API - TypeScript + React 18 + Tailwind CSS frontend - Cross-compiled to Linux amd64 for Proxmox LXC deployment Tests: wol (MAC parsing, magic packet), syncengine/queue, scheduler/cron
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/syncserver/internal/syncengine"
|
||||
)
|
||||
|
||||
type SSEHandler struct {
|
||||
engine *syncengine.Engine
|
||||
}
|
||||
|
||||
func NewSSEHandler(engine *syncengine.Engine) *SSEHandler {
|
||||
return &SSEHandler{engine: engine}
|
||||
}
|
||||
|
||||
func (h *SSEHandler) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
jobIDStr := r.URL.Query().Get("job_id")
|
||||
var filterJobID int64
|
||||
if jobIDStr != "" {
|
||||
filterJobID, _ = strconv.ParseInt(jobIDStr, 10, 64)
|
||||
}
|
||||
|
||||
flusher, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
http.Error(w, "SSE not supported", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("X-Accel-Buffering", "no")
|
||||
|
||||
flusher.Flush()
|
||||
|
||||
if h.engine == nil {
|
||||
return
|
||||
}
|
||||
|
||||
events := h.engine.Events()
|
||||
for {
|
||||
select {
|
||||
case evt := <-events:
|
||||
if filterJobID != 0 && evt.JobID != filterJobID {
|
||||
continue
|
||||
}
|
||||
data, _ := json.Marshal(evt)
|
||||
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", evt.Type, data)
|
||||
flusher.Flush()
|
||||
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
|
||||
case <-time.After(30 * time.Second):
|
||||
fmt.Fprintf(w, ": keepalive\n\n")
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user