Five complementary improvements to handler layer:
1. Fix Pre-Commit Hook
- Remove broken Perl-style regex (unsupported by Go)
- Use -short flag to exclude integration tests
- Tests now run successfully in pre-commit
2. Extract Duplicate Logic
- Remove 100+ lines of duplicate data preparation
- Both Home() and CVContent() now use prepareTemplateData()
- Reduce cv_pages.go from 290 to 120 lines (58% reduction)
3. Request/Response Types
- Create internal/handlers/types.go with structured types
- PDFExportRequest, LanguageRequest, PreferenceToggleRequest
- Type-safe parameter parsing with centralized validation
- Refactor ExportPDF to use typed requests
4. Middleware Extraction
- Create internal/middleware/preferences.go
- PreferencesMiddleware reads cookies once, stores in context
- Automatic migration of old preference values
- Ready for integration in routes
5. Handler Tests
- Add internal/handlers/cv_pages_test.go (190 lines, 15+ cases)
- Add internal/handlers/cv_htmx_test.go (325 lines, 20+ cases)
- Test language validation, toggles, cookies, methods
- Increase handler test coverage significantly
Testing:
- All unit tests pass (35+ new test cases)
- Pre-commit hook working
- Build succeeds
- No breaking changes
Benefits:
- Type safety: Compile-time parameter validation
- Code quality: 170 lines of duplication eliminated
- Testing: 100% increase in test files
- Architecture: Clean middleware pattern
- Developer experience: Self-documenting request types
Documentation:
- Create _go-learning/refactorings/004-handler-improvements.md
- Document all five improvements with examples
- Include metrics, testing strategy, and future improvements
Part 1: Shared Utilities
- Create internal/fileutil package with FindDataFile() and LoadJSON()
- Create internal/lang package with language constants and validation
- Eliminate 46 lines of code duplication between cv/loader.go and ui/loader.go
- Simplify cv/loader.go from 69 to 36 lines (-48%)
- Simplify ui/loader.go from 56 to 24 lines (-57%)
Part 2: Validation Layer
- Add comprehensive validation in internal/models/cv/validation.go
- Validate Personal (name, email format, URLs)
- Validate Experience (required fields, dates)
- Validate Education (required fields)
- Validate Skills (proficiency ranges 1-5, categories)
- Validate Languages (proficiency levels 1-5)
- Validate Projects (title, URLs)
- Validate Meta (version, language)
- Integrate validation into LoadCV() - automatic on load
- Create ValidationError and ValidationErrors types for clear error reporting
- Report all validation errors at once (better UX)
Testing:
- Add comprehensive tests for fileutil package (FindDataFile, LoadJSON)
- Add tests for lang package (IsValid, Validate, All)
- Add 280+ validation test cases covering edge cases
- All tests pass with real CV data (cv-en.json, cv-es.json)
- Fixed validation to allow both URLs and local paths for gitRepoUrl
Documentation:
- Create _go-learning/refactorings/002-shared-utilities-validation.md
- Document architecture, benefits, testing, and interview talking points
- Explain WHY decisions were made (DRY, type safety, data integrity)
Benefits:
- DRY: Single source of truth for utilities
- Type safety: Language constants instead of magic strings
- Data integrity: Validation catches errors at load time
- Better errors: Clear messages showing all issues at once
- Maintainability: Centralized utilities easier to update