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,104 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sessionRow struct {
|
||||
TokenHash []byte
|
||||
UserID int64
|
||||
CreatedAt time.Time
|
||||
LastSeenAt time.Time
|
||||
ExpiresAt time.Time
|
||||
Origin string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
type sessionWithUser struct {
|
||||
Session sessionRow
|
||||
User userRow
|
||||
}
|
||||
|
||||
type sessionStore struct {
|
||||
q dbtx
|
||||
t tableNames
|
||||
}
|
||||
|
||||
func newSessionStore(q dbtx, t tableNames) sessionStore {
|
||||
return sessionStore{q: q, t: t}
|
||||
}
|
||||
|
||||
func (s sessionStore) CreateSession(ctx context.Context, row sessionRow) error {
|
||||
_, err := s.q.ExecContext(ctx,
|
||||
s.t.sql("INSERT INTO {sessions} (`token_hash`, `user_id`, `created_at`, `last_seen_at`, `expires_at`, `origin`, `user_agent`) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
row.TokenHash, row.UserID, dbTime(row.CreatedAt), dbTime(row.LastSeenAt), dbTime(row.ExpiresAt), row.Origin, row.UserAgent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s sessionStore) SessionByTokenHash(ctx context.Context, tokenHash []byte) (sessionWithUser, error) {
|
||||
var r sessionWithUser
|
||||
u, err := scanUser(s.q.QueryRowContext(ctx,
|
||||
s.t.sql("SELECT "+userColumns+", x.`token_hash`, x.`user_id`, x.`created_at`, x.`last_seen_at`, x.`expires_at`, x.`origin`, x.`user_agent` "+
|
||||
"FROM {sessions} x JOIN {users} u ON u.`id` = x.`user_id` WHERE x.`token_hash` = ?"), tokenHash),
|
||||
&r.Session.TokenHash, &r.Session.UserID, &r.Session.CreatedAt, &r.Session.LastSeenAt, &r.Session.ExpiresAt, &r.Session.Origin, &r.Session.UserAgent)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return sessionWithUser{}, errRowNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return sessionWithUser{}, fmt.Errorf("read session: %w", err)
|
||||
}
|
||||
r.User = u
|
||||
scanUTC(&r.Session.CreatedAt)
|
||||
scanUTC(&r.Session.LastSeenAt)
|
||||
scanUTC(&r.Session.ExpiresAt)
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (s sessionStore) TouchSession(ctx context.Context, tokenHash []byte, now time.Time) error {
|
||||
if _, err := s.q.ExecContext(ctx, s.t.sql("UPDATE {sessions} SET `last_seen_at` = ? WHERE `token_hash` = ?"), dbTime(now), tokenHash); err != nil {
|
||||
return fmt.Errorf("touch session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s sessionStore) deleted(ctx context.Context, what, query string, args ...any) (int64, error) {
|
||||
res, err := s.q.ExecContext(ctx, s.t.sql(query), args...)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s: %w", what, err)
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s: %w", what, err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (s sessionStore) DeleteSession(ctx context.Context, tokenHash []byte) (bool, error) {
|
||||
n, err := s.deleted(ctx, "delete session", "DELETE FROM {sessions} WHERE `token_hash` = ?", tokenHash)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (s sessionStore) DeleteUserSessions(ctx context.Context, userID int64) (int64, error) {
|
||||
return s.deleted(ctx, "delete user sessions", "DELETE FROM {sessions} WHERE `user_id` = ?", userID)
|
||||
}
|
||||
|
||||
func (s sessionStore) DeleteUserSessionsExcept(ctx context.Context, userID int64, keepTokenHash []byte) (int64, error) {
|
||||
return s.deleted(ctx, "delete user sessions",
|
||||
"DELETE FROM {sessions} WHERE `user_id` = ? AND `token_hash` <> ?", userID, keepTokenHash)
|
||||
}
|
||||
|
||||
func (s sessionStore) DeleteExpiredSessions(ctx context.Context, now time.Time, idleTimeout time.Duration) (int64, error) {
|
||||
now = dbTime(now)
|
||||
return s.deleted(ctx, "delete expired sessions",
|
||||
"DELETE FROM {sessions} WHERE `expires_at` <= ? OR `last_seen_at` <= ?", now, now.Add(-idleTimeout))
|
||||
}
|
||||
Reference in new issue
Block a user