tangled
alpha
login
or
join now
cherry.computer
/
website
My personal site
cherry.computer
htmx
tailwind
axum
askama
0
fork
atom
overview
issues
pulls
pipelines
feat: allow media requests to be mocked
cherry.computer
3 months ago
d4c5b46a
3f881d6b
verified
This commit was signed with the committer's
known signature
.
cherry.computer
SSH Key Fingerprint:
SHA256:SIA77Ll0IpMb8Xd3RtaGT+PBIGIePhJJg5W2r6Td7cc=
+45
-11
2 changed files
expand all
collapse all
unified
split
server
src
index.rs
main.rs
+38
-7
server/src/index.rs
···
5
5
};
6
6
7
7
use askama::Template;
8
8
+
use serde::Deserialize;
9
9
+
10
10
+
#[derive(Deserialize)]
11
11
+
pub struct IndexOptions {
12
12
+
#[serde(default)]
13
13
+
mock: bool,
14
14
+
}
8
15
9
16
#[derive(Template, Debug, Clone)]
10
17
#[template(path = "index.html")]
···
13
20
}
14
21
15
22
impl RootTemplate {
16
16
-
pub async fn new(apple_music_client: &AppleMusicClient) -> RootTemplate {
17
17
-
let (game, movie, song) = tokio::join!(
18
18
-
backloggd::cached_fetch(),
19
19
-
letterboxd::cached_fetch(),
20
20
-
apple_music::cached_fetch(apple_music_client)
21
21
-
);
22
22
-
let media = [game, movie, song].into_iter().flatten().collect();
23
23
+
pub async fn new(apple_music_client: &AppleMusicClient, options: IndexOptions) -> RootTemplate {
24
24
+
let media = if options.mock {
25
25
+
mocked_media()
26
26
+
} else {
27
27
+
let (game, movie, song) = tokio::join!(
28
28
+
backloggd::cached_fetch(),
29
29
+
letterboxd::cached_fetch(),
30
30
+
apple_music::cached_fetch(apple_music_client)
31
31
+
);
32
32
+
[game, movie, song].into_iter().flatten().collect()
33
33
+
};
23
34
24
35
RootTemplate { media }
25
36
}
26
37
}
38
38
+
39
39
+
fn mocked_media() -> Vec<Media> {
40
40
+
vec![
41
41
+
Media {
42
42
+
name: "Cyberpunk 2077: Ultimate Edition".to_owned(),
43
43
+
image: "https://images.igdb.com/igdb/image/upload/t_cover_big/co7iy1.jpg".to_owned(),
44
44
+
context: "Nintendo Switch 2".to_owned(),
45
45
+
},
46
46
+
Media {
47
47
+
name: "The Thursday Murder Club".to_owned(),
48
48
+
image: "https://a.ltrbxd.com/resized/film-poster/6/6/6/2/8/6/666286-the-thursday-murder-club-0-230-0-345-crop.jpg?v=4bfeae38a7".to_owned(),
49
49
+
context: "1 star".to_owned(),
50
50
+
},
51
51
+
Media {
52
52
+
name: "We Might Feel Unsound".to_owned(),
53
53
+
image: "https://is1-ssl.mzstatic.com/image/thumb/Music124/v4/f4/b2/8e/f4b28ee4-01c6-232c-56a7-b97fd5b0e0ae/00602527857671.rgb.jpg/240x240bb.jpg".to_owned(),
54
54
+
context: "James Blake — James Blake".to_owned(),
55
55
+
},
56
56
+
]
57
57
+
}
+7
-4
server/src/main.rs
···
7
7
8
8
#[cfg(debug_assertions)]
9
9
use crate::am_auth_flow::AuthFlowTemplate;
10
10
-
use crate::index::RootTemplate;
10
10
+
use crate::index::{IndexOptions, RootTemplate};
11
11
use crate::scrapers::apple_music::AppleMusicClient;
12
12
13
13
use askama::Template;
14
14
use axum::{
15
15
Router,
16
16
-
extract::State,
16
16
+
extract::{Query, State},
17
17
http::{HeaderName, HeaderValue, StatusCode},
18
18
response::{Html, IntoResponse},
19
19
routing::{get, get_service},
···
59
59
Ok(())
60
60
}
61
61
62
62
-
async fn render_index_handler(State(state): State<AppState>) -> impl IntoResponse {
63
63
-
let template = RootTemplate::new(&state.apple_music_client).await;
62
62
+
async fn render_index_handler(
63
63
+
Query(options): Query<IndexOptions>,
64
64
+
State(state): State<AppState>,
65
65
+
) -> impl IntoResponse {
66
66
+
let template = RootTemplate::new(&state.apple_music_client, options).await;
64
67
template.render().map(Html).map_err(|err| {
65
68
tracing::error!("failed to render index: {err:?}");
66
69
StatusCode::INTERNAL_SERVER_ERROR