refactor: remove from_globals() and construct AppContext explicitly (#656 PR F) - #671
Conversation
…656 PR F) ## Context Issue #656 tracks cleanup tasks after phase-5 DI migration (#639). This PR implements **PR F**: remove the `from_globals()` bridge and construct `AppContext` explicitly at bootstrap. ## Problem `AppContext::from_globals()` was a transitional bridge between the old global-based architecture and the new DI pattern. Now that all handlers and scheduler use `AppContext`, we can eliminate this bridge. ## Solution Construct `AppContext` explicitly in `main.rs` using values already available at that point: - `get_db_pool()` — database pool - `client` — Nostr client - `MOSTRO_CONFIG` — settings - `MESSAGE_QUEUES.queue_order_msg` — message queue - `mostro_keys` — signing keys ## Changes ### main.rs - Construct `AppContext::new()` explicitly with all dependencies - Pass `ctx` to `run()` instead of `my_keys` and `client` - Import `MESSAGE_QUEUES` and `MOSTRO_CONFIG` ### app.rs - `run()` signature changed: `(my_keys, client, ln_client)` → `(ctx, ln_client)` - Extract `my_keys`, `client`, and `pow` from `ctx` at function start - Remove `AppContext::from_globals()` call from event loop - Remove unused `Settings` import ### context.rs - **Removed `from_globals()` method entirely** - Removed unused `MESSAGE_QUEUES` import ## Breaking Changes `run()` function signature changed: - **Before:** `run(my_keys: Keys, client: &Client, ln_client: &mut LndConnector)` - **After:** `run(ctx: AppContext, ln_client: &mut LndConnector)` ## Benefits 1. **No more bridge code** — cleaner architecture 2. **Single construction point** — `AppContext` built once in main 3. **Explicit dependencies** — all deps visible at construction 4. **Testability** — easier to mock in tests ## Diff Stats - 3 files changed - +26 / -61 lines - **Net: -35 lines** 🧹 ## Validation ✅ `cargo fmt` ✅ `cargo clippy --all-targets --all-features -- -D warnings` ✅ `cargo test --bin mostrod` (189 passed, 0 failed) ## DI Migration Complete! 🎉 With this PR, the dependency injection migration from #639 is **complete**: | PR | Description | Status | |----|-------------|--------| | PR A | Remove legacy wrappers | ✅ #663 | | PR B | Simplify dispatcher | ✅ #665 | | PR C | Remove global accesses | ✅ #666 | | PR D | Migrate scheduler | ✅ #667 | | PR E | Add keys to AppContext | ✅ #670 | | **PR F** | Remove from_globals() | ✅ This PR | ## Related - Parent cleanup issue: #656 - Suggested by @codaMW in #667 review
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
💤 Files with no reviewable changes (1)
WalkthroughThis PR refactors the dependency injection pattern by consolidating scattered function parameters into a single AppContext argument. It removes the global-based Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…656 PR F) (#671) ## Context Issue #656 tracks cleanup tasks after phase-5 DI migration (#639). This PR implements **PR F**: remove the `from_globals()` bridge and construct `AppContext` explicitly at bootstrap. ## Problem `AppContext::from_globals()` was a transitional bridge between the old global-based architecture and the new DI pattern. Now that all handlers and scheduler use `AppContext`, we can eliminate this bridge. ## Solution Construct `AppContext` explicitly in `main.rs` using values already available at that point: - `get_db_pool()` — database pool - `client` — Nostr client - `MOSTRO_CONFIG` — settings - `MESSAGE_QUEUES.queue_order_msg` — message queue - `mostro_keys` — signing keys ## Changes ### main.rs - Construct `AppContext::new()` explicitly with all dependencies - Pass `ctx` to `run()` instead of `my_keys` and `client` - Import `MESSAGE_QUEUES` and `MOSTRO_CONFIG` ### app.rs - `run()` signature changed: `(my_keys, client, ln_client)` → `(ctx, ln_client)` - Extract `my_keys`, `client`, and `pow` from `ctx` at function start - Remove `AppContext::from_globals()` call from event loop - Remove unused `Settings` import ### context.rs - **Removed `from_globals()` method entirely** - Removed unused `MESSAGE_QUEUES` import ## Breaking Changes `run()` function signature changed: - **Before:** `run(my_keys: Keys, client: &Client, ln_client: &mut LndConnector)` - **After:** `run(ctx: AppContext, ln_client: &mut LndConnector)` ## Benefits 1. **No more bridge code** — cleaner architecture 2. **Single construction point** — `AppContext` built once in main 3. **Explicit dependencies** — all deps visible at construction 4. **Testability** — easier to mock in tests ## Diff Stats - 3 files changed - +26 / -61 lines - **Net: -35 lines** 🧹 ## Validation ✅ `cargo fmt` ✅ `cargo clippy --all-targets --all-features -- -D warnings` ✅ `cargo test --bin mostrod` (189 passed, 0 failed) ## DI Migration Complete! 🎉 With this PR, the dependency injection migration from #639 is **complete**: | PR | Description | Status | |----|-------------|--------| | PR A | Remove legacy wrappers | ✅ #663 | | PR B | Simplify dispatcher | ✅ #665 | | PR C | Remove global accesses | ✅ #666 | | PR D | Migrate scheduler | ✅ #667 | | PR E | Add keys to AppContext | ✅ #670 | | **PR F** | Remove from_globals() | ✅ This PR | ## Related - Parent cleanup issue: #656 - Suggested by @codaMW in #667 review Co-authored-by: MostronatorCoder[bot] <182182091+MostronatorCoder[bot]@users.noreply.github.com>
…656 PR F) (#671) (#672) ## Context Issue #656 tracks cleanup tasks after phase-5 DI migration (#639). This PR implements **PR F**: remove the `from_globals()` bridge and construct `AppContext` explicitly at bootstrap. ## Problem `AppContext::from_globals()` was a transitional bridge between the old global-based architecture and the new DI pattern. Now that all handlers and scheduler use `AppContext`, we can eliminate this bridge. ## Solution Construct `AppContext` explicitly in `main.rs` using values already available at that point: - `get_db_pool()` — database pool - `client` — Nostr client - `MOSTRO_CONFIG` — settings - `MESSAGE_QUEUES.queue_order_msg` — message queue - `mostro_keys` — signing keys ## Changes ### main.rs - Construct `AppContext::new()` explicitly with all dependencies - Pass `ctx` to `run()` instead of `my_keys` and `client` - Import `MESSAGE_QUEUES` and `MOSTRO_CONFIG` ### app.rs - `run()` signature changed: `(my_keys, client, ln_client)` → `(ctx, ln_client)` - Extract `my_keys`, `client`, and `pow` from `ctx` at function start - Remove `AppContext::from_globals()` call from event loop - Remove unused `Settings` import ### context.rs - **Removed `from_globals()` method entirely** - Removed unused `MESSAGE_QUEUES` import ## Breaking Changes `run()` function signature changed: - **Before:** `run(my_keys: Keys, client: &Client, ln_client: &mut LndConnector)` - **After:** `run(ctx: AppContext, ln_client: &mut LndConnector)` ## Benefits 1. **No more bridge code** — cleaner architecture 2. **Single construction point** — `AppContext` built once in main 3. **Explicit dependencies** — all deps visible at construction 4. **Testability** — easier to mock in tests ## Diff Stats - 3 files changed - +26 / -61 lines - **Net: -35 lines** 🧹 ## Validation ✅ `cargo fmt` ✅ `cargo clippy --all-targets --all-features -- -D warnings` ✅ `cargo test --bin mostrod` (189 passed, 0 failed) ## DI Migration Complete! 🎉 With this PR, the dependency injection migration from #639 is **complete**: | PR | Description | Status | |----|-------------|--------| | PR A | Remove legacy wrappers | ✅ #663 | | PR B | Simplify dispatcher | ✅ #665 | | PR C | Remove global accesses | ✅ #666 | | PR D | Migrate scheduler | ✅ #667 | | PR E | Add keys to AppContext | ✅ #670 | | **PR F** | Remove from_globals() | ✅ This PR | ## Related - Parent cleanup issue: #656 - Suggested by @codaMW in #667 review Co-authored-by: mostronatorcoder[bot] <263173566+mostronatorcoder[bot]@users.noreply.github.com> Co-authored-by: MostronatorCoder[bot] <182182091+MostronatorCoder[bot]@users.noreply.github.com>
Context
Issue #656 tracks cleanup tasks after phase-5 DI migration (#639).
This PR implements PR F: remove the
from_globals()bridge and constructAppContextexplicitly at bootstrap.Problem
AppContext::from_globals()was a transitional bridge between the old global-based architecture and the new DI pattern. Now that all handlers and scheduler useAppContext, we can eliminate this bridge.Solution
Construct
AppContextexplicitly inmain.rsusing values already available:get_db_pool()— database poolclient— Nostr clientMOSTRO_CONFIG— settingsMESSAGE_QUEUES.queue_order_msg— message queuemostro_keys— signing keysChanges
main.rs
AppContext::new()explicitly with all dependenciesctxtorun()instead ofmy_keysandclientapp.rs
run()signature:(my_keys, client, ln_client)→(ctx, ln_client)ctxat function startfrom_globals()call from event loopcontext.rs
from_globals()method entirely 🗑️MESSAGE_QUEUESimportBreaking Changes
run()function signature changed:Benefits
AppContextbuilt once in mainDiff Stats
Validation
✅
cargo fmt✅
cargo clippy --all-targets --all-features -- -D warnings✅
cargo test --bin mostrod(189 passed, 0 failed)🎉 DI Migration Complete!
With this PR, the dependency injection migration from #639 is complete:
Related
Summary by CodeRabbit