The floor crate
One crate for the framework, its build step, Floor's API client and the floor CLI: its features, prelude, macros and the files a backend needs.
Floor in Rust is one crate, at version 0.6. On crates.io it's usefloor (the name floor was
taken there); its library is floor, so code says use floor::…. Its features pick the part
you need: a product backend takes the default, app.
Getting it
In Cargo.toml (floor new writes this for you):
[dependencies]
floor = { package = "usefloor", version = "0.6" }package = "usefloor" keeps the name floor in your code. Installing needs no token, on a
laptop or in CI.
Features
| Feature | What it gives you | Who uses it |
|---|---|---|
app (default) | The framework: a product backend as files, run by Floor. Turns on link-ws. | Product backends |
build | floor::build::generate(), for a backend's build.rs | Product backends, as a build-dependency |
client | floor::client, a typed client for Floor's API, and its types (api) | Tools, scripts, services |
api | floor::api: the requests and responses Floor's API takes and gives | client, bundle |
cli | The floor command-line tool | cargo install usefloor --features cli |
link | floor::link: the connection between Floor and a backend | Floor itself; app includes it |
link-ws | The link over a WebSocket, for a backend that can't reach Floor's socket (floor dev on a laptop) | app includes it |
bundle | floor::bundle: the .floor release file | cli |
A backend needs only app and, in its build-dependencies, build.
The prelude
use floor::prelude::*; at the top of a handler file brings in what handlers use:
| Kind | Items |
|---|---|
| The handle | Cx |
| Extractors and trigger inputs | Json, Query, Path, Body, Headers, Mqtt, MqttRaw, Job, DeviceEvent, Stop |
| Outputs | Json, Created, NoContent |
| Errors | Error, Result, OrNotFound |
| Realtime | Channel |
| WebSockets | Socket, Message (from floor::ws) |
| Services | Chat, Notification, Upload |
| Attributes | get, post, put, patch, delete, ws, mqtt, every, cron, device, job, background |
| Derive | Type |
| serde | Serialize, Deserialize |
The crate root
Beyond the prelude, the crate root has:
| Item | What it is |
|---|---|
floor::App, floor::Auth, floor::Limits, floor::Permissions | The app the build step generates, and who may call a route. See App. |
floor::run | Runs a backend. See App. |
floor::User | The caller, as Floor vouches for them. See Cx. |
floor::Request, floor::Response, floor::IntoResponse | A request as extractors read it, and what a route returns. See Handlers. |
floor::FromRequest, floor::FromTrigger, floor::Input, floor::Output | The traits behind extractors and trigger inputs, and how a route is described to the client. |
floor::FloorApi | Floor as a backend reaches it. Tests use a fake one. |
floor::Method | http::Method. |
floor::services | What Cx's services take and return. See Cx. |
floor::types | The shapes behind #[derive(floor::Type)]. |
floor::ws | Socket and Message, for WebSockets. |
floor::migrate | A backend's migrations, db/migrations/. |
floor::testing | TestApp. See Testing. |
The attributes and the derive are also at the root: #[floor::get(public)] works without the
prelude.
Re-exports
| Path | What |
|---|---|
floor::serde_json | serde_json |
floor::sqlx | sqlx |
floor::tracing | tracing, for handlers' logs: floor::tracing::info!(…), which floor dev and the journal show |
Keep serde, serde_json and sqlx as your own dependencies as well. Their derives and sqlx's
query! macros look for them by name.
Macros
floor::app!
floor::app!();Includes the app the build step generated: pub fn app() -> floor::App and the module tree of
api/, on/ and jobs/. Put it in src/lib.rs.
floor::channel!
floor::channel!(pub Lanes = "lanes", LaneCard);Declares a realtime channel and the message it carries. It makes a unit struct Lanes that
implements floor::Channel, with NAME = "lanes" and Message = LaneCard. cx.publish(Lanes, &card) publishes on app:lanes. The message type must implement Serialize and floor::Type.
Declare channels in channels.rs at the crate root. The build step reads that file and
registers each channel, so the dashboard's client gets a typed channels.lanes.
// channels.rs
floor::channel!(pub Lanes = "lanes", crate::model::LaneCard);floor::permissions!
floor::permissions! {
visits: { read: [admin, guard], remark: [admin, guard] },
gates: { open: [admin, guard] },
vehicles: { write: [admin] },
}Roles by resource and action, once for the whole API. It defines pub fn permissions() -> floor::Permissions, with one permission per resource:action (visits:read) and the roles
that hold it. Routes use it with #[get(can = "visits:read")], and handlers with
cx.can("visits:read").
Write it in permissions.rs at the crate root; the build step includes it. A can that names
a permission this file doesn't define stops the backend when it starts, and fails its tests.
The files a backend needs
gate/
├── Cargo.toml
├── build.rs
├── src/lib.rs
├── src/main.rs
├── api/ routes and WebSockets
├── on/ MQTT and device triggers, background tasks
├── jobs/ ticks, cron schedules, queue jobs
├── permissions.rs
├── channels.rs
└── db/migrations/floor new <name> writes all of these. See Attributes for what
goes in api/, on/ and jobs/.
Cargo.toml
The crate names floor twice: as the dependency, and as the build-dependency whose build
feature runs from build.rs.
[package]
name = "gate"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
floor = { package = "usefloor", version = "0.6" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { version = "0.9", default-features = false, features = ["postgres", "runtime-tokio", "tls-rustls-ring-webpki", "uuid", "time", "json", "migrate", "macros"] }
[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
[build-dependencies]
floor = { package = "usefloor", version = "0.6", default-features = false, features = ["build"] }build.rs
fn main() {
floor::build::generate();
}The build step walks api/, on/ and jobs/, finds the handlers in each file, and writes
$OUT_DIR/floor_app.rs. It is a build script, not a macro, so that Cargo rebuilds when you
add a handler file. A mistake, such as two files serving the same route, fails the build with
a message that names the file.
src/lib.rs
//! gate: a backend on the Floor framework.
floor::app!();
pub mod model;The crate's own modules (models, logic shared by handlers) live under src/ as usual.
src/main.rs
fn main() -> std::process::ExitCode {
floor::run(gate::app())
}floor::run serves HTTP, connects to Floor, and runs triggers until Floor drains the backend
or it is told to stop. See App.