Spaces:
Runtime error
Runtime error
| package db | |
| import "time" | |
| // APIKeyRepository defines operations for API key management | |
| type APIKeyRepository interface { | |
| CreateKey(hash, name string, expiresAt *time.Time) error | |
| GetActiveKeyHash() (string, error) | |
| DeactivateKey(id int) error | |
| } | |
| // SQLiteAPIKeyRepository implements APIKeyRepository for SQLite | |
| type SQLiteAPIKeyRepository struct { | |
| db Database | |
| } | |
| // NewSQLiteAPIKeyRepository creates a new SQLite API key repository | |
| func NewSQLiteAPIKeyRepository(db Database) *SQLiteAPIKeyRepository { | |
| return &SQLiteAPIKeyRepository{db: db} | |
| } | |
| func (r *SQLiteAPIKeyRepository) CreateKey(hash, name string, expiresAt *time.Time) error { | |
| _, err := r.db.Exec(` | |
| INSERT INTO api_keys (key_hash, name, expires_at) | |
| VALUES (?, ?, ?) | |
| `, hash, name, expiresAt) | |
| return err | |
| } | |
| func (r *SQLiteAPIKeyRepository) GetActiveKeyHash() (string, error) { | |
| var hash string | |
| err := r.db.QueryRow(` | |
| SELECT key_hash FROM api_keys | |
| WHERE is_active = TRUE | |
| AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP) | |
| `).Scan(&hash) | |
| return hash, err | |
| } | |
| func (r *SQLiteAPIKeyRepository) DeactivateKey(id int) error { | |
| _, err := r.db.Exec(` | |
| UPDATE api_keys | |
| SET is_active = FALSE | |
| WHERE id = ? | |
| `, id) | |
| return err | |
| } | |
| // PeerRepository defines operations for peer management | |
| type PeerRepository interface { | |
| AddPeer(url, publicKeyPEM string) error | |
| ListTrustedPeers() ([]struct { | |
| URL string | |
| PublicKey string | |
| }, error) | |
| } | |
| // SQLitePeerRepository implements PeerRepository for SQLite | |
| type SQLitePeerRepository struct { | |
| db Database | |
| } | |
| // NewSQLitePeerRepository creates a new SQLite peer repository | |
| func NewSQLitePeerRepository(db Database) *SQLitePeerRepository { | |
| return &SQLitePeerRepository{db: db} | |
| } | |
| func (r *SQLitePeerRepository) AddPeer(url, publicKeyPEM string) error { | |
| _, err := r.db.Exec(` | |
| INSERT INTO peers (url, public_key_pem) | |
| VALUES (?, ?) | |
| `, url, publicKeyPEM) | |
| return err | |
| } | |
| func (r *SQLitePeerRepository) ListTrustedPeers() ([]struct { | |
| URL string | |
| PublicKey string | |
| }, error) { | |
| rows, err := r.db.Query(` | |
| SELECT url, public_key_pem FROM peers | |
| `) | |
| if err != nil { | |
| return nil, err | |
| } | |
| defer rows.Close() | |
| var peers []struct { | |
| URL string | |
| PublicKey string | |
| } | |
| for rows.Next() { | |
| var peer struct { | |
| URL string | |
| PublicKey string | |
| } | |
| if err := rows.Scan(&peer.URL, &peer.PublicKey); err != nil { | |
| return nil, err | |
| } | |
| peers = append(peers, peer) | |
| } | |
| return peers, nil | |
| } | |
| // CreateTables creates the necessary database tables | |
| func CreateTables(db Database) error { | |
| _, err := db.Exec(` | |
| CREATE TABLE IF NOT EXISTS api_keys ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| key_hash TEXT NOT NULL UNIQUE, | |
| name TEXT NOT NULL, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| expires_at TIMESTAMP, | |
| is_active BOOLEAN DEFAULT TRUE | |
| ); | |
| CREATE TABLE IF NOT EXISTS peers ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| url TEXT NOT NULL UNIQUE, | |
| public_key_pem TEXT NOT NULL, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | |
| ); | |
| `) | |
| return err | |
| } | |