Files
Erupe/server/api/repo_user.go
Houmgaor ecfe58ffb4 feat: add SQLite support, setup wizard enhancements, and live dashboard
Add zero-dependency SQLite mode so users can run Erupe without
PostgreSQL. A transparent db.DB wrapper auto-translates PostgreSQL
SQL ($N placeholders, now(), ::casts, ILIKE, public. prefix,
TRUNCATE) for SQLite at runtime — all 28 repo files use the wrapper
with no per-query changes needed.

Setup wizard gains two new steps: quest file detection with download
link, and gameplay presets (solo/small/community/rebalanced). The API
server gets a /dashboard endpoint with auto-refreshing stats.

CI release workflow now builds and pushes Docker images to GHCR
alongside binary artifacts on tag push.

Key changes:
- common/db: DB/Tx wrapper with 6 SQL translation rules
- server/migrations/sqlite: full SQLite schema (0001-0005)
- config: Database.Driver field ("postgres" or "sqlite")
- main.go: SQLite connection with WAL mode, single writer
- server/setup: quest check + preset selection steps
- server/api: /dashboard with live stats
- .github/workflows: Docker in release, deduplicate docker.yml
2026-03-05 18:00:30 +01:00

67 lines
1.9 KiB
Go

package api
import (
"context"
"time"
dbutil "erupe-ce/common/db"
)
// APIUserRepository implements APIUserRepo with PostgreSQL.
type APIUserRepository struct {
db *dbutil.DB
}
// NewAPIUserRepository creates a new APIUserRepository.
func NewAPIUserRepository(db *dbutil.DB) *APIUserRepository {
return &APIUserRepository{db: db}
}
func (r *APIUserRepository) Register(ctx context.Context, username, passwordHash string, returnExpires time.Time) (uint32, uint32, error) {
var (
id uint32
rights uint32
)
err := r.db.QueryRowContext(
ctx, `
INSERT INTO users (username, password, return_expires)
VALUES ($1, $2, $3)
RETURNING id, rights
`,
username, passwordHash, returnExpires,
).Scan(&id, &rights)
return id, rights, err
}
func (r *APIUserRepository) GetCredentials(ctx context.Context, username string) (uint32, string, uint32, error) {
var (
id uint32
passwordHash string
rights uint32
)
err := r.db.QueryRowContext(ctx, "SELECT id, password, rights FROM users WHERE username = $1", username).Scan(&id, &passwordHash, &rights)
return id, passwordHash, rights, err
}
func (r *APIUserRepository) GetLastLogin(uid uint32) (time.Time, error) {
var lastLogin time.Time
err := r.db.Get(&lastLogin, "SELECT COALESCE(last_login, now()) FROM users WHERE id=$1", uid)
return lastLogin, err
}
func (r *APIUserRepository) GetReturnExpiry(uid uint32) (time.Time, error) {
var returnExpiry time.Time
err := r.db.Get(&returnExpiry, "SELECT return_expires FROM users WHERE id=$1", uid)
return returnExpiry, err
}
func (r *APIUserRepository) UpdateReturnExpiry(uid uint32, expiry time.Time) error {
_, err := r.db.Exec("UPDATE users SET return_expires=$1 WHERE id=$2", expiry, uid)
return err
}
func (r *APIUserRepository) UpdateLastLogin(uid uint32, loginTime time.Time) error {
_, err := r.db.Exec("UPDATE users SET last_login=$1 WHERE id=$2", loginTime, uid)
return err
}