diff --git a/src/cli/config_routes.rs b/src/cli/config_routes.rs index 62cf89dd179a19317f6e15cffc90fe4da479f3bf..b9396f737b49c21d75aacc8d215d51afab119f4a 100644 --- a/src/cli/config_routes.rs +++ b/src/cli/config_routes.rs @@ -2,9 +2,9 @@ use std::collections::HashMap; #[derive(Clone, Debug, Deserialize)] pub struct RouteHandler { - route: String, - response: Option<ResponseHandler>, - script: Option<String>, + pub route: String, + pub response: Option<ResponseHandler>, + pub script: Option<String>, } #[derive(Clone, Debug, Deserialize)] diff --git a/src/routing/request.rs b/src/routing/request.rs index 68931d59426a0b12fbef746db45749f9cb265f2e..7e210685a4b3e8f89a0a9e8dee04a2a2f5d32369 100644 --- a/src/routing/request.rs +++ b/src/routing/request.rs @@ -72,6 +72,7 @@ pub mod path { use std::path as std_path; use std::option::Option::*; + #[derive(PartialEq, Eq, Hash)] pub struct MatchablePath(pub String); pub type PathMatch = (String, String); pub type MatchList = Vec<PathMatch>; diff --git a/src/routing/scripting.rs b/src/routing/scripting.rs index 2903e9f410af843984a2fcfa9aed24c05553dd18..460e4531453d550d6a8f029a2d514c63ca632c6a 100644 --- a/src/routing/scripting.rs +++ b/src/routing/scripting.rs @@ -1,14 +1,46 @@ use routing::request; use server::LuaRuntime; use rlua::{Lua}; +use scripting::run_script; -#[get("/__testing__/run-script")] -pub fn route_script(path: request::RequestPath, runtime: LuaRuntime) -> String { - let _lua: Lua = runtime.into(); //todo: Use This - let doowap = path.0; - let foo = request::path::MatchablePath(String::from(doowap)); - let matches = foo.matches(String::from("/inspection/123")); - println!("{:?}", matches); +use rocket::request::{FromForm, FormItems}; +use std::collections::HashMap; + +#[derive(Debug)] +pub struct ScriptParams { + pub script_name: String, + pub script_params: HashMap<String, String>, +} + +impl <'form> FromForm<'form> for ScriptParams { + type Error = (); + + fn from_form(items: &mut FormItems<'form>, _: bool) -> Result<ScriptParams, Self::Error> { + let mut script_name: Option<String> = None; + let mut script_params: HashMap<String, String> = HashMap::new(); + + for (key, value) in items { + match key.as_str() { + "script_path" if script_name.is_none() => { script_name = Some(String::from(value.as_str())); }, + _ => { script_params.insert(String::from(key.as_str()), String::from(value.as_str())); }, + }; + } + + match script_name { + Some(name) => Ok(ScriptParams { script_name: name, script_params }), + None => Err(()), + } + } +} + + +#[get("/__run_script__?<params>")] +pub fn route_script(params: ScriptParams, runtime: LuaRuntime) -> String { + let lua: Lua = runtime.into(); //todo: Use This + + println!("{:?}", params); + + run_script("example/.swerve/get_user.lua", &lua); String::from("Yes") } \ No newline at end of file diff --git a/src/scripting/run_script.rs b/src/scripting/run_script.rs index 316c27e8f878a57f308a1f687e95a62af643e278..85d927a54949b46fec3e2aa80acee2f637714f09 100644 --- a/src/scripting/run_script.rs +++ b/src/scripting/run_script.rs @@ -2,8 +2,9 @@ use std::convert::AsRef; use std::path::Path; use std::fs::File; use std::io::Read; +use rlua::Lua; -pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> { +pub fn run_script<P: AsRef<Path>>(path: P, mut lua: &Lua) -> Option<String> { let mut file = File::open(&path).unwrap(); let mut buf = String::new(); @@ -12,5 +13,9 @@ pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> { Err(_) => return None, } + println!("{}", buf); + + lua.eval::<()>(&buf, None); + Some(buf) } \ No newline at end of file diff --git a/src/server/server.rs b/src/server/server.rs index 37a1b9453d9ddb28ee397b4b369509eb4c239f63..c2af951ab1a88df486b61e11ae83147bb5979d25 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -6,7 +6,10 @@ pub fn create_server(args: Args, config: SwerveConfig) -> Rocket { let server_config = server_config_from_input(args.clone(), config.clone()); let mut server = Rocket::custom(server_config, false) .manage(args.clone()) - .manage(config.clone()); + .manage(config.clone()) + .manage(routing::ScriptMap::from_config(&config.clone())) + + .attach(routing::RedirectScripts); let quiet = args.flag_quiet;