This commit is contained in:
2023-11-26 18:40:12 +01:00
parent 7934c5bfaa
commit b65b1a7a54
10 changed files with 399 additions and 47 deletions

View File

@@ -4,8 +4,11 @@ use std::collections::HashMap;
use rocket::{*, fairing::AdHoc};
use rocket_dyn_templates::{Template, tera::Value};
use ::serde::Deserialize;
use tools::tera::static_filter;
pub mod routes;
pub mod services;
pub mod tools;
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
@@ -13,13 +16,10 @@ pub struct PresentationConfig {
static_route: String,
}
pub fn static_filter(
value: &Value,
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, rocket_dyn_templates::tera::Error> {
let host = args.get("static_host");
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}/{}", host.unwrap().as_str().unwrap(), value.as_str().unwrap()))); // TODO: fix-me here!
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct CVBackendConfig {
cv_backend_path: String,
}
pub fn rocket_builder() -> Rocket<Build> {
@@ -35,6 +35,8 @@ pub fn rocket_builder() -> Rocket<Build> {
})
).attach(
AdHoc::config::<PresentationConfig>()
).attach(
AdHoc::config::<CVBackendConfig>()
).mount("/", routes![
routes::root::index
])

View File

@@ -1,18 +1,31 @@
use rocket::{get, State};
use rocket_dyn_templates::Template;
use log::error;
use ovlach_data::cv::cv::CV;
use rocket::{get, State, response::status::NotFound, http::Status};
use rocket_dyn_templates::{Template, context};
use serde::Serialize;
use crate::PresentationConfig;
use crate::{PresentationConfig, services::cv::fetch_cv_data_from_backend, CVBackendConfig};
#[derive(Serialize, Debug)]
struct RootPage {
static_host: String,
cv: CV
}
#[get("/")]
pub fn index(config: &State<PresentationConfig>) -> Template {
let context = RootPage {
static_host: config.static_route.clone(),
pub async fn index(presentation_config: &State<PresentationConfig>, cv_config: &State<CVBackendConfig>) -> Result<Template, Status> {
let context = match fetch_cv_data_from_backend(cv_config.cv_backend_path.clone()).await {
Ok(cv) => RootPage {
static_host: presentation_config.static_route.clone(),
cv
},
Err(e) => {
error!("Can't fetch CV data from backend {:?}", e);
return Err(Status::InternalServerError)
}
};
Template::render("default", &context)
//return Templ;
Ok(Template::render("default", &context))
}

21
src/services/cv.rs Normal file
View File

@@ -0,0 +1,21 @@
use ovlach_data::cv::cv::CV;
#[derive(Debug)]
pub enum FetchError {
ReqwestError(reqwest::Error)
}
impl From<reqwest::Error> for FetchError {
fn from(e: reqwest::Error) -> Self {
FetchError::ReqwestError(e)
}
}
pub async fn fetch_cv_data_from_backend(backend_host: String) -> Result<CV, FetchError> {
let resp = reqwest::get(format!("{}/{}", backend_host, "/api/cv/ovlach"))
.await?
.json::<CV>()
.await?;
Ok(resp)
}

1
src/services/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod cv;

1
src/tools/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod tera;

12
src/tools/tera.rs Normal file
View File

@@ -0,0 +1,12 @@
use std::collections::HashMap;
use rocket_dyn_templates::tera::{Value, Error};
pub fn static_filter(
value: &Value,
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
let host = args.get("static_host");
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}/{}", host.unwrap().as_str().unwrap(), value.as_str().unwrap()))); // TODO: fix-me here!
}