The dev loop
Make a backend and its handlers with floor new, and run it against Floor with floor dev while you edit.
This page covers the commands you use while writing a backend: floor new to make one and add handlers, and floor dev to run it against Floor and rebuild it on every save. At the end you know what each does, which flags it takes, and how the running backend reaches Floor. You need the CLI signed in and a local Postgres, as in Install.
1. Make a backend
floor new gateIt makes a folder gate/ holding a crate that builds on its own: Cargo.toml, floor.toml, .cargo/config.toml, build.rs, src/lib.rs, src/main.rs, api/health.rs, permissions.rs, channels.rs, a first migration, a test, .gitignore and rust-toolchain.toml.
| Flag | Does |
|---|---|
--project <slug> | The project in floor.toml (default: the backend's name) |
--floor-path <dir> | Use the floor crate from a checkout of Floor instead of crates.io |
The name is lowercase letters, digits and dashes. .cargo/config.toml points Cargo at the registry of the Floor you're signed in to.
2. Add handlers
In the backend's folder, floor new route, job and mqtt write a handler file that compiles and does nothing yet:
floor new route visits --can visits:read # api/visits.rs: GET /api/visits
floor new route "visits/[id]" --method post --role admin # api/visits/[id].rs: POST
floor new route lanes/ --public # api/lanes/index.rs: GET /api/lanes
floor new job sweep --every 30s # jobs/sweep.rs
floor new job prune --cron "15 3 * * *" # jobs/prune.rs, nightly
floor new job receipts --queue receipts --concurrency 2 # jobs/receipts.rs
floor new mqtt events --filter "+/events" # on/mqtt/events.rsfloor new route <path> writes api/<path>.rs, or api/<path>/index.rs for a path ending in /. If the file exists, it adds the method to it.
| Flag | Does |
|---|---|
--method | get (default), post, put, patch or delete |
--public | Anyone may call it |
--role "admin, guard" | Callers with one of these roles |
--can visits:read | Callers whose roles permissions.rs allows it |
With none of the last three, the route is signed_in. Path parameters come as Path<String>; change the type to what you need.
floor new job <name> writes jobs/<name>.rs with one of:
| Flag | Writes |
|---|---|
--every 30s | A tick (ms, s, m, h) |
--cron "0 2 * * *" | A schedule in Asia/Karachi; also @hourly, @daily |
--queue receipts | A handler for a queue's jobs; --concurrency n (default 1) |
floor new mqtt <name> writes on/mqtt/<name>.rs, with --filter (default +/events).
See Routes and Triggers for what to put in them.
3. Run it against Floor
floor dev --project sac-devfloor dev: gate for sac-dev
link wss://usefloor.dev/v1/link
http http://127.0.0.1:3000
database 127.0.0.1:5432/gate_dev
client web/src/floor.gen.ts
✓ ready in 11.5 s (build 10.4 s)
INFO floor::request: GET /api/health 200 0.1 ms
INFO floor::trigger: mqtt sac-dev/dev-1/events ok 10.5 msUse a development environment of the project (sac-dev), not production. floord sends the project's triggers to whichever backend is live for it, and while floor dev runs, that's the one on your laptop.
| Flag | Does |
|---|---|
--project <slug> | The project or environment to run against (default: floor.toml's project) |
--port <n> | The backend's HTTP port (default 3000) |
--database-url <url> | Its database (default: <name>_dev on the local Postgres, made if missing). Also read from DATABASE_URL |
--link <address> | Where floord's link is (default: worked out from the endpoint you signed in to) |
--ts <file> | Where the TypeScript client goes (default: web/src/floor.gen.ts when web/ exists) |
--web | Also run the dashboard's dev server: bun run dev (or npm run dev) in web/, with FLOOR_BACKEND set to the backend's address |
The output streams the backend's log, with a line per request and per trigger: what it was, the status or ok, and the time.
4. Save, and it rebuilds
floor dev watches api/, on/, jobs/, src/, db/, permissions.rs, channels.rs, Cargo.toml and build.rs. It polls, so it works on WSL's /mnt drives too. On every save it:
- builds the backend;
- rewrites the dashboard's client (and
floor.zod.tsif it's there); - restarts the backend, which applies new migrations as it starts;
- says when the backend answers again.
↻ changed: rebuilding
✓ ready in 3.0 s (build 2.7 s)A failed build keeps the last good version running, with the compiler's errors above:
✗ the build failed: fix it and save (the last good version keeps running)On the gate, a change to what a route answers took 3.2 to 3.4 seconds from saving the file to the running backend answering with it, about 2.6 s of it the build.
5. How it reaches Floor
floor dev asks floord for a link token for dev:<this machine>, then runs the backend with it.
- floord on this machine (an endpoint on
127.0.0.1orlocalhost): the backend connects to floord's link directly. - floord elsewhere: over a WebSocket to its API,
wss://usefloor.dev/v1/link.
Either way the backend is live for that project: it gets its MQTT messages, device events, ticks and jobs, and its cx calls act on that project's data.
6. The database
floor dev uses a database on your machine, not the project's database on Floor:
--database-urlorDATABASE_URLwhen given;- else
<name>_devon the local Postgres (FLOOR_DEV_POSTGRES, defaultpostgres://floor:floor@127.0.0.1:5432/postgres), made if it isn't there.
It applies the SQL migrations before the first build, so checked queries build against them. The backend applies all migrations, SQL and Rust, each time it starts. See The database.
7. Stop it
Ctrl-C stops the backend and the dashboard's dev server. On Linux and macOS the backend gets SIGTERM first and finishes what's in flight.
Running the binary yourself
The backend is an ordinary binary. floor dev and Floor's deploys set its environment:
| Variable | |
|---|---|
FLOOR_LINK, FLOOR_LINK_TOKEN | floord's link and the backend's token. Without them: HTTP only, no triggers, and routes that need a signed-in caller refuse everyone |
PORT, or FLOOR_HTTP | HTTP on 127.0.0.1:$PORT (default 3000), or FLOOR_HTTP as a full address |
DATABASE_URL, FLOOR_DB_POOL | The database, and the pool's size (default 4) |
FLOOR_MIGRATE=1 | Apply db/migrations at start |
FLOOR_LOG | The log filter (default info,sqlx=warn) |
It also answers a few flags and exits:
gate --describe # the manifest: routes and who may call them, triggers, permissions, migrations
gate --migrate # apply db/migrations to DATABASE_URL, print each, exit
gate --ts web/src/floor.gen.ts # write the dashboard client (see the dashboard client page)/_floor/health answers ok once the backend serves. Deploys and floor dev wait for it.
Lifecycle: at start the backend connects, says which triggers it wants, serves HTTP and says it's ready. When a newer version takes over, or it gets SIGTERM, it stops taking new connections, stops its background tasks, finishes what's in flight (up to 30 seconds), and exits. If floord restarts, the backend connects again, and floord sends again whatever wasn't acked.
See also CLI and floor.toml.