feat: create route handler macro

This commit is contained in:
Valerio Ageno
2024-05-18 20:45:35 +02:00
parent 74968b27cc
commit d4c9b25b90
12 changed files with 149 additions and 43 deletions
+41 -42
View File
@@ -5,25 +5,18 @@ use std::io::prelude::*;
use std::path::Path;
use std::path::PathBuf;
const ROOT_FOLDER: &'static str = "src/routes";
const AXUM_ENTRY_POINT: &'static str = r##"
// File automatically generated
// Do not manually change it
use axum::response::Html;
use axum::{http::Uri, routing::get, Router};
use ssr_rs::Ssr;
use std::cell::RefCell;
use std::fs::read_to_string;
use tower_http::services::ServeDir;
use tuono_lib::{ssr, Ssr};
thread_local! {
static SSR: RefCell<Ssr<'static, 'static>> = RefCell::new(
Ssr::from(
read_to_string("./out/server/server-main.js").unwrap(),
""
).unwrap()
)
}
// MODULE_IMPORTS
#[tokio::main]
async fn main() {
@@ -48,18 +41,13 @@ async fn catch_all(uri: Uri) -> Html<String> {
}}"#
);
let result = SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)));
let result = ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)));
match result {
Ok(html) => Html(html),
_ => Html("500 internal server error".to_string()),
}
}
// ROUTE_DECLARATIONS
"##;
fn create_main_file(base_path: &Path, bundled_file: &String) {
@@ -71,8 +59,6 @@ fn create_main_file(base_path: &Path, bundled_file: &String) {
.expect("write failed");
}
fn clean_up_source_file(file: &mut String) {}
fn get_route_name<'a>(path: &PathBuf, base_path: &Path) -> String {
let base_path_str = base_path.to_str().unwrap();
if base_path_str == "." {
@@ -96,9 +82,9 @@ fn get_route_name<'a>(path: &PathBuf, base_path: &Path) -> String {
.replace("index", "")
}
fn collect_handlers(base_path: &Path) -> (String, HashMap<String, String>) {
let mut declarations = String::from("// ROUTE_DECLARATIONS\n");
let mut fn_map: HashMap<String, String> = HashMap::new();
fn collect_handlers(base_path: &Path) -> HashMap<String, String> {
// <path, name>
let mut mods_map: HashMap<String, String> = HashMap::new();
let _: Vec<_> = glob(base_path.join("src/routes/**/*.rs").to_str().unwrap())
.unwrap()
@@ -106,29 +92,45 @@ fn collect_handlers(base_path: &Path) -> (String, HashMap<String, String>) {
let entry = entry.unwrap();
let route_name = get_route_name(&entry, base_path);
let mut file_content = fs::read_to_string(entry).unwrap();
clean_up_source_file(&mut file_content);
let hash = fastmurmur3::hash(file_content.as_bytes());
let new_fn_name = format!("fn_{}", hash);
let file_content = file_content.replace("get_server_side_props", &new_fn_name);
// Remove first slash
let mut module = route_name.as_str().chars();
module.next();
fn_map.insert(route_name, new_fn_name);
declarations.push_str(&file_content);
mods_map.insert(
module.as_str().to_string(),
entry
.to_str()
.unwrap()
.replace(base_path.to_str().unwrap(), ""),
);
})
.collect();
println!("{fn_map:?}");
dbg!(&mods_map);
return (declarations, fn_map);
return mods_map;
}
fn create_axum_routes_declaration(routes: HashMap<String, String>) -> String {
fn create_axum_routes_declaration(routes: &HashMap<String, String>) -> String {
let mut route_declarations = String::from("// ROUTE_BUILDER\n");
for (route, handler) in routes.iter() {
route_declarations.push_str(&format!(r#".route("{route}", get({handler}))"#))
for (route, _path) in routes.iter() {
route_declarations.push_str(&format!(r#".route("/{route}", get({route}::route))"#))
}
route_declarations
}
fn create_modules_declaration(routes: &HashMap<String, String>) -> String {
let mut route_declarations = String::from("// MODULE_IMPORTS\n");
for (route, path) in routes.iter() {
route_declarations.push_str(&format!(
r#"#[path="..{path}"]
mod {route};
"#
))
}
route_declarations
@@ -139,14 +141,11 @@ pub fn bundle() {
let base_path = std::env::current_dir().unwrap();
let (handlers, fn_map) = collect_handlers(&base_path);
let mods = collect_handlers(&base_path);
let bundled_file = AXUM_ENTRY_POINT.replace(
"// ROUTE_BUILDER\n",
&create_axum_routes_declaration(fn_map),
);
let bundled_file = bundled_file.replace("// ROUTE_DECLARATIONS", &handlers[..]);
let bundled_file = AXUM_ENTRY_POINT
.replace("// ROUTE_BUILDER\n", &create_axum_routes_declaration(&mods))
.replace("// MODULE_IMPORTS\n", &create_modules_declaration(&mods));
create_main_file(&base_path, &bundled_file);
}