master
Ondřej Hruška 4 years ago
parent e17681d6f5
commit 4e97f7a19a
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 40
      Cargo.lock
  2. 2
      yopa-web/Cargo.toml
  3. 2
      yopa-web/resources/templates/index.html.tera
  4. 20
      yopa-web/src/main.rs
  5. 27
      yopa-web/src/tera_ext.rs

40
Cargo.lock generated

@ -1163,12 +1163,24 @@ dependencies = [
"kernel32-sys",
"libc",
"log",
"miow",
"miow 0.2.2",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
name = "mio-named-pipes"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656"
dependencies = [
"log",
"mio",
"miow 0.3.6",
"winapi 0.3.9",
]
[[package]]
name = "mio-uds"
version = "0.6.8"
@ -1192,6 +1204,16 @@ dependencies = [
"ws2_32-sys",
]
[[package]]
name = "miow"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897"
dependencies = [
"socket2",
"winapi 0.3.9",
]
[[package]]
name = "net2"
version = "0.2.37"
@ -1947,19 +1969,34 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092"
dependencies = [
"bytes 0.5.6",
"fnv",
"futures-core",
"iovec",
"lazy_static",
"libc",
"memchr",
"mio",
"mio-named-pipes",
"mio-uds",
"num_cpus",
"pin-project-lite 0.1.11",
"signal-hook-registry",
"slab",
"tokio-macros",
"winapi 0.3.9",
]
[[package]]
name = "tokio-macros"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-util"
version = "0.3.1"
@ -2346,4 +2383,5 @@ dependencies = [
"parking_lot",
"simple-logging",
"tera",
"tokio",
]

@ -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 %}

@ -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)
}
}

Loading…
Cancel
Save