okay holy shit i can actually put HTML in responses now

This commit is contained in:
Erin 2024-04-15 03:25:50 -04:00
parent ac593e2882
commit 14d2792bed
Signed by: erin
SSH key fingerprint: SHA256:clvLPaxKthBet+VUQTKQdDkjaqg2/KnYHQaPASp5pFE
3 changed files with 52 additions and 2 deletions

16
Cargo.lock generated
View file

@ -389,6 +389,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-std", "async-std",
"html-escape",
"rocket", "rocket",
"sqlx", "sqlx",
] ]
@ -952,6 +953,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "html-escape"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d1ad449764d627e22bfd7cd5e8868264fc9236e07c752972b4080cd351cb476"
dependencies = [
"utf8-width",
]
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.12" version = "0.2.12"
@ -2560,6 +2570,12 @@ version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "utf8-width"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3"
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"

View file

@ -8,5 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.82" anyhow = "1.0.82"
async-std = { version = "1.12.0", features = ["attributes"] } async-std = { version = "1.12.0", features = ["attributes"] }
html-escape = "0.2.13"
rocket = "0.5.0" rocket = "0.5.0"
sqlx = { version = "0.7", features = ["runtime-async-std", "sqlite", "migrate"] } sqlx = { version = "0.7", features = ["runtime-async-std", "sqlite", "migrate"] }

View file

@ -1,3 +1,4 @@
use rocket::response::content;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use std::env; use std::env;
@ -25,7 +26,7 @@ async fn get_comments(pool: &SqlitePool, page: &str) -> Result<Vec<Comment>, sql
} }
#[rocket::get("/txt?<page>")] #[rocket::get("/txt?<page>")]
async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> Result<String, String> { async fn txt(page: &str, pool: &rocket::State<SqlitePool>) -> Result<String, String> {
Ok(get_comments(pool, page) Ok(get_comments(pool, page)
.await .await
.map_err(|_| "error fetching comments for this page, sorry")? .map_err(|_| "error fetching comments for this page, sorry")?
@ -41,6 +42,38 @@ async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> Result<String, S
.collect::<String>()) .collect::<String>())
} }
#[rocket::get("/html?<page>")]
async fn html(
page: &str,
pool: &rocket::State<SqlitePool>,
) -> Result<content::RawHtml<String>, String> {
let comments = get_comments(pool, page)
.await
.map_err(|_| "error fetching comments for this page, sorry")?;
let html = format!(
r#"<div class="comments">{}</div>"#,
comments
.into_iter()
.map(|rec| {
format!(
r#"
<div class="comment" data-id="{}">
<div class="comment__author">{}</div>
<div class="comment__content">{}</div>
</div>
"#,
rec.id,
html_escape::encode_text(&rec.name.unwrap_or("anonymous".to_owned())),
html_escape::encode_text(&rec.content),
)
})
.collect::<String>()
);
Ok(content::RawHtml(html))
}
#[rocket::get("/add?<page>&<name>&<content>")] #[rocket::get("/add?<page>&<name>&<content>")]
async fn add( async fn add(
page: &str, page: &str,
@ -69,5 +102,5 @@ async fn rocket() -> _ {
.expect("Failed to connect to database"); .expect("Failed to connect to database");
rocket::build() rocket::build()
.manage(pool) .manage(pool)
.mount("/", rocket::routes![index, add]) .mount("/", rocket::routes![txt, html, add])
} }