stuff
This commit is contained in:
@@ -17,5 +17,7 @@ tera = "1.6.1"
|
||||
actix-web-static-files = "3.0"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
tokio = { version="0.2.6", features=["full"] }
|
||||
|
||||
[build-dependencies]
|
||||
actix-web-static-files = "3.0"
|
||||
|
||||
@@ -12,6 +12,4 @@
|
||||
|
||||
<h1>Welcome to tera on actix</h1>
|
||||
|
||||
Number of visits: {{visits}}
|
||||
|
||||
{%- endblock %}
|
||||
|
||||
+4
-16
@@ -12,6 +12,7 @@ use tera::Tera;
|
||||
use include_dir::Dir;
|
||||
use std::sync::Arc;
|
||||
use log::LevelFilter;
|
||||
use crate::tera_ext::TeraExt;
|
||||
|
||||
mod tera_ext;
|
||||
|
||||
@@ -24,29 +25,19 @@ static TEMPLATES: include_dir::Dir = include_dir::include_dir!("./resources/temp
|
||||
lazy_static! {
|
||||
static ref TERA : Tera = {
|
||||
let mut tera = Tera::default();
|
||||
tera_ext::tera_includedir_walk_folder(&mut tera, &TEMPLATES);
|
||||
tera.add_include_dir_templates(&TEMPLATES);
|
||||
tera
|
||||
};
|
||||
}
|
||||
|
||||
struct VisitCounter {
|
||||
pub counter : AtomicUsize,
|
||||
}
|
||||
|
||||
impl VisitCounter {
|
||||
pub fn count_visit(&self) -> usize {
|
||||
self.counter.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn service_index(req: HttpRequest, visits : web::Data<VisitCounter>) -> actix_web::Result<impl Responder> {
|
||||
async fn service_index(req: HttpRequest) -> actix_web::Result<impl Responder> {
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("visits", &visits.count_visit());
|
||||
|
||||
let html = TERA.render("index", &context).map_err(|e| {
|
||||
actix_web::error::ErrorInternalServerError(e)
|
||||
})?;
|
||||
|
||||
Ok(HttpResponse::Ok().body(html))
|
||||
}
|
||||
|
||||
@@ -54,14 +45,11 @@ async fn service_index(req: HttpRequest, visits : web::Data<VisitCounter>) -> ac
|
||||
async fn main() -> std::io::Result<()> {
|
||||
simple_logging::log_to_stderr(LevelFilter::Debug);
|
||||
|
||||
let counter = web::Data::new(VisitCounter { counter : Default::default() });
|
||||
|
||||
HttpServer::new(move || {
|
||||
let static_files = actix_web_static_files::ResourceFiles::new("/static", included_static_files())
|
||||
.do_not_resolve_defaults();
|
||||
|
||||
App::new()
|
||||
.app_data(counter.clone())
|
||||
.service(service_index)
|
||||
.service(static_files)
|
||||
})
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
use tera::Tera;
|
||||
use include_dir::Dir;
|
||||
|
||||
/// Add templates from include_dir!() to Tera
|
||||
pub(crate) fn tera_includedir_walk_folder(tera : &mut Tera, dir: &Dir) {
|
||||
let mut templates = vec![];
|
||||
tera_includedir_walk_folder_inner(&mut templates, tera, dir);
|
||||
tera.add_raw_templates(templates);
|
||||
}
|
||||
|
||||
fn tera_includedir_walk_folder_inner(collected : &mut Vec<(String, String)>, tera : &mut Tera, dir: &Dir) {
|
||||
for f in dir.files() {
|
||||
if f.path().extension().unwrap_or_default() != "tera" {
|
||||
let halves : Vec<_> = f.path().file_name().unwrap().to_str().unwrap().split('.').collect();
|
||||
|
||||
if halves.last().unwrap() != &"tera" {
|
||||
debug!("Bad file: {:?}", f);
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = f.path().file_stem().unwrap().to_str().unwrap().split('.').nth(0).unwrap();
|
||||
let name = halves.first().unwrap();
|
||||
let content = f.contents_utf8().unwrap();
|
||||
|
||||
let p = f.path().parent().unwrap().join(name);
|
||||
@@ -28,3 +24,16 @@ fn tera_includedir_walk_folder_inner(collected : &mut Vec<(String, String)>, ter
|
||||
tera_includedir_walk_folder_inner(collected, tera, subdir)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait TeraExt {
|
||||
fn add_include_dir_templates(&mut self, dir: &Dir) -> tera::Result<()>;
|
||||
}
|
||||
|
||||
impl TeraExt for Tera {
|
||||
fn add_include_dir_templates(&mut self, dir: &Dir) -> tera::Result<()> {
|
||||
debug!("Walk dir: {:?}", dir);
|
||||
let mut templates = vec![];
|
||||
tera_includedir_walk_folder_inner(&mut templates, self, dir);
|
||||
self.add_raw_templates(templates)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user