This example creates a basic todo app with an Axum backend that uses Leptos' server functions to call SeaORM from the client and seamlessly run it on the server. The project basis is the Leptos Todo App Sqlite with Axum from the gbj/leptos GitHub repository.
This example cannot be built as a trunk standalone CSR-only app. Only the server may directly connect to the database.
cargo-leptos is now the easiest and most featureful way to build server side rendered apps with hydration. It provides automatic recompilation of client and server code, wasm optimisation, CSS minification, and more! Check out more about it here
- Install cargo-leptos
cargo install --locked cargo-leptos
- Build the site in watch mode, recompiling on file changes
cargo leptos watch
Open browser on http://localhost:3000/
- When ready to deploy, run
cargo leptos build --release
To run it as a server side app with hydration, you'll need to have wasm-pack installed.
- Edit the
[package.metadata.leptos]
section and setsite-root
to"."
. You'll also want to change the path of the<StyleSheet / >
component in the root component to point towards the CSS file in the root. This tells leptos that the WASM/JS files generated by wasm-pack are available at./pkg
and that the CSS files are no longer processed by cargo-leptos. Building to alternative folders is not supported at this time. You'll also want to edit the call toget_configuration()
to pass inSome(Cargo.toml)
, so that Leptos will read the settings instead of cargo-leptos. If you do so, your file/folder names cannot include dashes. - Install wasm-pack
cargo install wasm-pack
- Build the Webassembly used to hydrate the HTML from the server
wasm-pack build --target=web --debug --no-default-features --features=hydrate
- Run the server to serve the Webassembly, JS, and HTML
cargo run --no-default-features --features=ssr
This example includes quality checks and end-to-end testing.
To get started install Chromedriver for your platform. E.g. on macOS with Homebrew. We need to remove the quarantine attribute from the binary.
brew install --cask
xattr -d com.apple.quarantine $(which chromedriver)
To get started run this once.
cargo install cargo-make
cargo make ci
To only run the UI tests...
cargo make start-webdriver
cargo leptos watch # or cargo run...
cargo make test-ui
See the E2E README for more information about the testing strategy.
To work with SeaORM and extend the database, install the sea-orm-cli package by running the following command:
cargo install sea-orm-cli
To generate the entities from the existing database schema, run the following command:
sea-orm-cli generate entity -o src/entity
This command will discover all tables in the database and generate a corresponding SeaORM entity file for each table in the specified directory (src/entity).
See the SeaORM documentation for further information about migration and entity generation.
All credit for this example goes to authors and contributors of gbj/leptos GitHub repository, MIT License, Copyright 2022 Greg Johnston.