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
impl PublicRouter
Sourcepub fn new(tera: Arc<Tera>) -> Self
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.
Sourcepub fn static_dir(
self,
url_prefix: impl Into<String>,
fs_root: impl Into<PathBuf>,
) -> Self
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.
Sourcepub fn static_file(
self,
url_path: impl Into<String>,
bytes: &'static [u8],
content_type: &'static str,
) -> Self
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.
Sourcepub fn with_form_mailer(
self,
mailer: Arc<dyn Mailer>,
from_addr: impl Into<String>,
) -> Self
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.
Sourcepub fn at(self, prefix: impl Into<String>) -> Self
pub fn at(self, prefix: impl Into<String>) -> Self
Mount the CMS under prefix instead of the root. Same shape
as router_at.
Sourcepub fn cached(self, cache: BoxedCache, ttl: Duration) -> Self
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.
Sourcepub fn locale_mode(self, mode: LocaleMode) -> Self
pub fn locale_mode(self, mode: LocaleMode) -> Self
Pick how the router recognizes the request’s locale code.
See LocaleMode for the trade-offs.
Sourcepub fn tenant_templates(self, tt: TenantTemplates) -> Self
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();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.
Auto Trait Implementations§
impl !RefUnwindSafe for PublicRouter
impl !UnwindSafe for PublicRouter
impl Forget for PublicRouter
impl Freeze for PublicRouter
impl Send for PublicRouter
impl Sync for PublicRouter
impl Unpin for PublicRouter
impl UnsafeUnpin for PublicRouter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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