Skip to main content

PublicRouter

Struct PublicRouter 

Source
pub struct PublicRouter { /* private fields */ }
Expand description

Builder for the public CMS router. Use when you need to combine mount-prefix, response caching, and/or path-based locale resolution — the free router / router_at / *_cached functions are shortcuts for common combinations and don’t expose every knob.

use std::sync::Arc;
use std::time::Duration;
use rustango_cms::{LocaleMode, PublicRouter};
let router = PublicRouter::new(tera)
    .at("/p")
    .cached(cache, Duration::from_secs(60))
    .locale_mode(LocaleMode::PathOrQuery)
    .build();

Implementations§

Source§

impl PublicRouter

Source

pub fn new(tera: Arc<Tera>) -> Self

Start a builder bound to the given Tera instance, mounted at the root with no cache and query-mode locale resolution.

Source

pub fn static_dir( self, url_prefix: impl Into<String>, fs_root: impl Into<PathBuf>, ) -> Self

Mount a filesystem directory as a static-file server at url_prefix. Files under fs_root become reachable at {url_prefix}/{relative path}. fs_root is anchored to the process’s current working directory (matches what a host’s Path::new("./static") would resolve to).

Stacks: call multiple times to expose multiple dirs (e.g. /static/ for app assets + /admin-static/ for shared chrome).

§Path-traversal safety

The handler rejects any request whose normalized path escapes fs_root — ../ segments, absolute paths, and embedded .. after URL-decode all 404. Files outside the root are never served.

Source

pub fn static_file( self, url_path: impl Into<String>, bytes: &'static [u8], content_type: &'static str, ) -> Self

Serve a single static file from baked bytes. Pair with include_bytes!("…") to embed asset bytes directly in the binary — useful for favicon.ico, a small logo, or a brand stylesheet that ships with the host.

Source

pub fn with_form_mailer( self, mailer: Arc<dyn Mailer>, from_addr: impl Into<String>, ) -> Self

Wire a mailer for form-submission notifications. Each `cms_form_settings.notify_emails` recipient gets a copy of every submission to that page. from_addr is the From: stamped on every notification.

Source

pub fn at(self, prefix: impl Into<String>) -> Self

Mount the CMS under prefix instead of the root. Same shape as router_at.

Source

pub fn cached(self, cache: BoxedCache, ttl: Duration) -> Self

Layer rustango::cache_page::CachePageLayer onto the built router. cache is the storage backend (any rustango::cache::BoxedCache — InMemoryCache, RedisCache, …); ttl is the per-entry expiration. Cache invalidation is TTL-based — admin edits don’t surgically drop affected keys; editors see stale content for up to ttl seconds after a save.

Source

pub fn locale_mode(self, mode: LocaleMode) -> Self

Pick how the router recognizes the request’s locale code. See LocaleMode for the trade-offs.

Source

pub fn tenant_templates(self, tt: TenantTemplates) -> Self

Let each tenant override templates from its own directory.

root holds one subdirectory per tenant slug; a name found there wins over the global set and anything absent falls through, so a tenant only stores what differs. Keep root outside the glob this router’s Tera was built from, or every tenant’s files land in every instance.

Templates become deploy artifacts rather than code: dropping a file changes a site with no rebuild, and — depending on Reload — no restart either.

let overrides = TenantTemplates::new(Arc::clone(&tera), "./templates_tenants")
    .with_reload(Reload::Every(std::time::Duration::from_secs(30)));
let router = PublicRouter::new(tera).tenant_templates(overrides).build();
Source

pub fn tenant_templates_shared(self, tt: Arc<TenantTemplates>) -> Self

Like tenant_templates but takes an already-shared instance, so the same one can also be handed to crate::tenant_templates::install for the admin editor — otherwise the editor would be writing files a different cache is serving from.

Source

pub fn build(self) -> Router

Finalize the builder into an axum Router.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more