-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Dec 9, 2024
1 parent
da961c7
commit 5080e9c
Showing
3 changed files
with
38 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
``` | ||
|
||
|
@@ -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() { | ||
|
@@ -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) |