Skip to content

Commit 83b37b4

Browse files
committed
server: respond with items html
1 parent 0b595ea commit 83b37b4

File tree

7 files changed

+181
-4
lines changed

7 files changed

+181
-4
lines changed

Cargo.lock

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
askama = "0.12"
78
axum = { version = "0.7", features = ["tracing"] }
89
axum-extra = { version = "0.9", features = ["typed-routing"] }
910
clap = { version = "4", features = ["derive"] }
11+
lazy_static = "1.5.0"
1012
tokio = { version = "1", features = [ "full", "tracing"] }
1113
tracing = "0.1"
1214
tracing-subscriber = "0.3"
15+
uuid = { version = "1", features = ["v4"] }
1316

1417
[dev-dependencies]
1518
ureq = "2"

server/src/item.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use askama::Template;
2+
use uuid::Uuid;
3+
4+
#[derive(Debug, Clone)]
5+
pub struct Item {
6+
pub id: Uuid,
7+
pub name: String,
8+
pub price: u128,
9+
}
10+
11+
#[derive(Template)]
12+
#[template(path = "items.html")]
13+
pub struct ItemsTemplate<'a> {
14+
pub items: &'a Vec<Item>,
15+
}

server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod cli;
2+
pub mod item;
23
pub mod routes;
34

45
use crate::cli::CliArgs;

server/src/routes.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
use axum::http::StatusCode;
1+
use askama::Template;
2+
use axum::response::Html;
23
use axum_extra::routing::TypedPath;
4+
use lazy_static::lazy_static;
5+
use std::sync::Mutex;
36
use tracing::instrument;
47

8+
use crate::item::{Item, ItemsTemplate};
9+
10+
lazy_static! {
11+
static ref ITEMS: Mutex<Vec<Item>> = Mutex::new(vec![Item {
12+
id: uuid::Uuid::new_v4(),
13+
name: "test".to_string(),
14+
price: 12345
15+
}]);
16+
}
17+
518
#[derive(TypedPath, Debug, Clone, Copy)]
619
#[typed_path("/api/v1/items")]
720
pub struct GetItemsPath;
821

922
#[instrument(level = "trace", ret)]
10-
pub async fn get_items_handler(_: GetItemsPath) -> (StatusCode, &'static str) {
11-
(StatusCode::OK, "Hello World!")
23+
pub async fn get_items_handler(_: GetItemsPath) -> Html<String> {
24+
let items = ITEMS.lock().unwrap();
25+
let template = ItemsTemplate { items: &items };
26+
Html(template.render().unwrap())
1227
}

server/templates/items.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<table>
2+
<tr>
3+
<th>Id</th>
4+
<th>Name</th>
5+
</tr>
6+
{% for item in items %}
7+
<tr>
8+
<td>{{ item.id }}</td>
9+
<td>{{ item.name }}</td>
10+
</tr>
11+
{% endfor %}
12+
</ul>

server/tests/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub async fn test_server_listens() {
3030
.unwrap()
3131
.into_string()
3232
.unwrap();
33-
assert_eq!(response, "Hello World!")
33+
assert!(response.contains("<table>"));
34+
assert!(response.contains("test"))
3435
})
3536
.await
3637
.unwrap();

0 commit comments

Comments
 (0)