Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Your first panel

This chapter builds a complete admin in one file: a Book model, a resource for it, and the main that serves it with a login page. examples/quickstart is this app with writes opened and the stylesheet wired in.

Dependencies

An app depends on the tablo facade and picks its database driver with a tablo feature. It names topcoat and toasty directly for their macros, at the versions Tablo uses:

[dependencies]
tablo = { version = "0.11.1", features = ["sqlite"] }
topcoat = { version = "0.12", default-features = false, features = ["view", "router", "discover", "serve", "font", "font-fontsource", "asset"] }
toasty = { version = "0.11", default-features = false, features = ["jiff"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
uuid = "1.23"

The toolkit enables no driver itself: sqlite, postgresql, mysql and turso each turn on Toasty’s driver of the same name. The [workspace.dependencies] of Tablo’s Cargo.toml hold the pinned versions. tablo is the only Tablo dependency the app names, unless it builds its own stylesheet (The stylesheet): every path in this guide starts with tablo::, and the derives work through it.

The app

use tablo::{auth::create_admin, prelude::*};
use toasty::Db;
use topcoat::{
    Result,
    router::{Router, RouterBuilderDiscoverExt},
};

/// The model: every column the panel renders comes from this type, and
/// `ModelForm` writes `BookForm`, what the create and edit forms parse into.
#[derive(Debug, Clone, toasty::Model, tablo::PanelModel, tablo::ModelForm)]
pub struct Book {
    #[key]
    #[auto]
    pub id: uuid::Uuid,
    pub title: String,
}

pub struct BookResource;

impl Resource for BookResource {
    type Model = Book;
    type Form = BookForm;

    // The default policy denies everything; this one opens the list and
    // the records, and nothing else.
    fn declare() -> ResourceDef<Self> {
        ResourceDef::new().policy(ReadOnly)
    }
}

#[tokio::main]
pub async fn main() -> Result<()> {
    let mut db = Db::builder()
        // `Book`, and the tables Tablo ships: the login's users and sessions among them.
        .models(tablo::models!(Book))
        .connect("sqlite::memory:")
        .await?;
    db.push_schema().await?;

    // One account to sign in with.
    create_admin(&mut db, "admin@example.com", "secret", "Admin").await?;

    let mut builder = Router::builder().discover().with(db);
    builder.panel(Panel::new("admin").resource::<BookResource>())?;
    let router = builder.build();

    topcoat::start(router).await?;
    Ok(())
}

Run it and sign in as admin@example.com / secret:

cargo run
# open http://127.0.0.1:3000/admin/books

What each part does

  • tablo::PanelModel gives Book its default table and detail page: a column per field a person reads, so id, which Toasty generates, is left out. See Tables.
  • tablo::ModelForm writes BookForm, the record form a submission parses into: one field per model field a person fills, so id is left out. The record form also lays out the form. See Forms.
  • impl Resource declares the panel’s pages for one model. A resource must name Model and Form; every other item has a default, and the default policy denies everything. See Resources.
  • tablo::models!(..) lists every model Toasty maps: the app’s, then the ones Tablo ships, the built-in login’s users and sessions among them. Built with toasty::models! instead, a Db missing one fails the mount with an error naming it.
  • db.push_schema() creates the tables, which suits a prototype. A production app runs toasty-cli migrations instead.
  • Router::builder().discover() starts the app’s own router: the app owns it, and the panel is one part of it. with(db) gives it the database every handler reads.
  • Panel::new("admin") is a panel at /admin, framed in the shell — sidebar, topbar, toasts — by a layout it registers itself.
  • resource::<BookResource>() registers the resource’s routes under /admin/books, adds its sidebar entry, and, because the panel has no home page, makes /admin redirect to the book list. Panel and routing lists every route.
  • .panel(..) checks every declaration and mounts the panel, or returns a MountError listing every mistake it found. build() returns the Router.
  • topcoat::start serves the router on HOST:PORT (default 127.0.0.1:3000) until Ctrl+C or SIGTERM.

What the example leaves out

  • Writes. ReadOnly allows only listing and viewing, so the create and export links are hidden, the create and edit pages answer 403, and rows show no Edit or Delete action. Policy, auth, tenancy opens them.
  • Styling. The example registers no asset bundle, so pages render unstyled and without the shell’s scripts, but every page and form works. The router’s .assets(..) and the panel’s shell_assets add the stylesheet, the font and the scripts; see Panel and routing. The stylesheet itself comes from the build script below.

The stylesheet

The panel’s markup carries Tailwind classes. tablo::ui::DEFAULT_STYLESHEET is Tablo’s prebuilt stylesheet: every class Tablo writes, on the default theme. Hand it to Panel::shell_assets and the app needs no build script and no Tailwind:

#![allow(unused)]
fn main() {
pub fn asset_panel(db: Db, bundle: AssetBundle) -> Panel {
    let _router = Router::builder().discover().with(db).assets(bundle);
    Panel::new("admin").shell_assets(tablo::ui::DEFAULT_STYLESHEET, GEIST)
}
}

It covers none of the app’s own classes. An app that writes Tailwind classes in its own pages, or changes a theme token, builds its own stylesheet instead, with tablo-build under [build-dependencies]:

[build-dependencies]
tablo-build = "0.11.1"

The app owns styles.css at its package root. The build imports Tailwind and Tablo’s default theme ahead of it, so the file names the app’s own sources and redeclares only the tokens it changes:

@source "./src/**/*.rs";

:root {
  --primary: oklch(0.45 0.18 265);
}

Theme tokens lists every token; each is declared under :root and again under .dark, in crates/tablo-build/src/theme/.

build.rs runs the build:

fn main() {
    println!("cargo::rerun-if-changed=build.rs");
    println!("cargo::rerun-if-changed=src");
    tablo_build::tailwind().expect("the Tailwind build runs");
}

tablo_build::tailwind() adds Tablo’s own sources to the build, wherever Cargo unpacked them, and writes $OUT_DIR/tailwind.css, which tailwind::stylesheet!() (under topcoat’s tailwind feature) hands to Panel::shell_assets in place of the prebuilt one. The stylesheet names no path into Tablo. The build downloads the Tailwind CLI on first run.