Refactor imports and clean up whitespace in API and runner client files. Update auth package references to use the new package structure. Improve mutex usage in the runner client for better concurrency handling.

This commit is contained in:
2025-11-22 05:45:13 -06:00
parent fb2e318eaa
commit 27a09aedd6
3 changed files with 31 additions and 34 deletions

View File

@@ -7,8 +7,6 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/go-chi/chi/v5"
"fuego/internal/auth"
"fuego/pkg/types" "fuego/pkg/types"
) )
@@ -39,9 +37,9 @@ func (s *Server) handleGenerateRegistrationToken(w http.ResponseWriter, r *http.
} }
s.respondJSON(w, http.StatusCreated, map[string]interface{}{ s.respondJSON(w, http.StatusCreated, map[string]interface{}{
"token": token, "token": token,
"expires_in": expiresIn.String(), "expires_in": expiresIn.String(),
"expires_at": time.Now().Add(expiresIn), "expires_at": time.Now().Add(expiresIn),
}) })
} }
@@ -169,4 +167,3 @@ func (s *Server) handleListRunnersAdmin(w http.ResponseWriter, r *http.Request)
s.respondJSON(w, http.StatusOK, runners) s.respondJSON(w, http.StatusOK, runners)
} }

View File

@@ -10,35 +10,36 @@ import (
"sync" "sync"
"time" "time"
authpkg "fuego/internal/auth"
"fuego/internal/database"
"fuego/internal/storage"
"fuego/pkg/types"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors" "github.com/go-chi/cors"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"fuego/internal/auth"
"fuego/internal/database"
"fuego/internal/storage"
"fuego/pkg/types"
) )
// Server represents the API server // Server represents the API server
type Server struct { type Server struct {
db *database.DB db *database.DB
auth *auth.Auth auth *authpkg.Auth
secrets *auth.Secrets secrets *authpkg.Secrets
storage *storage.Storage storage *storage.Storage
router *chi.Mux router *chi.Mux
// WebSocket connections // WebSocket connections
wsUpgrader websocket.Upgrader wsUpgrader websocket.Upgrader
runnerConns map[int64]*websocket.Conn runnerConns map[int64]*websocket.Conn
runnerConnsMu sync.RWMutex runnerConnsMu sync.RWMutex
frontendConns map[string]*websocket.Conn // key: "jobId:taskId" frontendConns map[string]*websocket.Conn // key: "jobId:taskId"
frontendConnsMu sync.RWMutex frontendConnsMu sync.RWMutex
} }
// NewServer creates a new API server // NewServer creates a new API server
func NewServer(db *database.DB, auth *auth.Auth, storage *storage.Storage) (*Server, error) { func NewServer(db *database.DB, auth *authpkg.Auth, storage *storage.Storage) (*Server, error) {
secrets, err := auth.NewSecrets(db.DB) secrets, err := authpkg.NewSecrets(db.DB)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize secrets: %w", err) return nil, fmt.Errorf("failed to initialize secrets: %w", err)
} }
@@ -290,7 +291,7 @@ func (s *Server) handleGetMe(w http.ResponseWriter, r *http.Request) {
// Helper to get user ID from context // Helper to get user ID from context
func getUserID(r *http.Request) (int64, error) { func getUserID(r *http.Request) (int64, error) {
userID, ok := auth.GetUserID(r.Context()) userID, ok := authpkg.GetUserID(r.Context())
if !ok { if !ok {
return 0, fmt.Errorf("user ID not found in context") return 0, fmt.Errorf("user ID not found in context")
} }
@@ -510,4 +511,3 @@ func (s *Server) recoverTaskTimeouts() {
} }
} }
} }

View File

@@ -20,8 +20,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/gorilla/websocket"
"fuego/pkg/types" "fuego/pkg/types"
"github.com/gorilla/websocket"
) )
// Client represents a runner client // Client represents a runner client
@@ -35,7 +36,7 @@ type Client struct {
runnerSecret string runnerSecret string
managerSecret string managerSecret string
wsConn *websocket.Conn wsConn *websocket.Conn
wsConnMu sync.Mutex wsConnMu sync.RWMutex
stopChan chan struct{} stopChan chan struct{}
} }
@@ -268,8 +269,8 @@ func (c *Client) handleTaskAssignment(msg map[string]interface{}) {
// Convert to task map format // Convert to task map format
taskMap := map[string]interface{}{ taskMap := map[string]interface{}{
"id": taskID, "id": taskID,
"job_id": jobID, "job_id": jobID,
"frame_start": frameStart, "frame_start": frameStart,
"frame_end": frameEnd, "frame_end": frameEnd,
} }
@@ -813,4 +814,3 @@ func (c *Client) sendTaskComplete(taskID int64, outputPath string, success bool,
} }
return fmt.Errorf("WebSocket not connected, cannot complete task") return fmt.Errorf("WebSocket not connected, cannot complete task")
} }