Skip to content

Instantly share code, notes, and snippets.

@ddprrt
Created June 16, 2023 11:28
Show Gist options
  • Save ddprrt/f46956f76e056e358a3ab29af511f6c0 to your computer and use it in GitHub Desktop.
Save ddprrt/f46956f76e056e358a3ab29af511f6c0 to your computer and use it in GitHub Desktop.
Launchpad 2 Extra material
impl Podcast {
// ...
fn to_html(&self) -> String {
format!(
r#"
<html>
<head>
<title>My Podcast: {}</title>
</head>
<body>
<h1>{}</h1>
<p>{}</p>
<audio controls src="{}"></audio>
</body>
</html>
"#,
self.title,
self.title,
self.description,
match self.audio_file {
Some(ref file) => file,
None => "No audio available",
}
)
}
}
async fn root(
State(app_state): State<AppState>
) -> impl IntoResponse {
let response = format!(
r#"
<html>
<head>
<title>My Podcasts</title>
</head>
<body>
<h1>My Podcasts</h1>
<ul>
{}
</ul>
</body>
</html>
"#,
app_state
.iter()
.enumerate()
.map(|(id, podcast)| {
format!(r#"<li><a href="/{}">{}</a></li>"#, id, podcast.title)
})
.collect::<Vec<String>>()
.join("\n")
);
Html(response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment