mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-15 13:33:28 +02:00
Added plain text (gemtext like) templates
This commit is contained in:
@ -177,7 +177,7 @@ async fn main() {
|
||||
},
|
||||
},
|
||||
};
|
||||
let template_glob = template_base_dir.clone()+"*.html";
|
||||
let template_glob = template_base_dir.clone()+"*";
|
||||
println!("Parsing Templates from '{}' ...", &template_glob);
|
||||
let res = Tera::new((template_glob).as_str());
|
||||
let tera = match res {
|
||||
|
@ -38,6 +38,16 @@ impl ToString for ResponseFormat {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseFormat {
|
||||
fn to_file_extension(&self) -> String {
|
||||
match self {
|
||||
ResponseFormat::TextPlain => ".txt",
|
||||
ResponseFormat::TextHtml => ".html",
|
||||
ResponseFormat::ApplicationJson => ".json",
|
||||
}.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/* Template Settings */
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||
@ -88,7 +98,7 @@ impl Engine {
|
||||
view: &View,
|
||||
) -> Response {
|
||||
let mut response = match settings.format {
|
||||
ResponseFormat::TextHtml => {
|
||||
ResponseFormat::TextHtml | ResponseFormat::TextPlain => {
|
||||
let template_name = view.template_name();
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
@ -99,18 +109,23 @@ impl Engine {
|
||||
context.insert("data", &view);
|
||||
context.insert("extra", &self.template_config);
|
||||
|
||||
match self.tera.render(&(template_name+".html"), &context) {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
match self.tera.render(&(template_name+&settings.format.to_file_extension()), &context) {
|
||||
Ok(text) =>
|
||||
match settings.format {
|
||||
ResponseFormat::TextHtml => Html(text).into_response(),
|
||||
_ => text.into_response(),
|
||||
}
|
||||
Err(e) => {
|
||||
println!("There was an error while rendering index.html: {e:?}");
|
||||
let mut response = "Template error, contact owner or see logs.".into_response();
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
return response;
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Template error, contact owner or see logs.\n"
|
||||
).into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO: Plain Text should have its own matcher
|
||||
ResponseFormat::ApplicationJson | ResponseFormat::TextPlain => {
|
||||
ResponseFormat::ApplicationJson => {
|
||||
match view {
|
||||
View::Dig{result, ..} => {
|
||||
Json(result).into_response()
|
||||
|
Reference in New Issue
Block a user