anvil_core/facade.rs
1//! Facade-style ambient helpers.
2//!
3//! Inside a request task, these resolve the request-scoped `Container`
4//! installed by `inject_container_mw` and hand you the underlying service.
5//! Outside a request (e.g. in `main.rs` before the server starts) they
6//! panic — use `Container` directly in that case.
7//!
8//! Laravel's `Cache::put()`, `Mail::to()`, `DB::connection()` etc. work this
9//! way: an ambient container resolves the right concrete implementation
10//! without each call site having to plumb a `$container` reference.
11//! Anvilforge's version is opt-in — handlers that take `State<Container>`
12//! work just as well, and the explicit signature is recommended in
13//! library code.
14
15use crate::container::{self, Container};
16
17/// The current request's container. Panics outside a request task.
18pub fn app() -> Container {
19 container::current()
20}
21
22/// Default DB driver pool. `let users = User::query().get(&db()).await?;`
23///
24/// Multi-driver friendly: returns the `cast::Pool` enum that the user's
25/// `DATABASE_URL` resolved to.
26pub fn db() -> cast_core::Pool {
27 container::current().driver_pool()
28}
29
30/// Default cache store.
31pub fn cache() -> crate::cache::CacheStore {
32 container::current().cache().clone()
33}
34
35/// Default queue.
36pub fn queue() -> crate::queue::QueueHandle {
37 container::current().queue().clone()
38}
39
40/// Default mailer.
41pub fn mailer() -> crate::mail::MailerHandle {
42 container::current().mailer().clone()
43}
44
45/// Storage manager.
46pub fn storage() -> crate::storage::StorageManager {
47 container::current().storage().clone()
48}
49
50/// Event bus.
51pub fn events() -> crate::event::EventBus {
52 container::current().events().clone()
53}
54
55/// Application config (`APP_NAME`, `APP_ENV`, `APP_KEY`, `APP_URL`, …).
56pub fn config() -> crate::config::AppConfig {
57 container::current().app().clone()
58}