Skip to content

Commit

Permalink
Hot reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 9, 2024
1 parent da961c7 commit 5080e9c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Run inside the supplied `devcontainer`. You'll need to fetch the template which

1. git submodule init
1. git submodule update
1. cd `rust-on-nails.com`
1. `cd rust-on-nails.com`
1. `zs` which is an alias for `zola serve --interface 0.0.0.0 --port 2222`

The access the site from http://localhost:2222
1 change: 1 addition & 0 deletions rust-on-nails.com/content/.spelling
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ markup.rs
- docs/full-stack-web/web-server.md
Actix
Tokio
justfile
- docs/ide-setup/introduction.md
Django
SkyTrace
Expand Down
49 changes: 36 additions & 13 deletions rust-on-nails.com/content/docs/full-stack-web/web-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ Make sure you're in the `crates/web-server` folder and add Axum to your `Cargo.t

```sh
cargo add [email protected] --no-default-features -F json,http1,tokio
cargo add tokio@1 --no-default-features -F macros,fs,rt-multi-thread
cargo add [email protected] --F form
cargo add tokio@1 --no-default-features -F macros,rt-multi-thread
cargo add [email protected] --no-default-features
cargo add [email protected]
cargo add serde@1 -F "derive"
cargo add --path ../db
```

Expand All @@ -120,10 +124,12 @@ And replace your `crates/web-server/src/main.rs` with the following
mod config;
mod errors;

use crate::errors::CustomError;
use axum::{extract::Extension, response::Json, routing::get, Router};
use std::net::SocketAddr;

use crate::errors::CustomError;
use axum::{routing::get, Extension, Json, Router};
use db::User;
use tower_livereload::LiveReloadLayer;

#[tokio::main]
async fn main() {
Expand All @@ -134,40 +140,57 @@ async fn main() {
// build our application with a route
let app = Router::new()
.route("/", get(users))
.layer(LiveReloadLayer::new())
.layer(Extension(config))
.layer(Extension(pool.clone()));

// run it
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
println!("listening on {}", addr);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service()).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}

async fn users(Extension(pool): Extension<db::Pool>) -> Result<Json<Vec<User>>, CustomError> {
let client = pool.get().await?;

let users = db::queries::users::get_users()
.bind(&client)
.all()
.await?;
let users = db::queries::users::get_users().bind(&client).all().await?;

Ok(Json(users))
}
```

## Watch the Server
## Watching for changes and Hot Reload

We could use `cargo run` to start our server but Rust on Nails comes with a built in alias that will watch your code for changes and restart your server.
We could use `cargo run` to start our server but ideally we'd like our server to re-start when we make changes and for the browser to reload itself.

It also uses a very fast linker called [Mold](https://github.com/rui314/mold) to speed up our incremental build times.
We've installed [Just](https://github.com/casey/just) which is a command runner.

Issue the following command to create a justfile with an entry to run our server.

```sh
echo -e 'watch:\n mold -run cargo watch --workdir /workspace/ -w crates/web-server -w crates/db --no-gitignore -x "run --bin web-server"' > Justfile
```

You should get a `Justfile`like the following.

```Justfile
watch:
mold -run cargo watch --workdir /workspace/ -w crates/web-server -w crates/db --no-gitignore -x "run --bin web-server"
```
Run the server

Issue the following command in your `app` folder.

```sh
cw
just watch
```

The server will run up. It's setup for hot reload so when you change the code, the browser will automatically update.

It also uses a very fast linker called [Mold](https://github.com/rui314/mold) to speed up our incremental build times.

And you should be able to point your browser at `http://localhost:3000` and see the web server deliver a plain text list of users.

![Users](/axum-screenshot.png)

0 comments on commit 5080e9c

Please sign in to comment.