oxide_framework_core/controller.rs
1use std::sync::Arc;
2
3use axum::Router;
4
5use crate::router::OxideRouter;
6use crate::state::AppState;
7
8/// Trait implemented by `#[controller]`-annotated types.
9///
10/// You don't implement this manually — the `#[controller("/prefix")]` macro
11/// generates the implementation for you.
12///
13/// # Generated methods
14///
15/// - `from_state` — constructs the controller, extracting dependencies from
16/// [`AppState`]. Panics with a clear message at startup if a dependency is
17/// missing.
18/// - `register` — returns an [`OxideRouter`] with all route methods registered.
19/// Methods that take `&self` are wrapped in closures that capture `Arc<Self>`.
20pub trait Controller: Send + Sync + Sized + 'static {
21 /// URL prefix for all routes in this controller (e.g. `"/api/users"`).
22 const PREFIX: &'static str;
23
24 /// Construct the controller from application state.
25 fn from_state(state: &AppState) -> Self;
26
27 /// Register all route methods on a fresh router.
28 fn register(self: Arc<Self>) -> OxideRouter;
29
30 /// Override to apply controller-scoped middleware (auth, logging, etc.).
31 ///
32 /// The router passed in already contains all of this controller's routes.
33 /// Return the router with additional layers applied. The default is a
34 /// no-op (no extra middleware).
35 ///
36 /// If the `#[controller]` macro finds a `fn middleware(router: Router) -> Router`
37 /// method in the impl block, it generates this override automatically.
38 fn configure_router(router: Router) -> Router {
39 router
40 }
41}
42