pub struct App { /* private fields */ }Expand description
The entry point of every rustlavel application.
App::new()?
.routes(routes::web::routes)
.plugin(Telescope::default())
.serve()
.awaitImplementations§
Source§impl App
impl App
Sourcepub fn from_root(root: impl Into<PathBuf>) -> Result<Self>
pub fn from_root(root: impl Into<PathBuf>) -> Result<Self>
Boot from an explicit project root. Tests use this to point at a fixture.
Sourcepub fn registered<T: Send + Sync + 'static>(&self) -> Option<&T>
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.
pub fn config(&self) -> &Config
pub fn root(&self) -> &Path
Sourcepub fn routes(self, define: impl FnOnce(&mut Router)) -> Self
pub fn routes(self, define: impl FnOnce(&mut Router)) -> Self
Register routes — this is what loads routes/web.rs.
Sourcepub fn middleware(self, middleware: impl Middleware) -> Self
pub fn middleware(self, middleware: impl Middleware) -> Self
Add middleware that runs for every request.
Sourcepub fn on_shutdown(
self,
work: impl FnOnce() -> BoxFuture<()> + Send + Sync + 'static,
) -> Self
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(®istrar).start();
app.on_shutdown(move || Box::pin(async move {
let _ = registrar.deregister().await;
}))pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
Sourcepub fn plugin_boxed(self, plugin: Box<dyn Plugin>) -> Self
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.
Sourcepub fn public(self, dir: impl Into<PathBuf>) -> Self
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.
Sourcepub async fn run(self) -> Result<()>
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.
Sourcepub fn take_parts(self) -> (Router, Context)
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.
Sourcepub fn test_client(self) -> TestClient
pub fn test_client(self) -> TestClient
A test client for this application, without binding a port.