dfgd
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate lazy_static;
|
||||
|
||||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest};
|
||||
use parking_lot::Mutex;
|
||||
use actix_web::web::{service, scope};
|
||||
use actix_web::http::StatusCode;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::path::{PathBuf, Path};
|
||||
use actix_web_static_files;
|
||||
use tera::Tera;
|
||||
use include_dir::Dir;
|
||||
use std::sync::Arc;
|
||||
use log::LevelFilter;
|
||||
|
||||
mod tera_ext;
|
||||
|
||||
// Embed static files
|
||||
include!(concat!(env!("OUT_DIR"), "/static_files.rs"));
|
||||
|
||||
// Embed templates
|
||||
static TEMPLATES: include_dir::Dir = include_dir::include_dir!("./resources/templates");
|
||||
|
||||
lazy_static! {
|
||||
static ref TERA : Tera = {
|
||||
let mut tera = Tera::default();
|
||||
tera_ext::tera_includedir_walk_folder(&mut tera, &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> {
|
||||
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))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
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)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?.run().await
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
// #[get("/")]
|
||||
// async fn hello(state : web::Data<VisitCounter>) -> impl Responder {
|
||||
// HttpResponse::Ok().body(format!("Hello world! {}", state.count_visit()))
|
||||
// }
|
||||
//
|
||||
// #[post("/echo")]
|
||||
// async fn echo(req_body: String) -> impl Responder {
|
||||
// HttpResponse::Ok().body(req_body)
|
||||
// }
|
||||
// async fn static_files(req: HttpRequest) -> actix_web::Result<NamedFile> {
|
||||
// let path: PathBuf = req.match_info().query("filename").parse().unwrap();
|
||||
//
|
||||
//
|
||||
//
|
||||
// Ok(NamedFile::open(path)?)
|
||||
// }
|
||||
//
|
||||
// #[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
// async fn user_friend(web::Path((user_id, friend)): web::Path<(u32, String)>, state : web::Data<VisitCounter>) -> impl Responder {
|
||||
// HttpResponse::Ok()
|
||||
// .header("Content-Type", "text/html")
|
||||
// .body(format!("<h1>Welcome {}, user_id {}!</h1>Visitor nr {}\n", friend, user_id, state.count_visit()))
|
||||
// }
|
||||
@@ -0,0 +1,30 @@
|
||||
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" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = f.path().file_stem().unwrap().to_str().unwrap().split('.').nth(0).unwrap();
|
||||
let content = f.contents_utf8().unwrap();
|
||||
|
||||
let p = f.path().parent().unwrap().join(name);
|
||||
let template_path = p.to_str().unwrap();
|
||||
|
||||
debug!("Add template: {}", template_path);
|
||||
collected.push((template_path.to_string(), content.to_string()));
|
||||
}
|
||||
|
||||
for subdir in dir.dirs() {
|
||||
tera_includedir_walk_folder_inner(collected, tera, subdir)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user