add basic page for paper

This commit is contained in:
Vri 🌈 2023-11-15 19:58:58 +01:00
parent 2d4b93d9e4
commit 1fa011078d
Signed by: vrifox
SSH key fingerprint: SHA256:7OLOvW+jmULkXpdl5rUWgid7WJQhOIEgj+4WP/PtCpI
4 changed files with 472 additions and 8 deletions

View file

@ -1,4 +1,10 @@
use axum::{extract::Query, http::header, response::IntoResponse, routing::get, Router};
use axum::{
extract::{Path, Query},
http::header,
response::IntoResponse,
routing::get,
Router,
};
use std::collections::HashMap;
use tera::Tera;
@ -7,7 +13,8 @@ async fn main() {
// build our application with a single route
let app = Router::new()
.route("/", get(index))
.route("/search", get(search));
.route("/search", get(search))
.route("/paper/:id", get(paper));
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
@ -23,7 +30,7 @@ async fn index() -> impl IntoResponse {
let context = tera::Context::new();
// Render the template with the given context
let rendered = tera.render("index.html.tera", &context).unwrap();
let rendered = tera.render("index.html", &context).unwrap();
header(rendered)
}
@ -34,7 +41,28 @@ async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoRespo
let mut context = tera::Context::new();
context.insert("query", params.get("q").unwrap());
let rendered = tera.render("search.html.tera", &context).unwrap();
let rendered = tera.render("search.html", &context).unwrap();
header(rendered)
}
async fn paper(Path(paper_id): Path<u64>) -> impl IntoResponse {
let response = reqwest::get(format!(
"https://www.muenchen-transparent.de/oparl/v1.0/paper/{0}",
paper_id
))
.await
.unwrap()
.json::<HashMap<String, String>>()
.await
.unwrap();
let tera = tera_create();
let mut context = tera::Context::new();
context.insert("paper_id", &paper_id);
context.insert("paper_name", &response.get("name").unwrap());
let rendered = tera.render("paper.html", &context).unwrap();
header(rendered)
}