Conversation
- Created WithUser middleware that fetches user from DB and adds to context - Eliminated ~130 lines of duplicate user retrieval code across handlers - Updated 6 handlers: media.go (3 methods), mappings.go (3 methods), media_detail.go (1), play.go (1) - Integrated WithUser into auth middleware chain (WithUserID -> WithUser -> RequireAuth) - Updated all handler tests to chain both middlewares correctly - All tests passing, no lint issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a comprehensive refactoring to eliminate code duplication, improve maintainability, and enhance testability across the codebase. The changes eliminate ~315 lines of duplicated code while adding ~621 net lines (including extensive test coverage).
Key Changes
Code Duplication Elimination (~315 lines removed):
TryServerURLshelper (~75 lines)WithUsermiddleware to eliminate repeated user DB fetches (~130 lines)app.Initializer(~110 lines)New Components:
internal/helpers/plex.go- Generic helper for trying multiple server URLs with fallbackinternal/middleware/user.go- Middleware to fetch user once and add to contextinternal/app/initializer.go- Extracted initialization logic from main.goBug Fixes:
context.Background()usage in pairing handler with proper 30-second timeoutInfrastructure:
BeginTx,WithTransactionhelper)Files Changed (18 files)
internal/helpers/plex.go,internal/helpers/plex_test.go,internal/middleware/user.go,internal/middleware/user_test.go,internal/app/initializer.gomain.go,internal/db/db.go,internal/db/db_test.go, handlers (media.go, mappings.go, pairing.go, play.go, media_detail.go), handler testsCommits (8)
Test Plan
./checks.sh)internal/helpers/plex_test.go- TryServerURLs helper testsinternal/middleware/user_test.go- WithUser middleware testsinternal/db/db_test.go- Transaction tests (BeginTx, WithTransaction)go fmt)golangci-lint- 0 issues)go test -race)Impact