Skip to main content

App

Struct App 

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

Configures an application: routes, middleware and shared state.

Implementations§

Source§

impl App

Source

pub fn new() -> Self

Source

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.

Source

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.

Source

pub fn hosts<I, S>(self, hosts: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

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).

Source

pub fn without_health_check(self) -> Self

Turns the built-in health check off.

Source

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>().

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn seeds<F, Fut>(self, seeds: F) -> Self
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Registers the data a new database starts with (usually seeds::run, from src/seeds.glacier), which glacier db seed loads in one transaction.

Source

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.

Source

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.

Source

pub fn env(self, env: Env) -> Self

Overrides the environment, which otherwise comes from the config or GLACIER_ENV.

Source

pub fn routes(self, draw: impl FnOnce(&mut Router)) -> Self

Source

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.

Source

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).

Source

pub fn webhooks(self, webhooks: Webhooks) -> Self

The providers whose webhooks the app receives, at post "/webhooks/:provider" => glacier::webhooks::receive; see crate::webhooks.

Source

pub fn billing(self, billing: Billing) -> Self

How the app takes payments; see crate::billing.

Source

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.

Source

pub fn mail_previews(self, previews: Previews) -> Self

The mail previews /dev/mail shows in development; see crate::mail::Previews.

Source

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).

Source

pub fn mail_delivery(self, delivery: impl Delivery) -> Self

Sends mail through delivery instead of what [mail] configures.

Source

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.

Source

pub fn max_body_size(self, bytes: usize) -> Self

Requests with larger bodies are rejected with 413 Payload Too Large.

Source

pub fn build(self) -> Service

Freezes the configuration into a cheaply cloneable Service.

Source

pub async fn serve(self, addr: impl ToSocketAddrs) -> Result<()>

Builds the app and serves it until Ctrl-C or SIGTERM.

Trait Implementations§

Source§

impl Default for App

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<T> IntoField<Option<T>> for T

Source§

fn into_field(self) -> Option<T>

Source§

impl<T> IntoField<T> for T

Source§

fn into_field(self) -> T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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