dust

Request Lifecycle

Introduction

You feel more confident with a tool when you understand how it works. This guide gives you a high-level tour of a dust-framework application: how it starts, what happens to a request on its way to your handler, and how the response gets back to the browser. Along the way, you'll see where to register services, add middleware, and handle errors. If some of the terms are new, don't worry. Each one links to the guide that covers it, and a rough picture of the whole is enough to start.

Here is the whole path in one picture:

text
startup
  config -> providers register -> your app registers -> checks -> boot hooks
request
  kernel -> global middleware -> router -> route middleware -> handler
         <- route middleware <- error rendering <- response

A console command follows the same startup, then runs the command in a context of its own instead of handling a request.

Starting up

Creating the application

You describe your application in src/app.rs, where the configure function calls Application::configure(). The builder collects your providers, routes, middleware, and other settings, but nothing runs yet. The main function in src/main.rs calls run on the builder:

rust
use std::process::ExitCode;fn main() -> ExitCode {    app::configure().run()}

Your tests build the same application through app::testing::create in src/testing.rs, so a test exercises the configuration you ship, not a copy of it.

Whether your application is serving requests or running a test, dust-framework puts it together in this order:

  1. Configuration. dust-framework reads your environment variables and .env file. Service providers set their defaults, then your configuration files run, so your values override a package's.
  2. Providers register. Each package's provider registers its services and adds its routes, middleware, commands, and other definitions.
  3. Your application registers. dust-framework finds your #[singleton] and #[bind] services, listeners, and commands. Then your with_services, with_routing, with_middleware, and other callbacks in src/app.rs run, after every provider.
  4. dust-framework checks the result. A mistake such as an invalid environment variable, two routes that conflict, or a task scheduled for a command that doesn't exist stops the application.

The checks are deliberately strict, and they run before the server binds its port. A conflicting route or a missing dependency fails the deploy instead of failing a user's request. The error names the file and the item to fix.

Booting

Next, the application boots. Every provider's boot method runs, followed by the booted callbacks in src/app.rs. The application boots once, before it serves its first request or runs a command, and every request and command after that shares its services. If a boot hook returns an error, the application stops before it handles anything.

Booting doesn't build your services. dust-framework builds each one the first time something asks for it, such as a handler's Inject<T> argument, and shares it from then on. That keeps startup fast and lets a service read the final configuration. If a service must exist before the first request, resolve it in a booted callback; the container guide shows how.

Once the application boots, run serves it, on 127.0.0.1:8000 by default. When you name a console command, such as dust route:list, run runs that command instead, with the same configuration and services.

Handling a request

The kernel

Every request first enters dust-framework's HTTP kernel. To the server, the kernel is your whole application: it receives a request and returns a response. The kernel removes forwarded headers unless the request came through a trusted proxy. It gives the request a request ID for your logs, and a context, which helpers such as dust::config() and dust::url::route read.

The kernel also starts the request's tracing span, so every database query, cache lookup, and outgoing request inside the handler is recorded under the same trace.

Global middleware and routing

Next, the request passes through the global middleware, which run on every request. They reject hosts your application doesn't serve, answer CORS requests, and trim the strings in form input. They run before routing, so method spoofing can turn an HTML form's POST into the PUT or DELETE your route expects.

Then the router finds the route that matches the request. If no route matches, dust-framework serves the matching file from public/. If no file matches either, your fallback route handles the request, or the response is a 404.

A health check registered with routing.health(...) is answered before all of this, so a probe never waits on a session, a database, or a maintenance page. That is why a normal route may not use the health path: the framework refuses to start when one would be unreachable. See the health route.

The route

The matched route runs its middleware, then its handler:

  1. The route's own middleware. These are the middleware you assign to the route or to a route group that contains it, such as Authenticate.
  2. The handler. It does your application's work and returns a response.

Route middleware run in the order they are listed, so an outer middleware sees the request first and the response last. A middleware may short-circuit the rest of the chain, such as Authenticate redirecting a guest before the handler runs.

Sending the response

The response travels back out through the route's middleware in reverse order, so each one may inspect or change it. When a handler returns an error or panics, dust-framework renders the error as a response first. The route's middleware then see an ordinary response, such as a 404 or 500 page, the same as when the handler succeeds.

If a middleware fails or panics instead, the kernel turns that into an error response too. One failing request never takes down the server. Finally, the kernel logs the request with its status and duration and sends the response.

A streamed response, a download, or an event stream is an exception to the summary line's timing: the kernel logs it after the last chunk is sent, so its duration covers the whole response. If the client disconnects first, the status is 499.

Running a command

A console command shares the startup and the container with the server, but not the request path. run boots the application, then executes the command inside a command context. The command can resolve services, read configuration, and write logs exactly as a handler can; it cannot use the session or the current request, because there isn't one.

A command's exit code is its result. Returning Ok(()) exits with 0; returning an error prints it, reports it through the error handler, and exits with 1. A command that returns u8 chooses its own code. The console guide covers the cases.

Long-running commands such as serve, queue:work, and schedule:work don't wrap their whole life in one command span; each request, job, or scheduled task runs in a span of its own. The console guide shows how to mark a command as long-running.

Where your startup code goes

Packages take part in each step above through a service provider, which adds their services, routes, middleware, and commands, and can check its setup when the application boots. Your application doesn't need a provider of its own, because src/app.rs has the same hooks, such as with_services and booted, and they always run after every package's provider. Put your application's own startup code there.

You want to Use
Register a service, route, middleware, command, or view with_services, with_routing, with_middleware, with_commands, with_views
Change a setting after the environment is read with_config
Run code once, after every provider booted booted
Fail startup when something is misconfigured Return an error from booted
Add a package's contributions with_providers, then providers.add(...)

If you write a dust-framework Rust package that you want to publish and share with the community, you'll use a service provider to set its configuration defaults, add its routes, middleware, and commands, and do anything else your application would do in src/app.rs. The package development guide shows you how, and lists each builder with its src/app.rs counterpart.