2024-05-14 20:07:24 +02:00
|
|
|
use glob::glob;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2024-05-14 20:42:32 +02:00
|
|
|
const AXUM_ENTRY_POINT: &'static str = r##"
|
|
|
|
|
// File automatically generated
|
2024-05-14 20:07:24 +02:00
|
|
|
// Do not manually change it
|
|
|
|
|
|
|
|
|
|
use axum::response::Html;
|
2024-05-14 20:42:32 +02:00
|
|
|
use axum::{http::Uri, routing::get, Router};
|
2024-05-14 20:07:24 +02:00
|
|
|
use ssr_rs::Ssr;
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::fs::read_to_string;
|
2024-05-14 20:42:32 +02:00
|
|
|
use tower_http::services::ServeDir;
|
2024-05-14 20:07:24 +02:00
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
|
static SSR: RefCell<Ssr<'static, 'static>> = RefCell::new(
|
|
|
|
|
Ssr::from(
|
2024-05-14 20:42:32 +02:00
|
|
|
read_to_string("./out/server/server-main.js").unwrap(),
|
2024-05-14 20:07:24 +02:00
|
|
|
""
|
|
|
|
|
).unwrap()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
Ssr::create_platform();
|
|
|
|
|
|
|
|
|
|
let app = Router::new()
|
|
|
|
|
// ROUTE_BUILDER
|
2024-05-18 11:46:57 +02:00
|
|
|
.nest_service("/__tuono", ServeDir::new(".tuono"))
|
2024-05-14 20:42:32 +02:00
|
|
|
.fallback_service(ServeDir::new("public").fallback(get(catch_all)));
|
2024-05-14 20:07:24 +02:00
|
|
|
|
|
|
|
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
|
|
|
|
axum::serve(listener, app).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 20:42:32 +02:00
|
|
|
async fn catch_all(uri: Uri) -> Html<String> {
|
|
|
|
|
dbg!(&uri.path());
|
2024-05-14 21:23:32 +02:00
|
|
|
let path = &uri.path();
|
|
|
|
|
|
|
|
|
|
let payload = format!(
|
|
|
|
|
r#"{{
|
|
|
|
|
"path": "{path}"
|
|
|
|
|
}}"#
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let result = SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)));
|
2024-05-14 20:42:32 +02:00
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(html) => Html(html),
|
|
|
|
|
_ => Html("500 internal server error".to_string()),
|
|
|
|
|
}
|
2024-05-14 20:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-14 20:42:32 +02:00
|
|
|
|
2024-05-14 20:07:24 +02:00
|
|
|
// ROUTE_DECLARATIONS
|
|
|
|
|
|
2024-05-14 20:42:32 +02:00
|
|
|
|
2024-05-14 20:07:24 +02:00
|
|
|
"##;
|
|
|
|
|
|
|
|
|
|
fn create_main_file(base_path: &Path, bundled_file: &String) {
|
|
|
|
|
let mut data_file =
|
|
|
|
|
fs::File::create(base_path.join(".tuono/main.rs")).expect("creation failed");
|
|
|
|
|
|
|
|
|
|
data_file
|
|
|
|
|
.write(bundled_file.as_bytes())
|
|
|
|
|
.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 == "." {
|
|
|
|
|
return path.to_str().unwrap().replace("/src/routes/", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: refactor this horrible code
|
|
|
|
|
if base_path_str.ends_with("/") {
|
|
|
|
|
return path
|
|
|
|
|
.to_str()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.replace(&format!("{base_path_str}src/routes"), "")
|
|
|
|
|
.replace(".rs", "")
|
|
|
|
|
.replace("index", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path.to_str()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.replace(&format!("{base_path_str}/src/routes"), "")
|
|
|
|
|
.replace(".rs", "")
|
|
|
|
|
.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();
|
|
|
|
|
|
|
|
|
|
let _: Vec<_> = glob(base_path.join("src/routes/**/*.rs").to_str().unwrap())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.map(|entry| {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
fn_map.insert(route_name, new_fn_name);
|
|
|
|
|
|
|
|
|
|
declarations.push_str(&file_content);
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
println!("{fn_map:?}");
|
|
|
|
|
|
|
|
|
|
return (declarations, fn_map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}))"#))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
route_declarations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bundle() {
|
|
|
|
|
println!("Axum project bundling");
|
|
|
|
|
|
|
|
|
|
let base_path = std::env::current_dir().unwrap();
|
|
|
|
|
|
|
|
|
|
let (handlers, fn_map) = 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[..]);
|
|
|
|
|
|
|
|
|
|
create_main_file(&base_path, &bundled_file);
|
|
|
|
|
}
|