Skip to content

Commit a486df2

Browse files
committed
Initialize the Server
1 parent a0ebf7c commit a486df2

83 files changed

Lines changed: 294 additions & 41 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

content/en/labs/a2.initialize-the-server.md

Lines changed: 166 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ These steps will generate the following folder structure,
2929
└── app.rs
3030
```
3131

32-
{{< tabs count=3 label="Select File" >}}
33-
{{< tab label="app.rs" >}}
32+
{{< tabs count=3 label="Select File" >}} {{< tab label="app.rs" >}}
33+
3434
```rust {title="crates/book_service/src/bin/app.rs"}
3535
fn main() {
3636
println!("Hello, world!");
3737
}
3838
```
39-
{{< /tab >}}
40-
{{< tab label="Cargo.toml" >}}
39+
40+
{{< /tab >}} {{< tab label="Cargo.toml" >}}
41+
4142
```rust {title="crates/book_service/Cargo.toml"}
4243
[package]
4344
name = "book_service"
@@ -51,15 +52,16 @@ path = "src/bin/app.rs"
5152

5253
[dependencies]
5354
```
54-
{{< /tab >}}
55-
{{< tab label="Root Cargo.toml" >}}
55+
56+
{{< /tab >}} {{< tab label="Root Cargo.toml" >}}
57+
5658
```toml {title="Cargo.toml"}
5759
[workspace]
5860
resolver = "3"
5961
members = ["crates/book_service"]
6062
```
61-
{{< /tab >}}
62-
{{< /tabs >}}
63+
64+
{{< /tab >}} {{< /tabs >}}
6365

6466
> [!tip]
6567
> You should see `Hello, world!` while running `cargo run -p book_service` from the project root.
@@ -92,7 +94,7 @@ Add a new file `.gitattributes` to the project root.
9294
> git commit -m "Create New Workspace"
9395
> ```
9496
95-
## Add `rustfmt.toml`
97+
## Configure `rustfmt`
9698
9799
Add a new file `rustfmt.toml` to the project root.
98100
@@ -118,6 +120,48 @@ format_code_in_doc_comments = true
118120
format_macro_matchers = true
119121
```
120122
123+
> [!tip]
124+
> Most above features have not been stable for years. And by default Rust makes, unstable features are only available in
125+
nightly channel. So, we need to:
126+
> - Run `rustup component add rustfmt --toolchain nightly` to install `rustfmt` on nightly toolchain.
127+
> - Then `cargo +nightly fmt --all --check` to run `rustfmt` on nightly toolchain, with unstable features.
128+
129+
## Configure `clippy`
130+
131+
Add the following configurations to the `book_service/Cargo.toml`.
132+
133+
```toml
134+
[lints]
135+
workspace = true
136+
```
137+
138+
Add the following clippy configurations to the `Cargo.toml` file in the project root.
139+
140+
```toml
141+
[workspace.lints.clippy]
142+
doc_markdown = "warn"
143+
manual_let_else = "warn"
144+
match_same_arms = "warn"
145+
redundant_closure_for_method_calls = "warn"
146+
redundant_else = "warn"
147+
semicolon_if_nothing_returned = "warn"
148+
type_complexity = "allow"
149+
undocumented_unsafe_blocks = "warn"
150+
unwrap_or_default = "warn"
151+
needless_lifetimes = "allow"
152+
too_many_arguments = "allow"
153+
nonstandard_macro_braces = "warn"
154+
print_stdout = "warn"
155+
print_stderr = "warn"
156+
157+
ptr_as_ptr = "warn"
158+
ptr_cast_constness = "warn"
159+
ref_as_ptr = "warn"
160+
161+
allow_attributes = "warn"
162+
allow_attributes_without_reason = "warn"
163+
```
164+
121165
## Add Initial Dependency Crates
122166

123167
Run following `cargo add` commands from the `book_service` folder:
@@ -158,7 +202,6 @@ Update `book_service/src/bin/app.rs`
158202

159203
```rust
160204
use book_service::routes;
161-
162205
use tracing::info;
163206
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
164207

@@ -187,12 +230,121 @@ async fn main() -> anyhow::Result<()> {
187230
Run `cargo run -p book_service` and you should see our initial log `"Starting server"`.
188231

189232
```json
190-
{"timestamp":"2027-01-01T00:00:00.123456Z","level":"INFO","fields":{"message":"Starting server","addr":"0.0.0.0:3000"}}
233+
{
234+
"timestamp": "2027-01-01T00:00:00.123456Z",
235+
"level": "INFO",
236+
"fields": {
237+
"message": "Starting server",
238+
"addr": "0.0.0.0:3000"
239+
}
240+
}
191241
```
192242

193243
Run `curl -i 'http://localhost:3000/livez'` and you should see `HTTP/1.1 200 OK` in its response.
194244

195245
> [!tip]
196-
> - We use [`tracing_subscriber::EnvFilter`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html) to filter spans and events based the given set of filter directives, \
197-
> `"info,tower_http=warn,axum=warn,toasty=warn,tokio_postgres=warn"`
198-
> - Change it (log level) to just `"trace"`, rerun the application, and trigger the `curl` request. Now, you can see all trace logs generated by Axum and 3rd party crates.
246+
> - We use [
247+
`tracing_subscriber::EnvFilter`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html)
248+
to filter spans and events based the given set of filter directives, \
249+
> `"info,tower_http=warn,axum=warn,toasty=warn,tokio_postgres=warn"`
250+
> - Change it (log level) to just `"trace"`, rerun the application, and trigger the `curl` request. Now, you can see all
251+
trace logs generated by Axum and 3rd party crates.
252+
253+
## Add Initial justfiles
254+
255+
> [!tip]
256+
> To improve developer productivity, we use [`Just`](https://github.com/casey/just#installation), a modern command
257+
runner and alternative to Make.
258+
259+
Add a new file `justfile` to the project root.
260+
261+
```justfile
262+
# List available commands
263+
help:
264+
@just --list --unsorted --list-prefix ' ' --list-heading $'RESTful API Workspace\n'
265+
@echo ''
266+
@just --list --unsorted --list-prefix ' ' --list-heading $' BOOK SERVICE\n' --justfile crates/book_service/justfile
267+
268+
# Run lints on the workspace members (cargo fmt and clippy)
269+
lint:
270+
cargo +nightly fmt --all --check
271+
cargo clippy --workspace --all-targets -- -D warnings
272+
273+
# Run cargo check on the workspace members
274+
check:
275+
cargo check --workspace
276+
277+
# Run cargo build on the workspace members
278+
build:
279+
cargo build --workspace --all-targets
280+
281+
# Run cargo clean on the workspace members
282+
clean:
283+
cargo clean
284+
285+
# Run cargo test on the workspace members
286+
test:
287+
cargo test --workspace
288+
289+
# Forward to the BOOK-SERVICE
290+
mod book "crates/book_service"
291+
```
292+
293+
Add a new file `book_service/justfile`.
294+
295+
```justfile
296+
export CARGO_TARGET_DIR := "../../target"
297+
298+
# List available commands
299+
help:
300+
@just --list --unsorted --list-heading $'📖BOOK-SERVICE\n'
301+
302+
# Run lints (cargo fmt and clippy)
303+
lint:
304+
cargo +nightly fmt --package book_service -- --check
305+
cargo clippy --all-targets -- -D warnings
306+
307+
# Run cargo check
308+
check:
309+
cargo check
310+
311+
# Run cargo build
312+
build:
313+
cargo build --all-targets
314+
315+
# Run cargo clean on the book_service package
316+
clean:
317+
cargo clean --package book_service
318+
319+
# Run cargo test
320+
test:
321+
cargo test
322+
323+
# Run server app
324+
app:
325+
cargo run --bin app
326+
```
327+
328+
> [!tip]
329+
> Run `just` from the project root to view all available commands.
330+
>
331+
> ```shell
332+
> RESTful API Workspace
333+
> help # List available commands
334+
> lint # Run lints on the workspace members (cargo fmt and clippy)
335+
> check # Run cargo check on the workspace members
336+
> build # Run cargo build on the workspace members
337+
> clean # Run cargo clean on the workspace members
338+
> test # Run cargo test on the workspace members
339+
> book ... # Forward to the BOOK-SERVICE
340+
>
341+
> BOOK SERVICE
342+
> help # List available commands
343+
> lint # Run lints (cargo fmt and clippy)
344+
> check # Run cargo check
345+
> build # Run cargo build
346+
> clean # Run cargo clean on the book_service package
347+
> test # Run cargo test
348+
> app # Run server app
349+
> ```
350+
> For example: `just book lint` to run `cargo fmt` and `clippy` on the `book_service` package.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html><html lang=en-US><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="Rust Programming Language Tutorials for Everyone!"><meta name=author content="Dumindu Madunuwan"><meta name=theme-color content="#ffffff" media="(prefers-color-scheme: light)"><meta name=theme-color content="#101010" media="(prefers-color-scheme: dark)"><title>Learning Rust · Rust Programming Language Tutorials for Everyone!</title><link rel=canonical href=https://learning-rust.github.io/><link rel=stylesheet href=/assets/css/home.min.2f64b534694f7783ee00c07c45f130ef260d59af33aefe9e41a6606a53c5de38.css integrity><link href=https://learning-rust.github.io/pagefind/pagefind-component-ui.css rel=stylesheet><link rel=manifest href=/manifest.json><link rel=icon href=/favicon/favicon.ico><link rel=icon href=/favicon/favicon-16x16.png sizes=16x16 type=image/png><link rel=icon href=/favicon/favicon-32x32.png sizes=32x32 type=image/png><link rel=apple-touch-icon href=/favicon/apple-touch-icon.png sizes=180x180><script async src="https://www.googletagmanager.com/gtag/js?id=G-FZHQCXSZ89"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-FZHQCXSZ89")</script><meta property="og:title" content="Learning Rust"><meta property="og:description" content="Learning Rust - Rust Programming Language Tutorials for Everyone!"><meta property="og:type" content="article"><meta property="og:url" content="https://learning-rust.github.io/"><meta property="og:image" content="https://learning-rust.github.io/og.jpg"><meta property="og:site_name" content="Learning Rust"><meta name=twitter:card content="summary_large_image"><meta name=twitter:title content="Learning Rust"><meta name=twitter:description content="Learning Rust - Rust Programming Language Tutorials for Everyone!"><meta name=twitter:image content="https://learning-rust.github.io/og.jpg"><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","headline":"Learning Rust","description":"Learning Rust - Rust Programming Language Tutorials for Everyone!","image":"https:\/\/learning-rust.github.io\/og.jpg","author":{"@type":"Person","name":"Dumindu Madunuwan","url":"https:\/\/github.com\/dumindu"},"publisher":{"@type":"Organization","name":"Learning Rust","logo":{"@type":"ImageObject","url":"https:\/\/learning-rust.github.io\/"}},"dateModified":"2026-08-12T22:30:58Z","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/learning-rust.github.io\/"}}</script></head><body><div id=content-wrapper><header id=site-header><div id=site-header-logo><a href=https://learning-rust.github.io/><img height=52px src=https://learning-rust.github.io/logo.svg alt="Site Logo"></a></div><div id=site-header-actions><div id=search-box><pagefind-modal-trigger compact=true></pagefind-modal-trigger><pagefind-modal></pagefind-modal></div><div id=theme-dropdown class=dropdown><button class=dropdown-btn aria-haspopup=menu aria-label="Select the theme">
1+
<!doctype html><html lang=en-US><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="Rust Programming Language Tutorials for Everyone!"><meta name=author content="Dumindu Madunuwan"><meta name=theme-color content="#ffffff" media="(prefers-color-scheme: light)"><meta name=theme-color content="#101010" media="(prefers-color-scheme: dark)"><title>Learning Rust · Rust Programming Language Tutorials for Everyone!</title><link rel=canonical href=https://learning-rust.github.io/><link rel=stylesheet href=/assets/css/home.min.2f64b534694f7783ee00c07c45f130ef260d59af33aefe9e41a6606a53c5de38.css integrity><link href=https://learning-rust.github.io/pagefind/pagefind-component-ui.css rel=stylesheet><link rel=manifest href=/manifest.json><link rel=icon href=/favicon/favicon.ico><link rel=icon href=/favicon/favicon-16x16.png sizes=16x16 type=image/png><link rel=icon href=/favicon/favicon-32x32.png sizes=32x32 type=image/png><link rel=apple-touch-icon href=/favicon/apple-touch-icon.png sizes=180x180><script async src="https://www.googletagmanager.com/gtag/js?id=G-FZHQCXSZ89"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-FZHQCXSZ89")</script><meta property="og:title" content="Learning Rust"><meta property="og:description" content="Learning Rust - Rust Programming Language Tutorials for Everyone!"><meta property="og:type" content="article"><meta property="og:url" content="https://learning-rust.github.io/"><meta property="og:image" content="https://learning-rust.github.io/og.jpg"><meta property="og:site_name" content="Learning Rust"><meta name=twitter:card content="summary_large_image"><meta name=twitter:title content="Learning Rust"><meta name=twitter:description content="Learning Rust - Rust Programming Language Tutorials for Everyone!"><meta name=twitter:image content="https://learning-rust.github.io/og.jpg"><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","headline":"Learning Rust","description":"Learning Rust - Rust Programming Language Tutorials for Everyone!","image":"https:\/\/learning-rust.github.io\/og.jpg","author":{"@type":"Person","name":"Dumindu Madunuwan","url":"https:\/\/github.com\/dumindu"},"publisher":{"@type":"Organization","name":"Learning Rust","logo":{"@type":"ImageObject","url":"https:\/\/learning-rust.github.io\/"}},"dateModified":"2026-08-13T15:44:54Z","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/learning-rust.github.io\/"}}</script></head><body><div id=content-wrapper><header id=site-header><div id=site-header-logo><a href=https://learning-rust.github.io/><img height=52px src=https://learning-rust.github.io/logo.svg alt="Site Logo"></a></div><div id=site-header-actions><div id=search-box><pagefind-modal-trigger compact=true></pagefind-modal-trigger><pagefind-modal></pagefind-modal></div><div id=theme-dropdown class=dropdown><button class=dropdown-btn aria-haspopup=menu aria-label="Select the theme">
22
<span><svg class="icon" height="20" viewBox="0 -960 960 960" width="20"><path d="M480-96q-79 0-149-30t-122.5-82.5T126-331 96-480q0-80 30.5-149.5t84-122 125-82.5T488-864q78 0 146.5 27T754-763t80.5 110T864-518q0 96-67 163t-163 67h-68q-8 0-14 5t-6 13q0 15 15 25t15 53q0 37-27 66.5T480-96zm0-384zm-173.5 18.5Q324-479 324-504t-17.5-42.5T264-564t-42.5 17.5T204-504t17.5 42.5T264-444t42.5-17.5zm120-144Q444-623 444-648t-17.5-42.5T384-708t-42.5 17.5T324-648t17.5 42.5T384-588t42.5-17.5zm192 0Q636-623 636-648t-17.5-42.5T576-708t-42.5 17.5T516-648t17.5 42.5T576-588t42.5-17.5zm120 144Q756-479 756-504t-17.5-42.5T696-564t-42.5 17.5T636-504t17.5 42.5T696-444t42.5-17.5zM480-168q11 0 17.5-8.5T504-192q0-16-15-28t-15-50 26.5-64 64.5-26h69q66 0 112-46t46-112q0-115-88.5-194.5T488-792q-134 0-227 91t-93 221 91 221 221 91z"/></svg>
33
</span><span><svg class="icon" height="20" viewBox="0 -960 960 960" width="20"><path d="m480-246 85.91-85.91Q577-343 591.5-343t25.5 11 11 25.5-10.93 25.43L505-169q-5.4 5-11.7 7.5t-13.5 2.5-13.5-2.5T455-169L342.93-281.07Q332-292 332.5-306.5T344-332t25.5-11 25.47 11.09L480-246zm0-468-85.91 85.91Q383-617 368.5-617T343-628t-11-25.5 10.93-25.43L455-791q5.4-5 11.7-7.5t13.5-2.5 13.5 2.5T505-791l112.07 112.07Q628-668 628-654t-11 25-25.5 11-25.59-10.97L480-714z"/></svg></span></button><ul role=menu class=dropdown-menu><li role=menuitem><button title=Light class=color-scheme data-value=light style=background:#fff;color:#000>
44
<span style=color:#000>Aa</span></button></li><li role=menuitem><button title=dark class=color-scheme data-value=dark style=background:#101010;color:#fff>

docs/index.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,7 @@ However, it&amp;rsquo;s good practice to specify the type on variables when usin
603603

604604

605605
&lt;div class="tabs" style="--tabs-count: 3;" aria-label="Select File" &gt;
606-
607-
606+
608607
&lt;details name="tabs-1" style="--n: 1" open&gt;
609608
&lt;summary data-index="app.rs"&gt;
610609
app.rs
@@ -637,8 +636,7 @@ However, it&amp;rsquo;s good practice to specify the type on variables when usin
637636

638637
&lt;/div&gt;
639638
&lt;/details&gt;
640-
641-
639+
642640
&lt;details name="tabs-1" style="--n: 2" open&gt;
643641
&lt;summary data-index="Cargo.toml"&gt;
644642
Cargo.toml
@@ -679,8 +677,7 @@ However, it&amp;rsquo;s good practice to specify the type on variables when usin
679677

680678
&lt;/div&gt;
681679
&lt;/details&gt;
682-
683-
680+
684681
&lt;details name="tabs-1" style="--n: 3" open&gt;
685682
&lt;summary data-index="Root Cargo.toml"&gt;
686683
Root Cargo.toml
@@ -713,8 +710,7 @@ However, it&amp;rsquo;s good practice to specify the type on variables when usin
713710

714711
&lt;/div&gt;
715712
&lt;/details&gt;
716-
717-
713+
718714
&lt;/div&gt;
719715

720716

docs/labs/index.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757

5858

5959
&lt;div class="tabs" style="--tabs-count: 3;" aria-label="Select File" &gt;
60-
61-
60+
6261
&lt;details name="tabs-1" style="--n: 1" open&gt;
6362
&lt;summary data-index="app.rs"&gt;
6463
app.rs
@@ -91,8 +90,7 @@
9190

9291
&lt;/div&gt;
9392
&lt;/details&gt;
94-
95-
93+
9694
&lt;details name="tabs-1" style="--n: 2" open&gt;
9795
&lt;summary data-index="Cargo.toml"&gt;
9896
Cargo.toml
@@ -133,8 +131,7 @@
133131

134132
&lt;/div&gt;
135133
&lt;/details&gt;
136-
137-
134+
138135
&lt;details name="tabs-1" style="--n: 3" open&gt;
139136
&lt;summary data-index="Root Cargo.toml"&gt;
140137
Root Cargo.toml
@@ -167,8 +164,7 @@
167164

168165
&lt;/div&gt;
169166
&lt;/details&gt;
170-
171-
167+
172168
&lt;/div&gt;
173169

174170

0 commit comments

Comments
 (0)