pub struct App { /* private fields */ }Expand description
Configures an application: routes, middleware and shared state.
Implementations§
Source§impl App
impl App
pub fn new() -> Self
Sourcepub fn telemetry(self, telemetry: Telemetry) -> Self
pub fn telemetry(self, telemetry: Telemetry) -> Self
Sends traces somewhere besides the logs, e.g. an OpenTelemetry
collector: .telemetry(glacier_otel::from_env("blog")?). Installed
when the app starts serving, and flushed when it stops.
Sourcepub fn health_check(self, path: &str) -> Self
pub fn health_check(self, path: &str) -> Self
Serves the health check at path instead of /up.
The health check answers GET (and HEAD) with 200 ok when the
app is up and its database answers, or 503 when the database
doesn’t within two seconds, so a deploy agent or load balancer knows
when to send traffic. It runs before any middleware: no logging, no
cookies, no HTTPS redirect. A route of the app’s own at the same path
takes its place.
Sourcepub fn hosts<I, S>(self, hosts: I) -> Self
pub fn hosts<I, S>(self, hosts: I) -> Self
Answers only requests for these hosts, refusing others with 403;
.example.com also allows its subdomains. The config’s hosts and
url set this (see glacier::hosts).
Sourcepub fn without_health_check(self) -> Self
pub fn without_health_check(self) -> Self
Turns the built-in health check off.
Sourcepub fn config(self, config: Config) -> Result<Self>
pub fn config(self, config: Config) -> Result<Self>
Applies a loaded Config: environment, secret_key_base,
server.max_body_size and the hosts the app answers for (hosts
and url). The config is also registered as state, so
handlers can read it with ctx.state::<Config>().
Sourcepub fn secret_key_base(self, secret: &str) -> Result<Self>
pub fn secret_key_base(self, secret: &str) -> Result<Self>
The secret that signs and encrypts cookies (including the session). Without one, a random key is generated per boot, which logs everyone out on restart; that’s refused in production.
Sourcepub fn database(self, repo: Repo) -> Self
pub fn database(self, repo: Repo) -> Self
Makes repo the ambient database for every request, so model calls
need no connection argument. It’s also registered as state.
Sourcepub fn named_database(self, name: &str, repo: Repo) -> Self
pub fn named_database(self, name: &str, repo: Repo) -> Self
Another database, for models declared with database: :name
(#[glacier::model(database = "name")]): ambient in every request,
with replica reads as the main database has them.
Sourcepub fn tasks(self, tasks: Vec<Task>) -> Self
pub fn tasks(self, tasks: Vec<Task>) -> Self
Registers the app’s tasks (tasks::all(), from
src/tasks.glacier), which glacier run <task> and the binary’s
app <task> run.
Sourcepub fn seeds<F, Fut>(self, seeds: F) -> Self
pub fn seeds<F, Fut>(self, seeds: F) -> Self
Registers the data a new database starts with (usually
seeds::run, from src/seeds.glacier), which glacier db seed
loads in one transaction.
Sourcepub fn named_migrations(self, name: &str, migrations: Vec<Migration>) -> Self
pub fn named_migrations(self, name: &str, migrations: Vec<Migration>) -> Self
Registers a named database’s migrations
(analytics_migrations::all()), which glacier db migrate runs
after the app’s own.
Sourcepub fn migrations(self, migrations: Vec<Migration>) -> Self
pub fn migrations(self, migrations: Vec<Migration>) -> Self
Registers the app’s migrations (usually migrations::all()), so
glacier db migrate can run them and development warns about
pending ones.
Sourcepub fn env(self, env: Env) -> Self
pub fn env(self, env: Env) -> Self
Overrides the environment, which otherwise comes from the config or
GLACIER_ENV.
pub fn routes(self, draw: impl FnOnce(&mut Router)) -> Self
Sourcepub fn middleware(self, middleware: impl Middleware) -> Self
pub fn middleware(self, middleware: impl Middleware) -> Self
Adds middleware that wraps every request, including 404s. The first
one added is the outermost. For middleware on some routes only, see
Router::pipe_through.
Sourcepub fn jobs(self, jobs: Jobs) -> Self
pub fn jobs(self, jobs: Jobs) -> Self
The background jobs the app runs: serve starts their workers on
the app’s database. Also registered as state, for tests
(TestClient::perform_jobs).
Sourcepub fn webhooks(self, webhooks: Webhooks) -> Self
pub fn webhooks(self, webhooks: Webhooks) -> Self
The providers whose webhooks the app receives, at
post "/webhooks/:provider" => glacier::webhooks::receive; see
crate::webhooks.
Sourcepub fn billing(self, billing: Billing) -> Self
pub fn billing(self, billing: Billing) -> Self
How the app takes payments; see crate::billing.
Sourcepub fn component_previews(self, gallery: fn() -> String) -> Self
pub fn component_previews(self, gallery: fn() -> String) -> Self
The page /dev/components shows in development: each
component’s examples, in the app’s layout. glacier::app!()
registers it.
Sourcepub fn mail_previews(self, previews: Previews) -> Self
pub fn mail_previews(self, previews: Previews) -> Self
The mail previews /dev/mail shows in development; see
crate::mail::Previews.
Sourcepub fn error_pages(self, render: fn(u16) -> Option<String>) -> Self
pub fn error_pages(self, render: fn(u16) -> Option<String>) -> Self
The app’s own error pages: render(404) gives the page for a
status, in the app’s layout, or None for a status without one.
glacier::app!() registers the templates in src/views/errors/
(404.html, 500.html).
Sourcepub fn mail_delivery(self, delivery: impl Delivery) -> Self
pub fn mail_delivery(self, delivery: impl Delivery) -> Self
Sends mail through delivery instead of what [mail] configures.
Sourcepub fn state<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
pub fn state<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
Registers shared state, retrieved in handlers with ctx.state::<T>().
One value per type; registering a type twice replaces the first.
Sourcepub fn max_body_size(self, bytes: usize) -> Self
pub fn max_body_size(self, bytes: usize) -> Self
Requests with larger bodies are rejected with 413 Payload Too Large.
Sourcepub async fn serve(self, addr: impl ToSocketAddrs) -> Result<()>
pub async fn serve(self, addr: impl ToSocketAddrs) -> Result<()>
Builds the app and serves it until Ctrl-C or SIGTERM.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for App
impl !UnwindSafe for App
impl Freeze for App
impl Send for App
impl Sync for App
impl Unpin for App
impl UnsafeUnpin for App
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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