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
All checks were successful
pedestrian-simulator / build (push) Successful in 58s
This commit is contained in:
@@ -6,37 +6,14 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var (
|
||||
stepManagers = make(map[string]*StepManager) // userID -> StepManager
|
||||
smMutex sync.RWMutex
|
||||
)
|
||||
|
||||
// getOrCreateStepManager retrieves or creates a StepManager for the given user
|
||||
func getOrCreateStepManager(userID string) *StepManager {
|
||||
smMutex.RLock()
|
||||
sm, exists := stepManagers[userID]
|
||||
smMutex.RUnlock()
|
||||
|
||||
if exists {
|
||||
return sm
|
||||
}
|
||||
|
||||
// Create new StepManager for this user
|
||||
smMutex.Lock()
|
||||
defer smMutex.Unlock()
|
||||
|
||||
// Double-check it wasn't created while we were waiting for the lock
|
||||
if sm, exists := stepManagers[userID]; exists {
|
||||
return sm
|
||||
}
|
||||
|
||||
sm = NewStepManager(userID)
|
||||
stepManagers[userID] = sm
|
||||
return sm
|
||||
// getStepManager creates a new StepManager for the given user, loading state from DB
|
||||
func getStepManager(userID string) *StepManager {
|
||||
return NewStepManager(userID)
|
||||
}
|
||||
|
||||
func initTimezone() {
|
||||
@@ -58,9 +35,8 @@ func initTimezone() {
|
||||
func main() {
|
||||
// Initialize components
|
||||
initTimezone()
|
||||
InitDB()
|
||||
InitFitbit()
|
||||
InitUserRegistry()
|
||||
InitVoteRegistry()
|
||||
|
||||
// 1. Serve Static Files (Frontend)
|
||||
fs := http.FileServer(http.Dir("frontend"))
|
||||
@@ -69,19 +45,19 @@ func main() {
|
||||
// 2. API Endpoints (all require authentication)
|
||||
http.HandleFunc("/api/status", RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := getUserID(r.Context())
|
||||
sm := getOrCreateStepManager(userID)
|
||||
sm := getStepManager(userID)
|
||||
|
||||
status := sm.GetStatus()
|
||||
|
||||
// Add user info to status
|
||||
user, exists := userRegistry.GetUser(userID)
|
||||
if exists && user != nil {
|
||||
user, err := GetUser(userID)
|
||||
if err == nil && user != nil {
|
||||
status["user"] = map[string]string{
|
||||
"displayName": user.DisplayName,
|
||||
"avatarUrl": user.AvatarURL,
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[API Status] WARNING: User info not found for ID: %s (exists=%v)\n", userID, exists)
|
||||
fmt.Printf("[API Status] WARNING: User info not found for ID: %s (err=%v)\n", userID, err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -95,7 +71,7 @@ func main() {
|
||||
}
|
||||
|
||||
userID, _ := getUserID(r.Context())
|
||||
sm := getOrCreateStepManager(userID)
|
||||
sm := getStepManager(userID)
|
||||
sm.Sync()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -107,7 +83,7 @@ func main() {
|
||||
}
|
||||
|
||||
userID, _ := getUserID(r.Context())
|
||||
sm := getOrCreateStepManager(userID)
|
||||
sm := getStepManager(userID)
|
||||
sm.StartNewTrip()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -119,7 +95,7 @@ func main() {
|
||||
}
|
||||
|
||||
userID, _ := getUserID(r.Context())
|
||||
sm := getOrCreateStepManager(userID)
|
||||
sm := getStepManager(userID)
|
||||
go sm.Drain() // Async so we don't block
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user