Refactor handlers package to separate common handler logic

Extract Handler struct, constructor, and shared utilities from scanner.go
into a new commonhandlers.go file for better code organization.

Changes:
- Move Handler struct definition to commonhandlers.go
- Move NewHandler constructor to commonhandlers.go
- Move SetupRoutes function to commonhandlers.go
- Move parseDate utility function to commonhandlers.go
- Remove unused imports from scanner.go
- Create dedicated commonhandlers.go for shared HTTP handler code

This refactoring improves code maintainability by separating
concerns between scanner-specific logic and common handler utilities,
making it easier to understand and extend the handlers package.
This commit is contained in:
2026-02-27 10:32:16 -05:00
parent 7885d22be4
commit 501c898e58
2 changed files with 71 additions and 63 deletions
+71
View File
@@ -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)
}
-63
View File
@@ -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"`