dust

Directory Structure

Introduction

A new dust-framework application already has a place for your routes, handlers, configuration, modules, and views, so you, your teammates, and the agents that write code with you know where to find each part.

The layout follows the framework's own conventions: code lives in src, the parts you can enable or remove live in modules, files the application reads or writes at runtime live beside the directory you start it from, and the build never reaches outside the project. The table below describes a fresh dust new application; Adding your own modules lists the directories you add as the application grows.

The root directory

Path What it holds
Cargo.toml Your project's name, its dependencies, and the framework's Cargo features, such as the database driver.
Cargo.lock The exact versions Cargo resolved. Commit it, so every server builds the same graph.
build.rs Rebuilds your application when a migration changes, because your migrations compile into the binary.
.env Your environment configuration, such as the database URL and the encryption key. Never commit it.
.env.example The same keys as .env, with placeholder values, for your teammates and your servers.
.cargo/config.toml Optional: shares Cargo's intermediate build directory between applications. See the shared build cache.
config/ TOML documents the application reads and writes at runtime, such as frontend.toml and medialib.toml.
database/migrations/ Your migrations, one Rust file per change.
database/seeders/ Your seeders, which fill a database with rows.
modules/ Self-contained feature modules with their own routes, views, and migrations.
src/ Your application's Rust code.
resources/views/ Your views and the layout every page extends.
public/ Files your application serves as they are, such as robots.txt, your own assets, and the vendored public/vendor/io-ui frontend.
storage/ Files your application writes as it runs, such as uploads, logs, and the SQLite database.
tests/ Your tests. Create the directory when you write your first one.
bin/ The runnable binaries that dust build copies here, such as your application and dust-cron.

You may rename your project in the [package] section of Cargo.toml. Keep the [lib] name app, though, because dust-framework and your tests find your code by that name, as in use app::models::User.

The config directory

The config directory holds TOML documents, not Rust. They are for settings that are easier to keep in a file than in an environment variable: a list of allowed origins, the frontend release source, or a value the application changes while it runs. A new application starts with three:

File What it holds
config/frontend.toml The frontend framework and where dust ui:update downloads it.
config/medialib.toml Where the media library keeps conversions and which disk it uses.
config/update.toml Where dust update --download fetches the framework source.

Read and write them with the TOML helpers, such as dust::tomconf::read("frontend", "framework"). A document may sit one folder deep, such as config/services/stripe.toml. The modules guide adds config/modules.toml when it needs to list disabled modules.

The modules directory

A module is a self-contained area of the application: its own routes, handlers, migrations, views, and services. Modules are compiled into the application, so they add no crate and no Cargo.toml, and they live outside src so the application's own code stays separate from the parts you can enable, disable, or remove.

code
modules/
├── mod.rs                  the registry
└── blog/
    ├── mod.rs              pub struct Blog; impl Module for Blog
    ├── routes.rs
    ├── handlers/
    ├── migrations/
    └── views/

modules/mod.rs declares each module and registers it with the application in one place. The modules guide covers the Module trait, creating a module with dust module:make, and disabling one without recompiling.

The resources directory

Path What it holds
resources/views/ Your views, written in Jinja syntax: pages, layouts, partials, and components.
resources/views/layout.html The document every page extends, including the io/ui stylesheet and behavior.
resources/views/welcome.html The page the home handler renders.

The frontend itself is not built from resources. dust new writes the prepared io/ui files to public/vendor/io-ui, and the layout links them from there:

html
<link rel="stylesheet" href="/vendor/io-ui/paz.css"><script type="module" src="/vendor/io-ui/paz.js"></script>

If you add a CSS pipeline of your own, keep its sources under resources/css or resources/js and write the built files to public/. Nothing in the framework requires such a pipeline. The frontend guide covers io/ui's classes, its data attributes, and how to update the vendored release.

The storage directory

The storage directory holds the files your application writes, and Git ignores what's inside. storage/app/ is for files your application keeps, such as uploads or the SQLite database. The public disk stores its files in storage/app/public, and dust-framework serves them at /storage, so a user can download an avatar stored at avatars/1.png from /storage/avatars/1.png. The local disk keeps private files in storage/app/private.

storage/framework/ holds the framework's own working files, such as compiled views, cached data, and test databases. You rarely touch it by hand. storage/logs/ holds log files when you use the daily log channel. By default, logs go to standard error, so the directory stays empty until you change that setting.

In production, you'll usually store uploads in object storage instead, so every server sees the same files and they survive a redeploy. DUST Cloud Object Storage works out of the box with no extra Cargo features, and you can also use any S3-compatible disk by enabling dust-framework's s3 feature.

The tests directory

The tests directory holds your tests. Cargo compiles each file in it as a separate crate that depends on your application, so a test sees only the modules src/lib.rs declares pub. A fresh application does not create the directory; add it with your first test file.

A test that accepts an Application runs inside a full application, with the same routes, middleware, and services you ship:

rust
// tests/welcome.rsuse dust::testing::prelude::*;#[dust::test]async fn the_welcome_page_loads(app: Application) {    app.get("/").await.assert_ok().assert_see("Welcome");}

Run them with cargo test. Tests that touch the database need refresh_database in src/testing.rs; on PostgreSQL and MySQL, the test user must be able to create databases, as Refreshing the database explains. The testing guide covers the client, assertions, fakes, and time travel.

The src directory

Most of your code lives in src, with a module for each kind of code, such as routes for your route files and http/handlers for your handlers.

Path What it holds
src/main.rs The executable: it calls app::configure().run().
src/lib.rs Declares your modules and re-exports configure. Your tests and the binary both use it.
src/app.rs Builds the application: migrations, services, routes, and middleware.
src/routes/ The route files, one per group, such as web.rs and api.rs.
src/http/handlers/ The functions your routes point at.
src/http/mod.rs Declares your HTTP-side modules.
src/config/ Rust configuration files for settings a TOML document can't express.
src/bin/dust-cron.rs The cron daemon binary that dust build copies to bin/.

Configuring your application

src/app.rs configures your application. Its configure function registers your migrations, your modules, your packages' service providers, your authentication guard, your route files, and your middleware. You'll come back to it whenever you register something new. A fresh application keeps it short:

rust
// src/app.rspub fn configure() -> ApplicationBuilder {    Application::configure()        .with_migrations(dust::db::migrations!("database/migrations"))        .with_services(crate::modules::register)        .with_routing(|routing, _| {            routing.web(crate::routes::web::routes);        })}

src/main.rs runs that configuration. With no arguments, it serves your application, and with a command name, such as dust migrate, it runs that console command.

src/lib.rs declares your application's modules. Rust compiles only what you declare, so a new file is invisible until it has a mod line:

rust
// src/lib.rspub mod app;pub mod config;pub mod http;pub mod routes;#[path = "../modules/mod.rs"]pub mod modules;pub use crate::app::configure;

Routes, handlers, and middleware

src/routes/ holds one file per route group. web.rs registers the routes a browser visits, with sessions and CSRF protection; api.rs registers stateless JSON routes. Each file exposes a routes function that takes the router:

rust
// src/routes/web.rsuse crate::http::handlers;use dust::prelude::*;pub fn routes(route: &mut Router) {    route.get("/", handlers::home::index);    route.get("/health", handlers::home::health);    route.get("/hello/{name}", handlers::home::hello);}

The handlers themselves live under src/http/handlers/, one file per resource or page group. src/app.rs registers both route files once, so adding a route means adding it to the file that owns its URL, not editing the builder. The routing guide covers route parameters, groups, names, and middleware, and the handlers guide covers what a handler may return.

Models

The src/models directory holds your models. A model is a struct that reads and writes one of your database tables. A fresh application has the users table from its first migration, but no models module; create src/models/ and add pub mod models; to src/lib.rs when you write your first model.

rust
// src/models/mod.rsmod user;pub use user::User;

Your User model reads the users table that authentication uses to log users in. Keep each model's policy and factory in the same file when you add them; the models guide explains the conventions.

Configuration files

The src/config directory holds your configuration files, written in Rust. Most settings come from your .env file, so a new application only has three: database.rs for your database connections, cors.rs for cross-origin requests, and services.rs for your own sections, such as your Sentry DSN. When you need to change something environment variables can't express, such as adding a cache store, you add a file here and a mod line to src/config/mod.rs.

src/config is for typed settings the application needs at startup. config/ at the root is for TOML documents the application reads and writes at runtime. A new setting usually belongs in .env; move it to src/config only when it needs code.

Console commands and the schedule

Console commands live wherever you declare them: a #[command] function is discovered in any module of your app crate. A fresh application has none of its own, so the framework's commands, such as migrate and route:list, are what dust list shows at first.

As your application grows, give commands their own module, src/console/, and declare it in src/lib.rs:

rust
// src/console/mod.rsmod prune_drafts;pub use prune_drafts::prune_drafts;
rust
// src/console/prune_drafts.rsuse dust::console::{Console, command};/// Delete every unpublished draft.#[command("posts:prune")]pub async fn prune_drafts(console: &mut Console) -> dust::Result<()> {    console.success("Drafts pruned.");    Ok(())}

A command struct is registered in src/app.rs with with_commands, and scheduled tasks are registered with with_schedule or in src/console/mod.rs. The console guide covers arguments, prompts, progress, and signals, and the scheduling guide covers recurring tasks.

Events and listeners

The src/events directory holds your events, the structs that describe something your application did, such as UserRegistered. The src/listeners directory holds the #[listener] functions that respond to them. For example, a send_welcome_email listener might email each new user.

Neither directory ships with a fresh application. Create them when you add your first event, declare both in src/lib.rs, and keep the event and its listeners close together. The events guide covers dispatching, queued listeners, and fakes, and the queues guide covers work that should run in the background.

Adding your own modules

As your application grows, you'll write code your application has no module for yet. Give each kind its own module. These are the usual ones:

Module What it holds
src/jobs/ Your queued jobs.
src/mail/ Your mailables.
src/notifications.rs Your notifications.
src/policies/ Your authorization policies.
src/services/ Your services, registered with #[singleton] or #[bind].
src/features.rs Your feature flags.
src/actions/ Reusable functions that several handlers call, such as create_order.

Declare each new module in src/lib.rs, and declare its files in the module's mod.rs. This jobs module holds a SendWelcomeEmail job in src/jobs/send_welcome_email.rs:

rust
// src/lib.rspub mod jobs;
rust
// src/jobs/mod.rsmod send_welcome_email;pub use send_welcome_email::SendWelcomeEmail;

Declare a module pub when your tests need its items, such as a job your test checks was dispatched. You may keep a module private when nothing outside it uses its types, such as a group of handlers that only route files reference.

You may also group a larger feature into a module of its own rather than splitting it across kind-based directories. For example, src/services/ works well for standalone API clients, while a src/billing/ module can keep its payment client, configuration section, middleware, and queued jobs together in one place. When the feature should also be disableable without recompiling, put it under modules/ instead, as the modules guide describes.

dust-framework finds your commands, schedule, listeners, jobs, services, feature flags, and config sections by their attributes in any declared module, so you don't register them in src/app.rs. Rust compiles only the modules you declare, though. If you forget a mod line, Rust skips the file without a warning, and nothing in it runs.