refactor: extract DB mixins, middleware handlers, and lifecycle hooks#2
Merged
Merged
Conversation
Three structural splits to keep the boilerplate readable as it grows — no behavior change (all endpoints, headers, and error envelopes are byte-identical). - database/mixins.py: split TimestampedBase into composable UUIDPrimaryKeyMixin, TimestampMixin, SoftDeleteMixin; TimestampedBase now composes all three. base.py keeps only Base + GUID + UTC helpers. Models can pick mixins a la carte. - middleware/cors.py + middleware/error_handler.py: move the inline CORS setup and exception handlers out of main.py behind configure_cors(app) and register_exception_handlers(app). - core/events.py: extract the ASGI lifespan into named _startup/_shutdown hooks, now wrapped in try/finally so shutdown runs on teardown-after-error. Shaped for Redis/queues/warm-up to slot in as fail-open steps later. main.py is now a thin assembly file. README updated to match. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What & why
Three structural splits to keep the boilerplate readable as it grows. No behavior change — all endpoints, security headers, and error envelopes are byte-identical (verified by the existing suite + a runtime smoke test).
1. Composable DB mixins (
database/mixins.py)TimestampedBasebundled UUID PK + timestamps + soft delete into one abstract base. Split into three independently-usable mixins —UUIDPrimaryKeyMixin,TimestampMixin,SoftDeleteMixin— withTimestampedBasenow composing all three.base.pykeeps only the primitives (Base,GUID, UTC helpers). Future models can pick columns à la carte:2. CORS + exception handlers out of main.py
3. Lifecycle hooks (core/events.py)
Extracted the inline ASGI lifespan into named _startup / _shutdown hooks, now wrapped in try/finally so shutdown (engine dispose) runs even on teardown-after-error. Documented convention for slotting in Redis / queues / model warm-up later as fail-open steps.
Verification