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