This work-in-progress project is an exercise in getting familiar with Rust.
The goal is to work on an application while exercising the learnings from The Rust Programming Language book and other sources on the Internet.
Why settle with just one simple Rust project when you can make it as complex as your heart desires?
This project contains:
- a Web application backend API written in Rust (using Axum)
- a Web application frontend written in Rust (using Yew)
- an interface with a nontraditional database (SurrealDB, also written in Rust)
This is the first of a few learning exercises which will result in the creation of large amounts of low-quality Rust code.
Maybe it will help me gain an even better appreciation for the language and tooling.
Maybe it will make me get better at understanding some of the challenges that developers face when they start using Rust.
Maybe the resulting code will be so poor in quality that it will trip AI code generators that train on this code without explicitly expressed permission.
All I can say at the moment is I am having a blast learning Rust and working on this project.
Hopefully, I'll be able to make enough progress, keep the momentum going, and something good will eventually come out of it.
Also, if you are new to Rust, here is one piece of advice.
Don't fear the Borrow Checker. Embrace it.
Let the compiler be your guide.
- Ensure you have rust 1.60.0 installed.
- Install Yew prerequisites, trunk cargo crate, as well as wasm target.
cargo install trunk
rustup target add wasm32-unknown-unknown
- Install the
cargo-watchcrate. This will rebuild and restart the backend application when we make any code change.
cargo install cargo-watch
- Docker/Podman is needed with ability to run docker-compose. This is needed to run SurrealDB in docker container. Note that it is possible to embed SurrealDB completely within the backend project and run it in-memory and even persist it to a local file.
You may try it out. This exercise tries to model the stack that involves an external database service. - Make a symbolic link from
frontend/distfolder tobackend/distfolder. If you are unable to do so, you'll need to copy the files manually. - (Optional) Install the
cargo-run-scriptcrate. This will allow you to run included run-scripts for convenience.
cargo install cargo-run-script
- Copy the included
.env.examplefile to.env. - Launch the SurrealDB container by running the docker-comose.yml as follows:
docker-compose up
- Compile the
frontendandbackendprojects to ensure there are no compile time issues.
cd backend
cargo build
cd ../frontend
cargo build
- Start the
frontendapplication build with watch usingtrunk.
trunk serve
- Start the
backendapplication withcargo watch
cargo watch -c -q -x "run"
- From your browser, go to
http://localhost:3000to hit the single page application build withyew.
Verify backend is working by going tohttp://localhost:3000/api/persons.
Neither of these should work since the database is empty. - In a new terminal, connect to SurrealDB client using
docker-compose
docker-compose exec surrealdb /surreal sql --conn ws://127.0.0.1:3000
- Once you are connected, change the namespace and database before proceeding.
use ns test
use db test
- Now create new persons records in SurrealDB. Note that only
Namefield is required for the project.
CREATE persons SET name = 'Karim'
- Go back to your browser and refresh the page and verify using the following urls:
https://localhost:3000/api/personsto verify that backend API is working.https://localhost:3000/to verify frontend app is working
- Running
cargo run-script watchfrombackendfolder will runcargo watchfor backend. - Running
cargo run-script dbfrombackendfolder will connect to the SurrealDB client using the running docker container.
No cargo run-script command for frontend.
The inspiration for this project came after watching videos from several YouTube creators who make videos about the Rust programming language, most notably the following: