build: add makefile, dockerfile, ci and development stack
- build: add makefile, dockerfile, ci and development stack - feat(config): add environment configuration and logger - feat(server): add server lifecycle with health and version routes - feat(db): add connection, contract check, migrations and stores - feat(cli): add command dispatch and user management - test(db): add integration tests against mysql
This commit is contained in:
1 parent
45d1e307c6
commit
7089a6f408
46 files changed
+8456
No files matched your search
@@ -0,0 +1,144 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type delegationRow struct {
|
||||
Zone string
|
||||
UserID int64
|
||||
Username string
|
||||
CreatedAt time.Time
|
||||
CreatedBy int64
|
||||
}
|
||||
|
||||
type userZone struct {
|
||||
Zone string
|
||||
CreatedAt time.Time
|
||||
ZoneExists bool
|
||||
}
|
||||
|
||||
type delegationStore struct {
|
||||
q dbtx
|
||||
t tableNames
|
||||
}
|
||||
|
||||
func newDelegationStore(q dbtx, t tableNames) delegationStore {
|
||||
return delegationStore{q: q, t: t}
|
||||
}
|
||||
|
||||
func (s delegationStore) GrantDelegation(ctx context.Context, zone string, userID, createdBy int64, now time.Time) (bool, error) {
|
||||
_, err := s.q.ExecContext(ctx,
|
||||
s.t.sql("INSERT INTO {delegations} (`zone`, `user_id`, `created_at`, `created_by`) VALUES (?, ?, ?, ?)"),
|
||||
zone, userID, dbTime(now), createdBy)
|
||||
if isDuplicateKey(err) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("grant delegation: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s delegationStore) RevokeDelegation(ctx context.Context, zone string, userID int64) (bool, error) {
|
||||
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {delegations} WHERE `zone` = ? AND `user_id` = ?"), zone, userID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("revoke delegation: %w", err)
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("revoke delegation: %w", err)
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (s delegationStore) ZoneUsers(ctx context.Context, zone string) ([]userRow, error) {
|
||||
rows, err := s.q.QueryContext(ctx,
|
||||
s.t.sql("SELECT "+userColumns+" FROM {delegations} d JOIN {users} u ON u.`id` = d.`user_id` "+
|
||||
"WHERE d.`zone` = ? AND u.`role` = 'user' AND u.`deleted_at` IS NULL ORDER BY u.`username`"), zone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("zone users: %w", err)
|
||||
}
|
||||
return collectUsers(rows)
|
||||
}
|
||||
|
||||
func (s delegationStore) UserZones(ctx context.Context, userID int64) ([]userZone, error) {
|
||||
rows, err := s.q.QueryContext(ctx,
|
||||
s.t.sql("SELECT d.`zone`, d.`created_at`, EXISTS (SELECT 1 FROM {records} r WHERE r.`zone` = d.`zone`) "+
|
||||
"FROM {delegations} d WHERE d.`user_id` = ? ORDER BY d.`zone`"), userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("user zones: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var zones []userZone
|
||||
for rows.Next() {
|
||||
var z userZone
|
||||
if err := rows.Scan(&z.Zone, &z.CreatedAt, &z.ZoneExists); err != nil {
|
||||
return nil, fmt.Errorf("user zones: %w", err)
|
||||
}
|
||||
scanUTC(&z.CreatedAt)
|
||||
zones = append(zones, z)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("user zones: %w", err)
|
||||
}
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func (s delegationStore) DeleteZoneDelegations(ctx context.Context, zone string) ([]delegationRow, error) {
|
||||
rows, err := s.q.QueryContext(ctx,
|
||||
s.t.sql("SELECT d.`zone`, d.`user_id`, u.`username`, d.`created_at`, d.`created_by` FROM {delegations} d "+
|
||||
"JOIN {users} u ON u.`id` = d.`user_id` WHERE d.`zone` = ? ORDER BY u.`username` FOR UPDATE OF d"), zone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("delete zone delegations: %w", err)
|
||||
}
|
||||
var deleted []delegationRow
|
||||
for rows.Next() {
|
||||
var d delegationRow
|
||||
if err := rows.Scan(&d.Zone, &d.UserID, &d.Username, &d.CreatedAt, &d.CreatedBy); err != nil {
|
||||
rows.Close()
|
||||
return nil, fmt.Errorf("delete zone delegations: %w", err)
|
||||
}
|
||||
scanUTC(&d.CreatedAt)
|
||||
deleted = append(deleted, d)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return nil, fmt.Errorf("delete zone delegations: %w", err)
|
||||
}
|
||||
rows.Close()
|
||||
if len(deleted) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if _, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {delegations} WHERE `zone` = ?"), zone); err != nil {
|
||||
return nil, fmt.Errorf("delete zone delegations: %w", err)
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (s delegationStore) hasDelegation(ctx context.Context, query, zone string, userID int64) (bool, error) {
|
||||
var one int
|
||||
err := s.q.QueryRowContext(ctx, s.t.sql(query), zone, userID).Scan(&one)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("read delegation: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s delegationStore) HasDelegation(ctx context.Context, zone string, userID int64) (bool, error) {
|
||||
return s.hasDelegation(ctx, "SELECT 1 FROM {delegations} WHERE `zone` = ? AND `user_id` = ?", zone, userID)
|
||||
}
|
||||
|
||||
func (s delegationStore) HasDelegationForShare(ctx context.Context, zone string, userID int64) (bool, error) {
|
||||
return s.hasDelegation(ctx, "SELECT 1 FROM {delegations} WHERE `zone` = ? AND `user_id` = ? FOR SHARE", zone, userID)
|
||||
}
|
||||
Reference in new issue
Block a user