Files
move-data-nas/internal/models/user.go
T
darroyo 8e08c73f60 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
2026-07-07 15:03:22 -04:00

67 lines
1.5 KiB
Go

package models
import (
"database/sql"
"time"
)
type User struct {
ID int64 `db:"id" json:"id"`
Username string `db:"username" json:"username"`
PasswordHash string `db:"password_hash" json:"-"`
Role string `db:"role" json:"role"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
}
type UserRepository struct {
db *sql.DB
}
func NewUserRepository(db *sql.DB) *UserRepository {
return &UserRepository{db: db}
}
func (r *UserRepository) Create(username, passwordHash, role string) (int64, error) {
res, err := r.db.Exec(
"INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)",
username, passwordHash, role,
)
if err != nil {
return 0, err
}
return res.LastInsertId()
}
func (r *UserRepository) GetByUsername(username string) (*User, error) {
var u User
err := r.db.QueryRow(
"SELECT id, username, password_hash, role, created_at FROM users WHERE username = ?",
username,
).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.Role, &u.CreatedAt)
if err != nil {
return nil, err
}
return &u, nil
}
func (r *UserRepository) Exists() (bool, error) {
var n int
err := r.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&n)
if err != nil {
return false, err
}
return n > 0, nil
}
func (r *UserRepository) GetByID(id int64) (*User, error) {
var u User
err := r.db.QueryRow(
"SELECT id, username, password_hash, role, created_at FROM users WHERE id = ?",
id,
).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.Role, &u.CreatedAt)
if err != nil {
return nil, err
}
return &u, nil
}