Skip to main content

laterite_admin/
routes.rs

1//! Routes a module mounts itself: admin screens, and public endpoints.
2//!
3//! A [`Resource`](crate::Resource) covers list and form screens as data. A
4//! [`Screen`] is the substrate underneath: any routes a module wants, mounted
5//! inside the admin with the same session, permission, CSRF and error handling a
6//! resource gets. Reach for it only when descriptors cannot express the screen.
7//!
8//! A [`PublicRoute`] is the same idea outside the admin, for the endpoints that
9//! have to live at a literal path: `/robots.txt`, a sitemap, a feed, a webhook.
10
11use std::collections::HashMap;
12use std::sync::Arc;
13
14use axum::Router;
15use laterite_core::{Db, Events, Registry, Text};
16
17use crate::form::{FormHandle, Forms};
18use crate::list::ImportLink;
19
20/// What a contributed route is given at boot.
21///
22/// Deliberately narrow: the admin's own router state stays private, so its
23/// composition can change without breaking a screen. Fields are read through
24/// accessors, so this can gain context after 1.0; `new` cannot, and extra context
25/// would arrive as a builder.
26#[derive(Clone)]
27pub struct RouteCtx {
28    db: Db,
29    base_path: Arc<str>,
30    admin_path: Arc<str>,
31    base_url: Arc<str>,
32    plugin_defined: Arc<Registry>,
33    events: Events,
34    forms: Forms,
35}
36
37impl RouteCtx {
38    pub(crate) fn new(
39        db: Db,
40        base_path: &str,
41        admin_path: &str,
42        base_url: &str,
43        plugin_defined: Arc<Registry>,
44        events: Events,
45        forms: Forms,
46    ) -> Self {
47        Self {
48            db,
49            base_path: Arc::from(base_path),
50            admin_path: Arc::from(admin_path),
51            base_url: Arc::from(base_url),
52            plugin_defined,
53            events,
54            forms,
55        }
56    }
57
58    pub fn db(&self) -> &Db {
59        &self.db
60    }
61
62    /// A resource's form, to write its records from code: an importer, a sync,
63    /// anything that saves what an operator could have typed.
64    ///
65    /// `path` is the resource's path as its descriptor declares it, relative to
66    /// the admin root (`/products`). `None` when no resource mounts there or the
67    /// one that does has no form. The handle is `Clone` and owns what it needs,
68    /// so a detached task may keep it.
69    pub fn form(&self, path: &str) -> Option<FormHandle> {
70        self.forms
71            .get(path)
72            .map(|form| FormHandle::new(self.db.clone(), form.clone()))
73    }
74
75    /// The application's event bus, for a route that announces a fact:
76    /// `ctx.events().emit(&Published { article_id }).await`. Clone it into the
77    /// route's state.
78    pub fn events(&self) -> &Events {
79        &self.events
80    }
81
82    /// The site's own origin, without a trailing slash: the configured
83    /// `app.url`, falling back to the bind address when none is declared.
84    ///
85    /// For the absolute URLs a route has to emit rather than link: a `<loc>` in a
86    /// sitemap, a canonical URL, an entry in a feed. The request's `Host` header
87    /// is not a substitute, because behind a proxy it is whatever the proxy sent.
88    pub fn base_url(&self) -> &str {
89        &self.base_url
90    }
91
92    /// The contributions of type `T` that the framework itself does not consume.
93    ///
94    /// This is how one module reads another's contributions. A module declaring
95    /// its own kind of extension point defines a type, other modules contribute
96    /// it from their `register`, and the route reads them here: the framework
97    /// never learns the type, and registration order does not matter because
98    /// everything is collected before any route runs.
99    pub fn contributions<T: Send + Sync + 'static>(&self) -> Vec<&T> {
100        self.plugin_defined.items::<T>()
101    }
102
103    /// Where this screen actually mounted, after the module namespace, any base
104    /// it declared, the deployment's overrides and the admin mount.
105    pub fn base_path(&self) -> &str {
106        &self.base_path
107    }
108
109    /// Where the admin panel is mounted, after `backend.path`. Ask rather than
110    /// assuming `/admin`: an operator may have moved it, and a module building a
111    /// link into the panel, or excluding it from a sitemap, needs the real one.
112    pub fn admin_path(&self) -> &str {
113        &self.admin_path
114    }
115
116    /// An absolute admin URL under this screen. Build every self-link this way:
117    /// a deployment can move the screen, so a hardcoded path would break.
118    pub fn url(&self, relative: &str) -> String {
119        join(&self.base_path, relative)
120    }
121
122    /// A context built by hand, for testing a route without booting the framework.
123    ///
124    /// `mount` takes a `RouteCtx`, and the framework's own constructor is private,
125    /// so without this a plugin could only exercise its routes through a full boot.
126    /// With it, a route is an ordinary `tower` service:
127    ///
128    /// ```ignore
129    /// let ctx = RouteCtx::builder(db).admin_path("/backoffice").build();
130    /// let response = MyRoute.mount(&ctx).oneshot(request).await?;
131    /// ```
132    ///
133    /// This is also how the context grows after 1.0: a new field gets a builder
134    /// method and a default, which `new` could not have offered.
135    pub fn builder(db: Db) -> RouteCtxBuilder {
136        RouteCtxBuilder {
137            db,
138            base_path: "/".to_string(),
139            admin_path: "/admin".to_string(),
140            base_url: "http://localhost".to_string(),
141            plugin_defined: None,
142            events: None,
143        }
144    }
145}
146
147/// Builds a [`RouteCtx`]. Every field has a default, so a test names only what it
148/// asserts on.
149pub struct RouteCtxBuilder {
150    db: Db,
151    base_path: String,
152    admin_path: String,
153    base_url: String,
154    plugin_defined: Option<Arc<Registry>>,
155    events: Option<Events>,
156}
157
158impl RouteCtxBuilder {
159    /// Where the screen mounted. Defaults to `/`.
160    pub fn base_path(mut self, path: impl Into<String>) -> Self {
161        self.base_path = path.into();
162        self
163    }
164
165    /// Where the panel mounted. Defaults to `/admin`, which is what a route
166    /// excluding the panel should *not* be allowed to assume: set it to something
167    /// else in a test that checks the exclusion is read rather than hardcoded.
168    pub fn admin_path(mut self, path: impl Into<String>) -> Self {
169        self.admin_path = path.into();
170        self
171    }
172
173    /// The site's origin. Defaults to `http://localhost`.
174    pub fn base_url(mut self, url: impl Into<String>) -> Self {
175        self.base_url = url.into();
176        self
177    }
178
179    /// The contributions `RouteCtx::contributions` will read. Defaults to empty.
180    pub fn contributions(mut self, registry: Arc<Registry>) -> Self {
181        self.plugin_defined = Some(registry);
182        self
183    }
184
185    /// The bus `RouteCtx::events` will hand out. Defaults to one nothing
186    /// listens on; pass one built with a listener to assert on what a route
187    /// announces.
188    pub fn events(mut self, events: Events) -> Self {
189        self.events = Some(events);
190        self
191    }
192
193    pub fn build(self) -> RouteCtx {
194        let events = self.events.unwrap_or_else(|| Events::new(self.db.clone()));
195        RouteCtx::new(
196            self.db,
197            &self.base_path,
198            &self.admin_path,
199            &self.base_url,
200            self.plugin_defined
201                .unwrap_or_else(|| Arc::new(Registry::new())),
202            events,
203            Arc::new(HashMap::new()),
204        )
205    }
206}
207
208fn join(base: &str, relative: &str) -> String {
209    let base = base.trim_end_matches('/');
210    let relative = relative.trim_start_matches('/');
211    if relative.is_empty() {
212        base.to_string()
213    } else {
214        format!("{base}/{relative}")
215    }
216}
217
218/// A module's own admin screen.
219pub trait Screen: Send + Sync + 'static {
220    /// The routes this screen serves, relative to its own base. Handlers capture
221    /// what they need from `ctx`, so the router carries no framework state.
222    fn mount(&self, ctx: &RouteCtx) -> Router;
223}
224
225/// A registered screen: where it mounts, what it is called, and who may reach it.
226///
227/// Built with [`ScreenReg::new`] plus its builder methods. Non-exhaustive so the
228/// framework can learn something new about a screen after 1.0 without a major bump:
229/// anything it needs to know belongs here rather than as another trait method, which
230/// is also what keeps the code half replaceable at the plugin boundary.
231#[non_exhaustive]
232pub struct ScreenReg {
233    /// Path relative to the module's admin base, starting with a slash.
234    pub base_path: String,
235    /// Menu label, localized at render. `None` mounts it without a nav entry.
236    pub nav_label: Option<Text>,
237    /// The permission every route under this screen requires. Required, because
238    /// an unguarded screen exposes whatever it serves to any signed-in operator.
239    pub permission: String,
240    pub screen: Arc<dyn Screen>,
241}
242
243impl ScreenReg {
244    pub fn new(
245        base_path: impl Into<String>,
246        permission: impl Into<String>,
247        screen: Arc<dyn Screen>,
248    ) -> Self {
249        Self {
250            base_path: base_path.into(),
251            nav_label: None,
252            permission: permission.into(),
253            screen,
254        }
255    }
256
257    /// Shows this screen in the menu under `label`.
258    pub fn in_menu(mut self, label: impl Into<Text>) -> Self {
259        self.nav_label = Some(label.into());
260        self
261    }
262}
263
264/// A module's own route outside the admin.
265///
266/// Public by definition: no permission gate and no admin chrome, though the
267/// styled error pages still apply. A route that accepts a POST declares its own
268/// stance on CSRF, because the admin's blanket protection assumes a session.
269pub trait PublicRoute: Send + Sync + 'static {
270    /// The routes this endpoint serves, relative to its declared path.
271    fn mount(&self, ctx: &RouteCtx) -> Router;
272}
273
274/// A registered public route.
275///
276/// The path is literal and never namespaced: `/robots.txt` has to be exactly
277/// that. Collisions are therefore likelier than in the admin, so two claims on
278/// one path, or a claim on the admin mount, abort the boot.
279/// Built with [`PublicRouteReg::new`]. Non-exhaustive for the same reason as
280/// [`ScreenReg`].
281#[non_exhaustive]
282pub struct PublicRouteReg {
283    pub path: String,
284    pub route: Arc<dyn PublicRoute>,
285}
286
287impl PublicRouteReg {
288    pub fn new(path: impl Into<String>, route: Arc<dyn PublicRoute>) -> Self {
289        Self {
290            path: path.into(),
291            route,
292        }
293    }
294}
295
296/// A feature a module mounts under every resource whose descriptor declares
297/// its key: the routes behind `import: true`, say.
298///
299/// The admin owns the key and the button; the module owns the routes. A
300/// descriptor declaring a key no module serves aborts boot naming it, so the
301/// admin never depends on the module.
302pub trait ResourceFeature: Send + Sync + 'static {
303    /// The routes, relative to `{resource}/{key}`, which is `ctx.base_path()`.
304    fn mount(&self, ctx: &RouteCtx, resource: &ResourceInfo) -> Router;
305}
306
307/// A registered feature, keyed by the descriptor key it serves. Mounted under
308/// every resource declaring that key, behind the key's permission or, when the
309/// key names none, the resource's.
310#[non_exhaustive]
311pub struct ResourceFeatureReg {
312    pub key: String,
313    pub feature: Arc<dyn ResourceFeature>,
314}
315
316impl ResourceFeatureReg {
317    pub fn new(key: impl Into<String>, feature: Arc<dyn ResourceFeature>) -> Self {
318        Self {
319            key: key.into(),
320            feature,
321        }
322    }
323}
324
325/// The resource a feature mounts under, as the feature sees it.
326#[derive(Clone)]
327#[non_exhaustive]
328pub struct ResourceInfo {
329    /// The resource's path as its descriptor declares it, relative to the
330    /// admin root (`/products`).
331    pub path: String,
332    /// Where its list mounted (`/admin/products`).
333    pub base_path: String,
334    /// The resource's menu label.
335    pub label: Text,
336    /// The permission the feature's routes are gated by.
337    pub permission: String,
338    /// The resource's form, to write its records through.
339    pub form: FormHandle,
340}
341
342impl ResourceInfo {
343    pub(crate) fn new(
344        path: String,
345        base_path: String,
346        label: Text,
347        permission: String,
348        form: FormHandle,
349    ) -> Self {
350        Self {
351            path,
352            base_path,
353            label,
354            permission,
355            form,
356        }
357    }
358}
359
360/// The link a list shows to a feature mounted under it.
361pub(crate) fn feature_link(base_path: &str, key: &str, permission: &str) -> ImportLink {
362    ImportLink {
363        href: format!("{base_path}/{key}"),
364        permission: permission.to_string(),
365    }
366}
367
368#[cfg(test)]
369mod tests {
370    use super::*;
371
372    #[test]
373    fn a_self_link_builds_from_where_the_screen_actually_mounted() {
374        let base = "/admin/rainmill/location/import";
375        assert_eq!(
376            join(base, "/step2"),
377            "/admin/rainmill/location/import/step2"
378        );
379        assert_eq!(join(base, "step2"), "/admin/rainmill/location/import/step2");
380        assert_eq!(join(base, ""), "/admin/rainmill/location/import");
381    }
382
383    #[test]
384    fn a_moved_screen_moves_its_links_with_it() {
385        assert_eq!(join("/admin/places", "/step2"), "/admin/places/step2");
386    }
387
388    #[test]
389    fn a_registration_defaults_to_no_menu_entry() {
390        struct Noop;
391        impl Screen for Noop {
392            fn mount(&self, _ctx: &RouteCtx) -> Router {
393                Router::new()
394            }
395        }
396        let reg = ScreenReg::new("/import", "acme.import", Arc::new(Noop));
397        assert!(reg.nav_label.is_none());
398        assert!(reg.in_menu("Import").nav_label.is_some());
399    }
400}