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:
2026-07-07 15:03:22 -04:00
parent 1a66ac58cd
commit 8e08c73f60
69 changed files with 7949 additions and 152 deletions
+152
View File
@@ -0,0 +1,152 @@
import { useEffect, useState } from 'react';
import { api, SyncPair, Machine } from '../api/client';
export default function SyncPairs() {
const [pairs, setPairs] = useState<SyncPair[]>([]);
const [machines, setMachines] = useState<Machine[]>([]);
const [showForm, setShowForm] = useState(false);
const [form, setForm] = useState({
id: undefined as number | undefined, name: '', source_machine_id: null as number | null, source_path: '',
dest_machine_id: null as number | null, dest_path: '',
direction: 'push', rsync_flags: '-aP', exclude_patterns: '', enabled: true,
});
const [running, setRunning] = useState<Record<number, boolean>>({});
useEffect(() => { load(); }, []);
async function load() {
try {
const [p, m] = await Promise.all([
api<SyncPair[]>('/api/sync-pairs'),
api<Machine[]>('/api/machines'),
]);
setPairs(p);
setMachines(m);
} catch {}
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
try {
const payload = {
name: form.name, source_machine_id: form.source_machine_id, source_path: form.source_path,
dest_machine_id: form.dest_machine_id, dest_path: form.dest_path,
direction: form.direction, rsync_flags: form.rsync_flags,
exclude_patterns: form.exclude_patterns, enabled: form.enabled,
};
await api(form.id ? `/api/sync-pairs/${form.id}` : '/api/sync-pairs', {
method: form.id ? 'PUT' : 'POST',
body: payload,
});
setShowForm(false);
resetForm();
load();
} catch (e: unknown) { alert((e as Error).message); }
}
async function trigger(pairId: number) {
setRunning(r => ({ ...r, [pairId]: true }));
try {
await api(`/api/sync-pairs/${pairId}/run`, { method: 'POST' });
load();
} catch (e: unknown) { alert((e as Error).message); }
setRunning(r => ({ ...r, [pairId]: false }));
}
async function remove(id: number) {
if (!confirm('Delete sync pair?')) return;
try { await api(`/api/sync-pairs/${id}`, { method: 'DELETE' }); load(); } catch { alert('Delete failed'); }
}
function resetForm() {
setForm({ id: undefined, name: '', source_machine_id: null, source_path: '', dest_machine_id: null, dest_path: '', direction: 'push', rsync_flags: '-aP', exclude_patterns: '', enabled: true });
}
function machineName(id: number | null) {
if (!id) return 'Local server';
const m = machines.find(m => m.id === id);
return m ? m.name : `Machine ${id}`;
}
return (
<div className="p-6">
<div className="flex justify-between items-center mb-6">
<h1 className="text-2xl font-bold">Sync Pairs</h1>
<button onClick={() => setShowForm(true)} className="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">
Add Sync Pair
</button>
</div>
{showForm && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<form onSubmit={handleSubmit} className="bg-gray-800 p-6 rounded-lg w-[500px] space-y-3 max-h-[90vh] overflow-y-auto">
<h2 className="text-lg font-bold">Sync Pair</h2>
<input placeholder="Name" value={form.name} onChange={e => setForm({...form, name: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white" required />
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-gray-400 text-xs">Source Machine</label>
<select value={form.source_machine_id ?? ''} onChange={e => setForm({...form, source_machine_id: e.target.value ? +e.target.value : null })} className="w-full bg-gray-700 rounded px-3 py-2 text-white">
<option value="">Local server</option>
{machines.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
</select>
</div>
<div>
<label className="text-gray-400 text-xs">Dest Machine</label>
<select value={form.dest_machine_id ?? ''} onChange={e => setForm({...form, dest_machine_id: e.target.value ? +e.target.value : null })} className="w-full bg-gray-700 rounded px-3 py-2 text-white">
<option value="">Local server</option>
{machines.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
</select>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<input placeholder="Source Path" value={form.source_path} onChange={e => setForm({...form, source_path: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white" required />
<input placeholder="Dest Path" value={form.dest_path} onChange={e => setForm({...form, dest_path: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white" required />
</div>
<div className="grid grid-cols-2 gap-3">
<select value={form.direction} onChange={e => setForm({...form, direction: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white">
<option value="push">Push</option>
<option value="pull">Pull</option>
<option value="mirror">Mirror</option>
</select>
<input placeholder="Rsync Flags" value={form.rsync_flags} onChange={e => setForm({...form, rsync_flags: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white" />
</div>
<textarea placeholder="Exclude Patterns (one per line)" value={form.exclude_patterns} onChange={e => setForm({...form, exclude_patterns: e.target.value})} className="w-full bg-gray-700 rounded px-3 py-2 text-white font-mono text-sm" rows={3} />
<div className="flex gap-2">
<button type="submit" className="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded flex-1">Save</button>
<button type="button" onClick={() => { setShowForm(false); resetForm(); }} className="bg-gray-600 hover:bg-gray-500 text-white px-4 py-2 rounded">Cancel</button>
</div>
</form>
</div>
)}
<table className="w-full text-sm bg-gray-800 rounded-lg overflow-hidden">
<thead className="bg-gray-700">
<tr className="text-left text-gray-400">
<th className="p-3">Name</th>
<th className="p-3">Source</th>
<th className="p-3">Dest</th>
<th className="p-3">Direction</th>
<th className="p-3">Actions</th>
</tr>
</thead>
<tbody>
{pairs.map(p => (
<tr key={p.id} className="border-t border-gray-700">
<td className="p-3 font-medium">{p.name}</td>
<td className="p-3 font-mono text-xs">{machineName(p.source_machine_id)}:{p.source_path}</td>
<td className="p-3 font-mono text-xs">{machineName(p.dest_machine_id)}:{p.dest_path}</td>
<td className="p-3">{p.direction}</td>
<td className="p-3">
<button onClick={() => trigger(p.id)} disabled={running[p.id]} className="text-green-400 hover:text-green-300 mr-3 disabled:opacity-50">
{running[p.id] ? 'Running...' : 'Run'}
</button>
<button onClick={() => remove(p.id)} className="text-red-400 hover:text-red-300">Delete</button>
</td>
</tr>
))}
{pairs.length === 0 && <tr><td colSpan={5} className="p-4 text-center text-gray-500">No sync pairs</td></tr>}
</tbody>
</table>
</div>
);
}