diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-01-23 21:37:18 -1000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-01-23 21:37:18 -1000 |
| commit | 465093343ddd398ce5f6377fc9c472d8251c618b (patch) | |
| tree | d333a2f1c8879f7b114817e929c95e9fcf5f4c3b /internal/handlers/response.go | |
| parent | e23c85577cbb0eac8b847dd989072698ff4e7a30 (diff) | |
Refactor: reduce code duplication with shared abstractions
- Add BaseClient HTTP abstraction (internal/api/http.go) to eliminate
duplicated HTTP boilerplate across Todoist, Trello, and PlanToEat clients
- Add response helpers (internal/handlers/response.go) for JSON/HTML responses
- Add generic cache wrapper (internal/handlers/cache.go) using Go generics
- Consolidate HandleCompleteAtom/HandleUncompleteAtom into handleAtomToggle
- Merge TabsHandler into Handler, delete tabs.go
- Extract sortTasksByUrgency and filterAndSortTrelloTasks helpers
- Update tests to work with new BaseClient structure
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/handlers/response.go')
| -rw-r--r-- | internal/handlers/response.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/internal/handlers/response.go b/internal/handlers/response.go new file mode 100644 index 0000000..3976f02 --- /dev/null +++ b/internal/handlers/response.go @@ -0,0 +1,36 @@ +package handlers + +import ( + "encoding/json" + "html/template" + "log" + "net/http" +) + +// JSONResponse writes data as JSON with appropriate headers +func JSONResponse(w http.ResponseWriter, data interface{}) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(data) +} + +// JSONError writes an error response as JSON +func JSONError(w http.ResponseWriter, status int, msg string, err error) { + if err != nil { + log.Printf("Error: %s: %v", msg, err) + } + http.Error(w, msg, status) +} + +// HTMLResponse renders an HTML template +func HTMLResponse(w http.ResponseWriter, tmpl *template.Template, name string, data interface{}) { + if err := tmpl.ExecuteTemplate(w, name, data); err != nil { + http.Error(w, "Failed to render template", http.StatusInternalServerError) + log.Printf("Error rendering template %s: %v", name, err) + } +} + +// HTMLString writes an HTML string directly +func HTMLString(w http.ResponseWriter, html string) { + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(html)) +} |
