diff --git a/internal/handlers/commonhandlers.go b/internal/handlers/commonhandlers.go new file mode 100644 index 0000000..3da4fb4 --- /dev/null +++ b/internal/handlers/commonhandlers.go @@ -0,0 +1,71 @@ +package handlers + +import ( + "bookhoard/internal/database" + "bookhoard/internal/services" + wsync "bookhoard/internal/sync" + "context" + "sync" + "time" + + "github.com/labstack/echo/v4" +) + +type Handler struct { + db *database.Queries + scanner *services.MediaScanner + worker *services.Worker + scheduler *services.Scheduler + queueProcessor *wsync.SyncQueueProcessor + queueCtx context.Context + queueCancel context.CancelFunc + ctx context.Context + cancel context.CancelFunc + mu sync.Mutex + watchModeCtx context.Context + watchModeCancel context.CancelFunc + watchingLibraries map[string]bool + connManager *wsync.ConnectionManager + cleanupTaskCancel context.CancelFunc +} + +func NewHandler(db *database.Queries, connManager *wsync.ConnectionManager, queueProcessor *wsync.SyncQueueProcessor) *Handler { + ctx, cancel := context.WithCancel(context.Background()) + worker := services.NewWorker(3) + scheduler := services.NewScheduler(worker, db) + + queueCtx, queueCancel := context.WithCancel(context.Background()) + + watchCtx, watchCancel := context.WithCancel(context.Background()) + + return &Handler{ + db: db, + scanner: services.NewMediaScanner(db), + worker: worker, + scheduler: scheduler, + queueProcessor: queueProcessor, + queueCtx: queueCtx, + queueCancel: queueCancel, + ctx: ctx, + cancel: cancel, + watchModeCtx: watchCtx, + watchModeCancel: watchCancel, + watchingLibraries: make(map[string]bool), + connManager: connManager, + } +} + +// parseDate parses a date string in YYYY-MM-DD format +func parseDate(dateStr string) time.Time { + if dateStr == "" { + return time.Time{} + } + if t, err := time.Parse("2006-01-02", dateStr); err == nil { + return t + } + return time.Time{} +} + +func SetupRoutes(g *echo.Group, db *database.Queries, connManager *wsync.ConnectionManager, queueProcessor *wsync.SyncQueueProcessor) *Handler { + return NewHandler(db, connManager, queueProcessor) +} diff --git a/internal/handlers/scanner.go b/internal/handlers/scanner.go index c7f2c7b..60f92d0 100644 --- a/internal/handlers/scanner.go +++ b/internal/handlers/scanner.go @@ -1,14 +1,10 @@ package handlers import ( - "bookhoard/internal/database" "bookhoard/internal/services" - wsync "bookhoard/internal/sync" "context" "fmt" "net/http" - "sync" - "time" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" @@ -19,50 +15,6 @@ const ( maxPaginationLimit = 1000 ) -type Handler struct { - db *database.Queries - scanner *services.MediaScanner - worker *services.Worker - scheduler *services.Scheduler - queueProcessor *wsync.SyncQueueProcessor - queueCtx context.Context - queueCancel context.CancelFunc - ctx context.Context - cancel context.CancelFunc - mu sync.Mutex - watchModeCtx context.Context - watchModeCancel context.CancelFunc - watchingLibraries map[string]bool - connManager *wsync.ConnectionManager - cleanupTaskCancel context.CancelFunc -} - -func NewHandler(db *database.Queries, connManager *wsync.ConnectionManager, queueProcessor *wsync.SyncQueueProcessor) *Handler { - ctx, cancel := context.WithCancel(context.Background()) - worker := services.NewWorker(3) - scheduler := services.NewScheduler(worker, db) - - queueCtx, queueCancel := context.WithCancel(context.Background()) - - watchCtx, watchCancel := context.WithCancel(context.Background()) - - return &Handler{ - db: db, - scanner: services.NewMediaScanner(db), - worker: worker, - scheduler: scheduler, - queueProcessor: queueProcessor, - queueCtx: queueCtx, - queueCancel: queueCancel, - ctx: ctx, - cancel: cancel, - watchModeCtx: watchCtx, - watchModeCancel: watchCancel, - watchingLibraries: make(map[string]bool), - connManager: connManager, - } -} - // StartBackgroundTasks starts the queue processor and cleanup task // This should be called once for the main handler instance func (h *Handler) StartBackgroundTasks() { @@ -70,21 +22,6 @@ func (h *Handler) StartBackgroundTasks() { go h.queueProcessor.Start(h.queueCtx) } -// parseDate parses a date string in YYYY-MM-DD format -func parseDate(dateStr string) time.Time { - if dateStr == "" { - return time.Time{} - } - if t, err := time.Parse("2006-01-02", dateStr); err == nil { - return t - } - return time.Time{} -} - -func SetupRoutes(g *echo.Group, db *database.Queries, connManager *wsync.ConnectionManager, queueProcessor *wsync.SyncQueueProcessor) *Handler { - return NewHandler(db, connManager, queueProcessor) -} - // ScanLibraryRequest represents the request for scanning a library type ScanLibraryRequest struct { FolderPaths []string `json:"folder_paths,omitempty"`