Refactor into modular architecture #32
Merged
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.
The previous
register_model_routes_internal()function had grown to over 600 lines, combining sync/async routing. This made the codebase hard to maintain, extend, and test.This PR addresses that by extracting each responsibility into its own module and introducing a clear router class hierarchy. The new structure improves separation of concerns, testability, extensibility, and overall readability.
Key Changes
Router Refactor:
Introduced a
BaseModelRouterclass in router/base.py for shared configuration and route wiring.Added
AsyncModelRouterandSyncModelRoutersubclasses inrouter/async_router.pyandrouter/sync_router.py, each handling their respective route registration logic.Handlers & Utilities:
Moved response serialization logic to handlers/response.py.
Centralized file upload extraction and relation logic in handlers/file_handler.py.
Moved foreign key conversion and related utilities to utils/model.py.
Moved hook execution logic to utils/hooks.py.
Kept file_upload.py and pagination.py focused and minimal.
Benefits
Separation of Concerns:
Each module now handles a single responsibility (routing, file handling, hooks, etc.), making the codebase easier to understand and maintain.
Testability:
Smaller, focused functions and classes are easier to unit-test and debug.
Extensibility:
Adding new features (e.g., custom pagination strategies, new hook types) is now straightforward and does not require editing a monolithic function.
Readability & Onboarding:
New contributors can easily find code by topic, rather than scrolling through a massive function.
Migration Notes
No breaking changes to the public API.
All existing features (sync/async routing, file uploads, hooks, pagination, filtering, FK conversion) are preserved and now easier to extend.
This refactor lays the foundation for a more maintainable and scalable Lazy Ninja. Feedback and suggestions are welcome!
closes #29