use antigravity to rewrite all your code to migrate from flat files to relational database. YOLO
All checks were successful
pedestrian-simulator / build (push) Successful in 58s

This commit is contained in:
2026-01-11 20:24:50 -07:00
parent df1ee20994
commit 512b36b10e
10 changed files with 364 additions and 459 deletions

View File

@@ -2,12 +2,10 @@ package main
import (
"crypto/rand"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
)
@@ -54,32 +52,24 @@ func CreateSession(fitbitUserID string) (*Session, error) {
return session, nil
}
// SaveSession persists a session to disk
// SaveSession persists a session to the database
func SaveSession(session *Session) error {
sessionDir := "data/sessions"
if err := os.MkdirAll(sessionDir, 0755); err != nil {
return fmt.Errorf("failed to create sessions directory: %w", err)
}
sessionPath := filepath.Join(sessionDir, session.Token+".json")
data, err := json.MarshalIndent(session, "", " ")
if err != nil {
return err
}
return os.WriteFile(sessionPath, data, 0600)
_, err := db.Exec(`
INSERT INTO sessions (token, fitbit_user_id, created_at, expires_at)
VALUES (?, ?, ?, ?)
`, session.Token, session.FitbitUserID, session.CreatedAt, session.ExpiresAt)
return err
}
// LoadSession loads a session from disk by token
// LoadSession loads a session from the database by token
func LoadSession(token string) (*Session, error) {
sessionPath := filepath.Join("data/sessions", token+".json")
data, err := os.ReadFile(sessionPath)
if err != nil {
return nil, err
}
var session Session
if err := json.Unmarshal(data, &session); err != nil {
err := db.QueryRow("SELECT token, fitbit_user_id, created_at, expires_at FROM sessions WHERE token = ?", token).
Scan(&session.Token, &session.FitbitUserID, &session.CreatedAt, &session.ExpiresAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("session not found")
}
return nil, err
}
@@ -92,10 +82,10 @@ func LoadSession(token string) (*Session, error) {
return &session, nil
}
// DeleteSession removes a session from disk
// DeleteSession removes a session from the database
func DeleteSession(token string) error {
sessionPath := filepath.Join("data/sessions", token+".json")
return os.Remove(sessionPath)
_, err := db.Exec("DELETE FROM sessions WHERE token = ?", token)
return err
}
// GetSessionFromRequest extracts the session from the request cookie