Attributes
Every handler attribute and what it takes, where each may live, how file paths become routes and trigger names, and the errors mistakes give.
An attribute goes on a pub async fn in a file under api/, on/ or jobs/. It checks its
settings when the crate compiles and registers the function with them. The build step says where
the function is: a route's path from its file, a trigger's name from its file and function.
All of them are in the prelude, and at the crate root: #[get(public)] and
#[floor::get(public)] are the same.
Where each lives
| Attribute | Folder |
|---|---|
get, post, put, patch, delete, ws | api/ only |
mqtt, every, cron, device, job, background | on/ or jobs/, never api/ |
The convention is on/ for what reacts to the outside (MQTT, device events, background tasks)
and jobs/ for what runs on time or from a queue (ticks, cron, jobs). The build step treats the
two the same.
A file may hold several handlers: get and post in api/visits/index.rs, or a #[device]
handler and a #[background] task in one file under on/. Functions without one of these
attributes are left alone, so helpers can sit next to handlers.
Routes
#[get], #[post], #[put], #[patch], #[delete]
#[get(public)]
#[get(signed_in)]
#[post(device)]
#[get(role = "admin")]
#[get(roles = "admin, guard")]
#[get(can = "visits:read")]
#[post(can = "files:write", body = "20mb", timeout = "2m")]A route on the file's path. Every route says who may call it, exactly once:
| Setting | Lets in |
|---|---|
public | anyone |
signed_in | any valid Floor token for the project: a user's access token or a service client's |
role = "admin" | callers with that role |
roles = "admin, guard" | callers with any of these roles (role and roles are the same) |
can = "visits:read" | callers whose roles permissions.rs allows resource:action |
device | the project's devices, by their Floor device tokens; cx.device() gives the hardware id |
And, optionally, its limits:
| Setting | Default | Range | Units |
|---|---|---|---|
body = "20mb" | 1 MiB | 1kb to 1gb | a number of bytes, or b, k/kb, m/mb, g/gb (powers of 1024) |
timeout = "2m" | 60 s | 1s to 30m | ms, s, m, h |
// api/visits/[id].rs: GET /api/visits/{id}
use floor::prelude::*;
#[get(can = "visits:read")]
pub async fn get(cx: Cx, Path(id): Path<i64>) -> Result<Json<Visit>> { … }Mistakes, at compile time:
| You wrote | The error |
|---|---|
no caller setting, e.g. #[get] or #[post(body = "2mb")] | say who may call it: public, signed_in, device, role = "…" or can = "resource:action" |
two caller settings, e.g. public, signed_in | say who may call it once |
role = "" | name at least one role |
can = "visits" | "visits" isn't resource:action |
body = "2tb" | "2tb" isn't a size like 10mb (kb, mb or gb) |
body = "512" | body is 1kb to 1gb |
timeout = "2h" | timeout is 1s to 30m |
timeout = "30" | "30" isn't a duration like 30s (ms, s, m or h) |
A can naming a permission permissions.rs doesn't define compiles, but stops the backend at
start and fails its tests.
#[ws]
#[ws(signed_in)]
#[ws(can = "visits:read")]A WebSocket on the file's path. It takes the same caller settings as a route, checked before the
upgrade. The token comes as Authorization: Bearer … or, from a browser, as ?access_token=….
It takes no body or timeout: "a WebSocket takes no body or timeout: it lasts as long as the
caller stays".
// api/live.rs: /api/live
#[ws(can = "visits:read")]
pub async fn live(cx: Cx, mut socket: Socket) -> Result<()> {
while let Some(m) = socket.recv().await {
if let Message::Text(t) = m && t == "refresh" {
socket.send_json(&inside(&cx).await?).await?;
}
}
Ok(())
}See WebSockets.
Triggers
#[mqtt]
#[mqtt("+/events")]MQTT messages whose topic matches the filter inside the project's prefix: +/events means
<project>/+/events. + matches one level, # the rest. The handler takes Mqtt<T> or
MqttRaw. Each message goes to one handler: when two filters match a topic, the first in file
order runs.
| You wrote | The error |
|---|---|
no string, e.g. #[mqtt] | write #[mqtt("+/events")] |
"", or a filter starting with / or $ | "$SYS/#" isn't a filter inside the project |
#[every]
#[every("30s")]A tick, one run at a time: a tick that comes while the last one still runs is skipped. The
interval is in ms, s, m or h, 100 ms or more. The handler is async fn(Cx) -> Result<()>.
| You wrote | The error |
|---|---|
#[every] | write #[every("30s")] |
"50ms" | ticks are 100 ms or more |
"30" | "30" isn't a duration like 30s (ms, s, m or h) |
#[cron]
#[cron("0 2 * * *")]
#[cron("@daily", tz = "UTC")]A schedule: five fields (minute hour day month weekday), or @hourly, @daily, @midnight,
@weekly, @monthly, @yearly, @annually. It is read in Asia/Karachi unless tz names
another zone. The handler is async fn(Cx) -> Result<()>.
Floor keeps the schedule as the project's cron entry floor.<name> while a version that has it
is live. Each run is a job on the queue floor.cron.<name>, taken one at a time, so a run that
falls during a deploy or a restart still happens, on the new version.
| You wrote | The error |
|---|---|
#[cron] | write #[cron("0 2 * * *")] or #[cron("@daily", tz = "UTC")] |
"0 2 * *" | "0 2 * *" has 4 fields; a cron schedule has five: minute hour day month weekday |
"@sometimes" | "@sometimes" isn't a cron alias (@hourly, @daily, @weekly, @monthly, @yearly) |
The attribute counts the fields; Floor checks what's in them.
#[device]
#[device(online, offline)]
#[device]Device events: online, offline, health, config, command. No list means all of them.
The handler takes DeviceEvent. Every handler whose list has the event runs, in file order; the
first error stops the rest.
| You wrote | The error |
|---|---|
#[device(rebooted)] | "rebooted" isn't a device event (online, offline, health, config, command) |
#[event]
#[event("message.received")]
#[event("message.*", "alert.fired")]
#[event]The project's events, the ones its webhooks get: exact types, prefixes ending in .*, or "*".
No list means all of them. The handler takes Event<T>. Every handler whose list matches runs,
in file order; the first error stops the rest.
| You wrote | The error |
|---|---|
#[event("Message")] | "Message" isn't an event type like "message.received", a prefix like "message.", or "" |
#[job]
#[job(queue = "receipts")]
#[job(queue = "receipts", concurrency = 4)]Jobs from one of the project's queues, up to concurrency at a time (1 to 64, default 1). The
handler takes Job<T>. Ok completes the job; Err fails it, and the queue retries it with
backoff. Put jobs on the queue with cx.enqueue.
| You wrote | The error |
|---|---|
no queue | #[job] needs queue = "…" |
concurrency = 0 or more than 64 | concurrency is 1-64 |
| another key | #[job] takes queue = "…" and concurrency = n |
At start, the backend refuses a queue handled by two functions ("jobs on "receipts" have two
handlers") and a queue whose name starts with floor. ("names starting with floor. are Floor's
own").
#[background]
#[background]A long-running task in the live version: it starts once the backend is ready and gets a Stop
that fires when a newer version takes over or the backend stops. The handler is
async fn(Cx, Stop) -> Result<()>. It takes no settings: "#[background] takes nothing".
Any attribute
| You wrote | The error |
|---|---|
| the attribute on something that isn't a function | #[get] goes on a function |
a function that isn't pub async fn | #[get] handlers are pub async fn |
Paths from files
A route's path is its file's path under the crate, without .rs:
| File | Path |
|---|---|
api/health.rs | /api/health |
api/visits/index.rs | /api/visits |
api/visits/[id].rs | /api/visits/{id} |
api/visits/[id]/remarks.rs | /api/visits/{id}/remarks |
api/gates/[lane]/open.rs | /api/gates/{lane}/open |
api/files/[...path].rs | /api/files/{*path}: the rest of the path, slashes included |
api/index.rs | /api |
index.rsserves its folder's path.[name].rsor a[name]folder is a parameter, whichPath<T>reads.[...name].rstakes the rest of the path.- Other names are used as they are:
api/ocr-sweep.rsserves/api/ocr-sweep. things.rsnext tothings/is fine: the file serves/api/things, the folder's files the paths below it.- Files and folders starting with
_or.aren't read at all: not a route, not a module. Keep shared code undersrc/.
Trigger names
A tick's, cron handler's or background task's name is its file's path under on/ or jobs/
and its function's name, joined with .. Dashes in file names become _:
| File | Function | Name |
|---|---|---|
jobs/sweep.rs | sweep | sweep.sweep |
jobs/ocr-sweep.rs | sweep | ocr_sweep.sweep |
jobs/reports.rs | nightly | reports.nightly |
on/presence.rs | arbiter | presence.arbiter |
The top folder isn't part of the name. Tests run a tick or a cron handler by this name:
t.tick("sweep.sweep"), t.cron("reports.nightly").
A cron handler's name also names its cron entry (floor.reports.nightly) and its queue
(floor.cron.reports.nightly). It is lower-cased, any character other than a letter, a digit,
., _ or - becomes -, and it must be 1 to 53 characters. Two cron handlers with the same
name stop the backend at start.
MQTT, device and job handlers have no name: their filter, events or queue say what they take.
Build errors
The build step fails cargo build with a message that names the file:
| Mistake | The error |
|---|---|
a handler that isn't pub async fn | api/visits/index.rs: fn get must be pub async fn |
a route or WebSocket outside api/ | on/visits.rs: routes and WebSockets live in api/ |
a trigger in api/ | api/sweep.rs: #[every] belongs in on/ or jobs/, not api/ |
| two handlers on one method and path | GET /api/visits is served twice: by api::visits::index::get and api::visits::__self::get |
| a cron name over 53 characters | cron handler …: its name "…" must be 1-53 characters |
| a Rust migration named otherwise | db/migrations/plate.rs: a Rust migration is named _.rs, e.g. 0002_plate_loose.rs |
| a file that doesn't parse | the file and the parser's message |