migration: askama to ramhorns
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending
ci/woodpecker/pr/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-09 19:02:06 +02:00
parent d1f1d5d478
commit 6d3b4c8398
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
11 changed files with 204 additions and 145 deletions

177
Cargo.lock generated
View file

@ -260,33 +260,10 @@ dependencies = [
]
[[package]]
name = "askama"
version = "0.12.0"
name = "arrayvec"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47cbc3cf73fa8d9833727bbee4835ba5c421a0d65b72daf9a7b5d0e0f9cfb57e"
dependencies = [
"askama_derive",
"askama_escape",
"humansize",
"num-traits",
"percent-encoding",
]
[[package]]
name = "askama_derive"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c22fbe0413545c098358e56966ff22cdd039e10215ae213cfbd65032b119fc94"
dependencies = [
"basic-toml",
"mime",
"mime_guess",
"nom",
"proc-macro2",
"quote",
"serde",
"syn 2.0.13",
]
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "askama_escape"
@ -300,6 +277,19 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bae"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33b8de67cc41132507eeece2584804efcb15f85ba516e34c944b7667f480397a"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "base64"
version = "0.21.0"
@ -307,13 +297,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"
[[package]]
name = "basic-toml"
version = "0.1.2"
name = "beef"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c0de75129aa8d0cceaf750b89013f0e08804d6ec61416da787b35ad0d7cddf1"
dependencies = [
"serde",
]
checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1"
[[package]]
name = "bitflags"
@ -475,9 +462,9 @@ version = "0.1.0"
dependencies = [
"actix-files",
"actix-web",
"askama",
"glob",
"minify-html",
"ramhorns",
"serde",
"toml",
]
@ -589,6 +576,15 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.2.6"
@ -627,15 +623,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humansize"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
dependencies = [
"libm",
]
[[package]]
name = "idna"
version = "0.3.0"
@ -689,12 +676,6 @@ version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]]
name = "libm"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb"
[[package]]
name = "local-channel"
version = "0.1.3"
@ -732,6 +713,29 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "logos"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf8b031682c67a8e3d5446840f9573eb7fe26efe7ec8d195c9ac4c0647c502f1"
dependencies = [
"logos-derive",
]
[[package]]
name = "logos-derive"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d849148dbaf9661a6151d1ca82b13bb4c4c128146a88d05253b38d4e2f496c"
dependencies = [
"beef",
"fnv",
"proc-macro2",
"quote",
"regex-syntax",
"syn 1.0.109",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -815,15 +819,6 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
@ -910,6 +905,30 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.56"
@ -919,6 +938,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "pulldown-cmark"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d9cc634bc78768157b5cbfe988ffcd1dcba95cd2b2f03a88316c08c6d00ed63"
dependencies = [
"bitflags",
"memchr",
"unicase",
]
[[package]]
name = "quote"
version = "1.0.26"
@ -928,6 +958,33 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "ramhorns"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47008ae2e2a9085a3f658203609d79f8a027829cf88a088d0c0084e18ba8f0b9"
dependencies = [
"arrayvec",
"beef",
"fnv",
"logos",
"pulldown-cmark",
"ramhorns-derive",
]
[[package]]
name = "ramhorns-derive"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ada9bbdd21adf426f932bf76b3db7d553538dffc16afd5fb8ce2ce2110a75536"
dependencies = [
"bae",
"fnv",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -1300,6 +1357,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "url"
version = "2.3.1"

View file

@ -12,7 +12,7 @@ license = "AGPL-3.0-or-later"
[dependencies]
actix-web = "4"
actix-files = "0.6"
askama = "0.12.0"
ramhorns = "0.14.0"
toml = "0.7.3"
serde = { version = "1.0.159", features = ["derive"] }
minify-html = "0.10.8"

View file

@ -1,3 +0,0 @@
[general]
# Directories to search for templates, relative to the crate root.
dirs = ["dist/templates", "templates"]

View file

@ -1,6 +1,6 @@
use serde::Deserialize;
use std::{
fs::{self, read_to_string, remove_dir_all},
fs::{self, remove_dir_all},
path::PathBuf,
};
@ -21,16 +21,7 @@ pub struct FileConfig {
pub struct Config {
pub fc: FileConfig,
pub static_location: String,
}
#[derive(Deserialize)]
struct AskamaConfig {
general: AskamaConfigGeneral,
}
#[derive(Deserialize)]
struct AskamaConfigGeneral {
dirs: Vec<String>,
pub templates_location: String,
}
impl FileConfig {
@ -81,22 +72,21 @@ fn get_file_config(file_path: &str) -> FileConfig {
}
}
fn get_askama_config() -> AskamaConfig {
toml::from_str(&read_to_string("askama.toml").unwrap()).unwrap()
}
pub fn get_config(file_path: &str) -> Config {
let internal_config = get_file_config(file_path);
let static_dir = "static".to_string();
// TODO: Check if templates dir is coherent with the whole list
let templates_dir = get_askama_config().general.dirs.last().unwrap().to_string();
// TODO: Check dist by askama config file
let files_root = init("dist".to_string(), static_dir.clone(), templates_dir);
let templates_dir = "templates".to_string();
let files_root = init(
"dist".to_string(),
static_dir.clone(),
templates_dir.clone(),
);
Config {
fc: internal_config,
static_location: format!("{}/{}", files_root, static_dir),
templates_location: format!("{}/{}", files_root, templates_dir),
}
}

View file

@ -1,7 +1,6 @@
use crate::{config::Config, template::render};
use actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder};
use askama::Template;
use crate::config::Config;
use ramhorns::Content;
#[routes]
#[get("/.well-known/security.txt")]
@ -13,8 +12,7 @@ pub async fn security(req: HttpRequest, config: web::Data<Config>) -> impl Respo
))
}
#[derive(Template)]
#[template(path = "security.txt")]
#[derive(Content)]
struct SecurityTemplate {
contact: String,
pref_lang: String,
@ -22,17 +20,19 @@ struct SecurityTemplate {
}
fn get_security(config: Config, info: ConnectionInfo) -> String {
let data = SecurityTemplate {
contact: config.fc.mail.unwrap_or_default(),
pref_lang: config.fc.lang.unwrap_or_default(),
url: format!(
"{}://{}/.well-known/security.txt",
info.scheme(),
info.host()
),
};
data.render().unwrap()
render(
config.templates_location,
"security.txt",
SecurityTemplate {
contact: config.fc.mail.unwrap_or_default(),
pref_lang: config.fc.lang.unwrap_or_default(),
url: format!(
"{}://{}/.well-known/security.txt",
info.scheme(),
info.host()
),
},
)
}
#[get("/humans.txt")]

View file

@ -1,17 +1,19 @@
use actix_web::{get, HttpResponse, Responder};
use askama::Template;
use crate::template::render_html;
use crate::{config::Config, template::render};
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
#[get("/contrib")]
pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_page())
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_page(config.get_ref().clone()))
}
#[derive(Template)]
#[template(path = "contrib.html")]
#[derive(Content)]
struct PortfolioTemplate {}
pub fn get_page() -> std::string::String {
render_html(&PortfolioTemplate {})
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"contrib.html",
PortfolioTemplate {},
)
}

View file

@ -1,17 +1,16 @@
use actix_web::{get, HttpResponse, Responder};
use askama::Template;
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::template::render_html;
use crate::{config::Config, template::render};
#[get("/")]
pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_page())
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_page(config.get_ref().clone()))
}
#[derive(Template)]
#[template(path = "index.html")]
#[derive(Content)]
struct IndexTemplate {}
pub fn get_page() -> std::string::String {
render_html(&IndexTemplate {})
pub fn get_page(config: Config) -> std::string::String {
render(config.templates_location, "index.html", IndexTemplate {})
}

View file

@ -1,17 +1,20 @@
use actix_web::{get, HttpResponse, Responder};
use askama::Template;
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::template::render_html;
use crate::{config::Config, template::render};
#[get("/networks")]
pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_page())
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_page(config.get_ref().clone()))
}
#[derive(Template)]
#[template(path = "networks.html")]
#[derive(Content)]
struct NetworksTemplate {}
pub fn get_page() -> std::string::String {
render_html(&NetworksTemplate {})
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"networks.html",
NetworksTemplate {},
)
}

View file

@ -1,16 +1,15 @@
use actix_web::{HttpResponse, Responder};
use askama::Template;
use actix_web::{web, HttpResponse, Responder};
use ramhorns::Content;
use crate::template::render_html;
use crate::{config::Config, template::render};
pub async fn page() -> impl Responder {
HttpResponse::NotFound().body(get_page())
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::NotFound().body(get_page(config.get_ref().clone()))
}
#[derive(Template)]
#[template(path = "404.html")]
#[derive(Content)]
struct Error404Template {}
pub fn get_page() -> std::string::String {
render_html(&Error404Template {})
pub fn get_page(config: Config) -> std::string::String {
render(config.templates_location, "404.html", Error404Template {})
}

View file

@ -1,17 +1,20 @@
use actix_web::{get, HttpResponse, Responder};
use askama::Template;
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::template::render_html;
use crate::{config::Config, template::render};
#[get("/portfolio")]
pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_page())
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_page(config.get_ref().clone()))
}
#[derive(Template)]
#[template(path = "portfolio.html")]
#[derive(Content)]
struct PortfolioTemplate {}
pub fn get_page() -> std::string::String {
render_html(&PortfolioTemplate {})
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"portfolio.html",
PortfolioTemplate {},
)
}

View file

@ -1,5 +1,8 @@
use askama::DynTemplate;
use ramhorns::{Content, Ramhorns};
pub fn render_html(template: &dyn DynTemplate) -> String {
template.dyn_render().unwrap()
pub fn render<C: Content>(template_dir: String, template: &str, data: C) -> String {
let mut tpls: Ramhorns = Ramhorns::lazy(template_dir).unwrap();
let tpl = tpls.from_file(template).unwrap();
tpl.render(&data)
}