Handle paste dynamic fetch

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-12-11 19:16:24 +05:30
parent 8d755c01f7
commit e7a18f4b03
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5
2 changed files with 10 additions and 6 deletions

View file

@ -9,4 +9,4 @@ edition = "2021"
actix-web = "4" actix-web = "4"
tera = "1.17.1" tera = "1.17.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11",default-features = false, features = ["rustls-tls"] } reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }

View file

@ -9,7 +9,7 @@ struct Template {
#[derive(Deserialize)] #[derive(Deserialize)]
struct Paste { struct Paste {
paste_id: String, paste_id: Option<String>,
} }
@ -20,15 +20,19 @@ async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Respond
let client = Client::new(); let client = Client::new();
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert("PRIVATE-TOKEN", "".parse().unwrap()); headers.insert("PRIVATE-TOKEN", "".parse().unwrap());
let resp = client.get("https://git.baalajimaestro.me/api/v4/projects/?ref=master").headers(headers).send().await.unwrap(); let resp: String;
let data = resp.text().await.unwrap(); if paste.paste_id.is_some() {
context.insert("paste_content", &data); resp = client.get(format!("https://git.baalajimaestro.me/api/v4/projects/12/repository/files/paste%2F{}.md?ref=master", paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap().text().await.unwrap();
}
else {
resp = "".to_string();
}
context.insert("paste_content", &resp);
let rendered = my_data.paste.render("paste.html", &context); let rendered = my_data.paste.render("paste.html", &context);
HttpResponse::Ok().body(rendered.unwrap()) HttpResponse::Ok().body(rendered.unwrap())
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let tera = match Tera::new("templates/*.html") { let tera = match Tera::new("templates/*.html") {