1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::db::Migration;
use crate::{Contributions, Registry, Routes};
/// A self-contained piece of an application: its routes and migrations, and
/// later its jobs, policies and views.
///
/// ```
/// # use renox::prelude::*;
/// # async fn index() -> &'static str { "" }
/// # async fn show() -> &'static str { "" }
/// pub struct Products;
///
/// impl Module for Products {
/// fn name(&self) -> &'static str { "products" }
///
/// fn routes(&self) -> Routes {
/// Routes::new()
/// .get("/products", index).name("products.index")
/// .get("/products/{id}", show).name("products.show")
/// }
/// }
/// ```
pub trait Module: Send + Sync + 'static {
/// The module's name, shown by `route:list`.
fn name(&self) -> &'static str;
/// The module's routes; none by default.
fn routes(&self) -> Routes {
Routes::new()
}
/// The module's routes, given everything modules contributed in
/// `register` (see `Registry::contribute`). Every `register` has run
/// by then, so module order does not matter. Calls [`Module::routes`]
/// by default.
///
/// ```
/// # use renox::prelude::*;
/// struct Link(&'static str);
/// struct Menu;
/// impl Module for Menu {
/// fn name(&self) -> &'static str { "menu" }
/// fn routes_with(&self, contributions: &Contributions) -> Routes {
/// let _links = contributions.all::<Link>();
/// Routes::new()
/// }
/// }
/// ```
fn routes_with(&self, _contributions: &Contributions) -> Routes {
self.routes()
}
/// Migrations this module owns, e.g. `renox::migrations!("src/app/products/migrations")`.
fn migrations(&self) -> &'static [Migration] {
&[]
}
/// Registers the module's jobs, event listeners and scheduled tasks.
///
/// ```
/// # use renox::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize)] struct SendReceipt { order_id: i64 }
/// # impl Job for SendReceipt { const NAME: &'static str = "send-receipt"; async fn handle(self, _: JobContext) -> Result { Ok(()) } }
/// # #[derive(Clone)] struct OrderPlaced { order_id: i64 }
/// # impl Event for OrderPlaced {}
/// # async fn close_day(_: AppState) -> Result { Ok(()) }
/// # struct Orders;
/// # impl Module for Orders {
/// # fn name(&self) -> &'static str { "orders" }
/// fn register(&self, app: &mut Registry) {
/// app.job::<SendReceipt>()
/// .listen(|e: OrderPlaced, state| async move {
/// state.dispatch(SendReceipt { order_id: e.order_id }).await?;
/// Ok(())
/// });
/// app.schedule().daily_at("02:00", "close-day", close_day);
/// }
/// # }
/// ```
fn register(&self, _app: &mut Registry) {}
}