Files
mc-gateway/internal/adminservice/repository.go
tursom 13ab1f6964
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
feat: add sqlite-backed admin management
2026-06-25 08:53:44 +08:00

105 lines
2.5 KiB
Go

package adminservice
import (
"context"
"database/sql"
"encoding/json"
"time"
)
type Repository struct {
db *sql.DB
now func() time.Time
}
func NewRepository(db *sql.DB) Repository {
return Repository{
db: db,
now: time.Now,
}
}
func NewRepositoryWithClock(db *sql.DB, now func() time.Time) Repository {
repo := NewRepository(db)
if now != nil {
repo.now = now
}
return repo
}
func (r Repository) EnsureDefaults(ctx context.Context, tcpAdminPort int) error {
now := r.now().Unix()
for _, service := range DefaultRecords(tcpAdminPort) {
options, err := json.Marshal(service.Options)
if err != nil {
return err
}
if _, err := r.db.ExecContext(ctx, `
INSERT INTO services(name, enabled, port, options_json, restart_required, created_at, updated_at)
VALUES (?, ?, ?, ?, 0, ?, ?)
ON CONFLICT(name) DO NOTHING`,
service.Name, boolToInt(service.Enabled), service.Port, string(options), now, now); err != nil {
return err
}
}
return nil
}
func (r Repository) List(ctx context.Context) ([]Record, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT name, enabled, port, options_json, restart_required, created_at, updated_at, updated_by
FROM services
ORDER BY CASE name
WHEN 'tcp_admin' THEN 0
WHEN 'kcp' THEN 1
WHEN 'quic' THEN 2
WHEN 'websocket' THEN 3
ELSE 4
END, name`)
if err != nil {
return nil, err
}
defer rows.Close()
var services []Record
for rows.Next() {
var service Record
var enabled, restartRequired int
var optionsJSON string
if err := rows.Scan(&service.Name, &enabled, &service.Port, &optionsJSON, &restartRequired, &service.CreatedAt, &service.UpdatedAt, &service.UpdatedBy); err != nil {
return nil, err
}
service.Enabled = enabled != 0
service.RestartRequired = restartRequired != 0
service.Options = DecodeOptions(optionsJSON)
services = append(services, service)
}
return services, rows.Err()
}
func (r Repository) Update(ctx context.Context, actor, name string, enabled bool, port int, options map[string]any) error {
if err := ValidateUpdate(name, enabled, port); err != nil {
return err
}
optionsJSON, err := json.Marshal(NormalizeOptions(name, options))
if err != nil {
return err
}
_, err = r.db.ExecContext(ctx, `
UPDATE services
SET enabled = ?, port = ?, options_json = ?, restart_required = 1, updated_at = ?, updated_by = ?
WHERE name = ?`,
boolToInt(enabled), port, string(optionsJSON), r.now().Unix(), actor, name)
return err
}
func boolToInt(value bool) int {
if value {
return 1
}
return 0
}