Skip to main content

App

Struct App 

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

The entry point of every rustlavel application.

ⓘ
App::new()?
    .routes(routes::web::routes)
    .plugin(Telescope::default())
    .serve()
    .await

Implementations§

Source§

impl App

Source

pub fn new() -> Result<Self>

Boot from the current directory: load .env, then config/*.json.

Source

pub fn from_root(root: impl Into<PathBuf>) -> Result<Self>

Boot from an explicit project root. Tests use this to point at a fixture.

Source

pub fn bare() -> Self

Build an application with no filesystem behind it, for unit tests.

Source

pub fn registered<T: Send + Sync + 'static>(&self) -> Option<&T>

Something the application registered with App::state, before it runs. The console commands read the database this way.

Source

pub fn config(&self) -> &Config

Source

pub fn root(&self) -> &Path

Source

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

Register routes — this is what loads routes/web.rs.

Source

pub fn middleware(self, middleware: impl Middleware) -> Self

Add middleware that runs for every request.

Source

pub fn on_shutdown( self, work: impl FnOnce() -> BoxFuture<()> + Send + Sync + 'static, ) -> Self

Register a service that handlers resolve with req.state::<T>(). Run something on the way down, after in-flight requests have drained.

What a service registered with a discovery server uses to say goodbye — the difference between a clean deploy and thirty seconds of requests routed to a process that has exited.

ⓘ
let registrar = Arc::new(Registrar::new(nodes, instance));
Arc::clone(&registrar).start();

app.on_shutdown(move || Box::pin(async move {
    let _ = registrar.deregister().await;
}))
Source

pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self

Source

pub fn plugin(self, plugin: impl Plugin) -> Self

Enable an optional package.

Source

pub fn plugin_boxed(self, plugin: Box<dyn Plugin>) -> Self

The same, for a plugin chosen at runtime.

plugin takes a concrete type, which is right when main.rs names each one. An application that keeps its features in a list — a module per feature, registered by walking the list — has Box<dyn Plugin> instead, and had no way in.

Source

pub fn fallback(self, handler: impl Handler) -> Self

Replace the default 404 handler.

Source

pub fn public(self, dir: impl Into<PathBuf>) -> Self

Serve static files from a directory. public/ is used automatically when it exists, so this is only needed for a different location.

Source

pub async fn run(self) -> Result<()>

The application’s entry point: serve, or run the command the CLI forwarded to this binary.

rustlavel route:list cannot answer on its own — in a compiled language only the built application knows its routes — so the CLI re-invokes this binary with the command as an argument.

Source

pub async fn serve(self) -> Result<()>

Bind and serve until Ctrl-C.

Source

pub fn take_parts(self) -> (Router, Context)

The finished router and context, for serving on a listener of the caller’s own. Tests that need a real socket use this.

Source

pub fn test_client(self) -> TestClient

A test client for this application, without binding a port.

Source

pub fn route_table(&self) -> Vec<(String, String, String)>

The routes as rustlavel route:list prints them.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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