diff --git a/src/lib.rs b/src/lib.rs index 06de60206023eaa179d80a7791fb8fabfd22cf9f..26cb6505f0715e0b53800fd011be5f3a08d27f17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,4 +12,5 @@ extern crate hyper; extern crate rand; pub mod cli; -pub mod routing; \ No newline at end of file +pub mod routing; +pub mod scripting; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a7a711360c6a56f40bc071a2da8598b235dc3d50..fb21a298fe4ad7f7a8d4cef781efa71c31a097ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,7 +149,11 @@ fn main() { printq!("[SETUP] Accepting uploads at /upload"); server = server.mount("/upload", routes![swerve::routing::mock_upload::to_file]); } - server = server.mount("/", routes![serve_root, serve_files]);; + server = server.mount("/", routes![ + serve_root, + serve_files, + routing::scripting::route_script + ]); if !args.flag_quiet { server = server.attach(rocket::fairing::AdHoc::on_launch(move |rckt| { diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 25ef89d92e38c76c939e21f8d265057fd6b93d08..b7054b18f5f05411ac7ed75c68e51f918937b6ec 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -1,2 +1,3 @@ pub mod mock_upload; -pub mod request; \ No newline at end of file +pub mod request; +pub mod scripting; \ No newline at end of file diff --git a/src/routing/scripting.rs b/src/routing/scripting.rs new file mode 100644 index 0000000000000000000000000000000000000000..165c0070b28c4941538a6bad02dbadfcf75f02cf --- /dev/null +++ b/src/routing/scripting.rs @@ -0,0 +1,8 @@ +use scripting::run_script; +use std::path::PathBuf; + +#[get("/__testing__/run-script")] +pub fn route_script() -> String { + let path = PathBuf::from("example/.swerve/get_user_by_id.rhai"); + run_script(path).unwrap_or(String::from("No script")) +} \ No newline at end of file diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..e7d98761b4f3f37a01d3f416fd8b941ec218de42 --- /dev/null +++ b/src/scripting/mod.rs @@ -0,0 +1,3 @@ +mod run_script; + +pub use self::run_script::run_script; \ No newline at end of file diff --git a/src/scripting/run_script.rs b/src/scripting/run_script.rs new file mode 100644 index 0000000000000000000000000000000000000000..96d9f57184f7b733964a8d9ffdf6ae96984469cd --- /dev/null +++ b/src/scripting/run_script.rs @@ -0,0 +1,46 @@ +//use dyon::{Runtime,Module,load_str,Variable,Dfn,Type}; +//use dyon::ast::convert; +use std::convert::AsRef; +use std::path::Path; +use std::fs::File; +use std::sync::Arc; +use std::io::Read; +use std::collections::HashMap; + +const SCRIPT_FOOTER: &'static str = " +fn main() { + println(\"Don't directly execute the module, nimrod\") +}"; + + +pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> { +// let mut resolution: Option<Variable> = None; +// dyon_fn!(fn resolve(val: Variable) { +// resolution = Some(val); // if let Ok(val) = runtime.pop() { Some(val) } else { None }; +// }); +// +// println!("{:?}", resolution); + + let mut file = File::open(&path).unwrap(); + let mut buf = String::new(); + + file.read_to_string(&mut buf); + buf.push_str(SCRIPT_FOOTER); +// let mut script_module = Module::new(); +// +// { +// load_str(path.as_ref().to_str().unwrap(), Arc::new(buf), &mut script_module); +// } + Some(buf) +// script_module.add(Arc::new("resolve".into()), resolve, Dfn { +// lts: vec![], +// tys: vec![Type::Object], +// ret: Type::Void, +// }); +// +// let mut hashmap: HashMap<Arc<String>, Variable> = HashMap::new(); + +// runtime.call_str("handle", &[Variable::f64(123f64)], &Arc::new(script_module)); + +// None +} \ No newline at end of file