zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use lazy_static::lazy_static;

use std::collections::HashMap;

use crate::core::cacheable::YourCache;
use crate::prelude2::*;

pub mod binary_tree;
pub mod mandelbrot_png;

pub use binary_tree::*;
pub use mandelbrot_png::*;

lazy_static! {
    #[rustfmt::skip]
    pub static ref TEMP_DIR: String = std::env::temp_dir().as_path().display().to_string();
    pub static ref TREE_MEMORY: YourCache::<BinaryTree<i32>> = YourCache::build(15, 5 * 1024 * 1024);
}

pub async fn algorithm_index(request: HttpRequest) -> impl Responder {
    let mut ctx = tera::Context::new();
    ctx.insert("name", "啦啦发啦");
    request.render(200, "algorithm/index.html", ctx)
}

// 蒙德布洛德集合绘图
pub async fn mandelbrot() -> impl Responder {
    let file_name = "mandel.png";
    let current_file = std::path::Path::new(&*TEMP_DIR).join(file_name);

    log::info!("mandelbrot_png: {}", current_file.display());

    let args = vec![
        current_file.display().to_string(),
        String::from("4000x3000"),
        String::from("-1.20,0.35"),
        String::from("-1,0.20"),
    ];

    write1(&args);

    actix_files::NamedFile::open(current_file.display().to_string())
}

#[rustfmt::skip]
pub async fn binary_tree_put(
    query: web::Query<HashMap<String, String>>,
    request: HttpRequest) -> impl Responder {

    let tree_value = query.get("treeValue").map(|s| s.to_string()).unwrap_or("8".to_string());

	let mut tree = TREE_MEMORY.put_if_absent("mytree", BinaryTree::Empty);

	tree.add(crate::commons::string_to_number(&tree_value).unwrap());

	TREE_MEMORY.put("mytree", tree);

    request.json(200, R::ok(TREE_MEMORY.get("mytree")))

}

#[rustfmt::skip]
pub async fn binary_tree_view(request: HttpRequest) -> impl Responder {
    let tree = TREE_MEMORY.get("mytree");

    for (i, node) in (0..).zip(&tree) {
        log::info!("Index={}, Value={:?}", i, node);
    }

    request.json(200, R::ok(tree))
}