From a38abc90ee4fe00bf0fbdf897c5ef93e80e0c256 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Mon, 19 Jan 2026 09:54:42 -1000 Subject: Refactor: Add cache key constants and configurable template path Code quality improvements: - Define CacheKey* constants in store package for type safety - Add TemplateDir to config (default: web/templates, env: TEMPLATE_DIR) - Update handlers to use store.CacheKey* instead of hardcoded strings - Update NewTabsHandler to accept templateDir parameter - Use filepath.Join for cross-platform template path construction Co-Authored-By: Claude Opus 4.5 --- internal/handlers/handlers.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'internal/handlers/handlers.go') diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 20095fe..0af2bba 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -7,6 +7,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" "sort" "strings" "sync" @@ -30,21 +31,21 @@ type Handler struct { } // New creates a new Handler instance -func New(store *store.Store, todoist api.TodoistAPI, trello api.TrelloAPI, obsidian api.ObsidianAPI, planToEat api.PlanToEatAPI, cfg *config.Config) *Handler { +func New(s *store.Store, todoist api.TodoistAPI, trello api.TrelloAPI, obsidian api.ObsidianAPI, planToEat api.PlanToEatAPI, cfg *config.Config) *Handler { // Parse templates including partials - tmpl, err := template.ParseGlob("web/templates/*.html") + tmpl, err := template.ParseGlob(filepath.Join(cfg.TemplateDir, "*.html")) if err != nil { log.Printf("Warning: failed to parse templates: %v", err) } // Also parse partials - tmpl, err = tmpl.ParseGlob("web/templates/partials/*.html") + tmpl, err = tmpl.ParseGlob(filepath.Join(cfg.TemplateDir, "partials", "*.html")) if err != nil { log.Printf("Warning: failed to parse partial templates: %v", err) } return &Handler{ - store: store, + store: s, todoistClient: todoist, trelloClient: trello, obsidianClient: obsidian, @@ -351,7 +352,7 @@ func (h *Handler) aggregateData(ctx context.Context, forceRefresh bool) (*models // fetchTasks fetches tasks from cache or API func (h *Handler) fetchTasks(ctx context.Context, forceRefresh bool) ([]models.Task, error) { - cacheKey := "todoist_tasks" + cacheKey := store.CacheKeyTodoistTasks // Check cache validity if !forceRefresh { @@ -387,7 +388,7 @@ func (h *Handler) fetchTasks(ctx context.Context, forceRefresh bool) ([]models.T // fetchNotes fetches notes from cache or filesystem func (h *Handler) fetchNotes(ctx context.Context, forceRefresh bool) ([]models.Note, error) { - cacheKey := "obsidian_notes" + cacheKey := store.CacheKeyObsidianNotes // Check cache validity if !forceRefresh { @@ -423,7 +424,7 @@ func (h *Handler) fetchNotes(ctx context.Context, forceRefresh bool) ([]models.N // fetchMeals fetches meals from cache or API func (h *Handler) fetchMeals(ctx context.Context, forceRefresh bool) ([]models.Meal, error) { - cacheKey := "plantoeat_meals" + cacheKey := store.CacheKeyPlanToEatMeals // Check cache validity if !forceRefresh { @@ -463,7 +464,7 @@ func (h *Handler) fetchMeals(ctx context.Context, forceRefresh bool) ([]models.M // fetchBoards fetches Trello boards from cache or API func (h *Handler) fetchBoards(ctx context.Context, forceRefresh bool) ([]models.Board, error) { - cacheKey := "trello_boards" + cacheKey := store.CacheKeyTrelloBoards // Check cache validity if !forceRefresh { @@ -765,7 +766,7 @@ func (h *Handler) HandleUnifiedAdd(w http.ResponseWriter, r *http.Request) { return } // Invalidate cache so fresh data is fetched - h.store.InvalidateCache("todoist_tasks") + h.store.InvalidateCache(store.CacheKeyTodoistTasks) case "trello": listID := r.FormValue("list_id") @@ -780,7 +781,7 @@ func (h *Handler) HandleUnifiedAdd(w http.ResponseWriter, r *http.Request) { return } // Invalidate cache so fresh data is fetched - h.store.InvalidateCache("trello_boards") + h.store.InvalidateCache(store.CacheKeyTrelloBoards) default: http.Error(w, "Invalid source", http.StatusBadRequest) -- cgit v1.2.3