Skip to content

Commit cde44c9

Browse files
committed
add template
1 parent adfd014 commit cde44c9

File tree

11 files changed

+229
-3
lines changed

11 files changed

+229
-3
lines changed

Cargo.lock

Lines changed: 75 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ reqwest = { version = "0.12", features = ["json"] }
5050
aion-types = { version = "0.1.0", path = "./crates/aion-types" }
5151
aion-parse = { version = "0.1.0", path = "./crates/parse" }
5252
is-url = "1.0.4"
53+
askama = "0.12.1"
54+
askama_actix = "0.14.0"
5355

5456
[dependencies.sqlx]
5557
version = "0.7"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod configuration;
22
pub mod routes;
33
pub mod startup;
44
pub mod telemetry;
5+
pub mod templates;

src/routes/rebase/query_all.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::templates::all_news::AllNewsTemplate;
12
use actix_web::web;
23
use actix_web::HttpResponse;
34
use aion_types::rebase::response::{ListAllItemsResponse, SimpleDisplay};
5+
use askama_actix::TemplateToResponse;
46
use sqlx::query_as;
57
use sqlx::PgPool;
68

@@ -56,7 +58,14 @@ pub async fn list_all(pool: web::Data<PgPool>) -> HttpResponse {
5658
.await;
5759

5860
match result {
59-
Ok(items) => HttpResponse::Ok().json(items),
61+
Ok(items) => {
62+
let items = items
63+
.into_iter()
64+
.map(SimpleDisplay::from)
65+
.collect::<Vec<_>>();
66+
let template = AllNewsTemplate { items };
67+
template.to_response()
68+
}
6069
Err(e) => {
6170
tracing::error!("Failed to execute query: {:?}", e);
6271
HttpResponse::InternalServerError().finish()

src/routes/rebase/query_latest_news.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use crate::templates::latest_news::LatestNewsTemplate;
12
use actix_web::web;
23
use actix_web::HttpResponse;
34
use aion_types::rebase::response::ListAllItemsResponse;
5+
use aion_types::rebase::response::SimpleDisplay;
6+
use askama_actix::TemplateToResponse;
47
use sqlx::query_as;
58
use sqlx::PgPool;
69

@@ -14,7 +17,14 @@ pub async fn list_latest_news(conn_pool: web::Data<PgPool>) -> HttpResponse {
1417
.await;
1518

1619
match tags_result {
17-
Ok(items) => HttpResponse::Ok().json(items),
20+
Ok(items) => {
21+
let items = items
22+
.into_iter()
23+
.map(SimpleDisplay::from)
24+
.collect::<Vec<_>>();
25+
let template = LatestNewsTemplate { items };
26+
template.to_response()
27+
}
1828
Err(e) => {
1929
tracing::error!("Failed to execute query: {:?}", e);
2030
HttpResponse::InternalServerError().finish()

src/templates/all_news.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use aion_types::rebase::response::SimpleDisplay;
2+
use askama_actix::Template;
3+
4+
#[derive(Template)] // this will generate the code...
5+
#[template(path = "all_news.html")] // using the template in this path, relative
6+
// to the `templates` dir in the crate root
7+
pub struct AllNewsTemplate {
8+
pub items: Vec<SimpleDisplay>,
9+
}

src/templates/latest_news.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use aion_types::rebase::response::SimpleDisplay;
2+
//use askama::Template;
3+
use askama_actix::Template;
4+
5+
#[derive(Template)] // this will generate the code...
6+
#[template(path = "latest_news.html")] // using the template in this path, relative
7+
// to the `templates` dir in the crate root
8+
pub struct LatestNewsTemplate {
9+
pub items: Vec<SimpleDisplay>,
10+
}

src/templates/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod all_news;
2+
pub mod latest_news;

templates/all_news.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Rebase All News</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
background-color: #f4f4f9;
10+
color: #333;
11+
margin: 0;
12+
padding: 20px;
13+
}
14+
h1 {
15+
color: #5a5a5a;
16+
}
17+
ul {
18+
list-style-type: none;
19+
padding: 0;
20+
}
21+
li {
22+
background-color: #fff;
23+
border: 1px solid #ddd;
24+
padding: 10px;
25+
margin-bottom: 10px;
26+
border-radius: 8px;
27+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
28+
}
29+
a {
30+
color: #0066cc;
31+
text-decoration: none;
32+
}
33+
a:hover {
34+
text-decoration: underline;
35+
}
36+
strong {
37+
color: #333;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<h1>All News</h1>
43+
<ul>
44+
{% for item in items %}
45+
<li>
46+
<strong>Title:</strong> {{ item.title }}<br />
47+
<strong>Introduce:</strong> {{ item.introduce }}<br />
48+
<strong>URL:</strong>
49+
<a href="{{ item.url }}">{{ item.url }}</a>
50+
</li>
51+
{% endfor %}
52+
</ul>
53+
</body>
54+
</html>

templates/hello.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, {{ name }}!

templates/latest_news.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Rebase Latest News</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
background-color: #f4f4f9;
10+
color: #333;
11+
margin: 0;
12+
padding: 20px;
13+
}
14+
h1 {
15+
color: #5a5a5a;
16+
}
17+
ul {
18+
list-style-type: none;
19+
padding: 0;
20+
}
21+
li {
22+
background-color: #fff;
23+
border: 1px solid #ddd;
24+
padding: 10px;
25+
margin-bottom: 10px;
26+
border-radius: 8px;
27+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
28+
}
29+
a {
30+
color: #0066cc;
31+
text-decoration: none;
32+
}
33+
a:hover {
34+
text-decoration: underline;
35+
}
36+
strong {
37+
color: #333;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<h1>Latest News</h1>
43+
<ul>
44+
{% for item in items %}
45+
<li>
46+
<strong>Title:</strong> {{ item.title }}<br />
47+
<strong>Introduce:</strong> {{ item.introduce }}<br />
48+
<strong>URL:</strong>
49+
<a href="{{ item.url }}">{{ item.url }}</a>
50+
</li>
51+
{% endfor %}
52+
</ul>
53+
</body>
54+
</html>

0 commit comments

Comments
 (0)