2023-12-03 17:41:04 +01:00

51 lines
1.9 KiB
Rust

use log::error;
use nanobyte_opentelemetry::rocket::{OtelReqwestClient, TracingSpan};
use nanobyte_tera::l18n::LanguageDescription;
use ovlach_data::cv::data::CV;
use rocket::{get, State, response::Redirect, http::Status};
use rocket_dyn_templates::Template;
use serde::Serialize;
use crate::{PresentationConfig, services::cv::fetch_cv_data_from_backend, CVBackendConfig, tools::rocket::RequestLanguage, DefaultPerson, FrontendSVCConfig};
#[derive(Serialize, Debug)]
struct RootPage {
static_host: String,
cv: CV,
lang: LanguageDescription,
contact_svc: String,
pdf_download_url: String
}
#[get("/<language>")]
pub async fn index(presentation_config: &State<PresentationConfig>, cv_config: &State<CVBackendConfig>, language: RequestLanguage, client: OtelReqwestClient,
default_person: &State<DefaultPerson>, frontend_svc: &State<FrontendSVCConfig>, span: TracingSpan) -> Result<Template, Status> {
let span = span.0.enter();
let context = match fetch_cv_data_from_backend(&cv_config.cv_backend_path, &default_person.default_person_name, &client.0).await {
Ok(cv) => RootPage {
static_host: presentation_config.static_route.clone(),
cv,
lang: LanguageDescription::new(language.language.as_str(), "ovlach_frontend"),
contact_svc: frontend_svc.inner().contact_service.clone(),
pdf_download_url: format!("{}/cv/{}/output.pdf", frontend_svc.inner().pdf_service.clone(), default_person.default_person_name)
},
Err(e) => {
error!("Can't fetch CV data from backend {:?}", e);
drop(span);
return Err(Status::InternalServerError)
}
};
let result = Ok(Template::render("default", context));
drop(span);
result
}
#[get("/")]
pub fn index_without_lang() -> Redirect {
// Default language is czech (TODO: config)
Redirect::to(format!("{}/{}", "", "cs"))
}