From b3e672c764c15d71a8f406467197ddd7f6c2a3e2 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Thu, 26 Apr 2018 15:02:03 +0100 Subject: [PATCH] link up fairing for script running, get params from internal redirect --- src/cli/config_routes.rs | 6 ++--- src/routing/request.rs | 1 + src/routing/scripting.rs | 46 +++++++++++++++++++++++++++++++------ src/scripting/run_script.rs | 7 +++++- src/server/server.rs | 5 +++- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/cli/config_routes.rs b/src/cli/config_routes.rs index 62cf89d..b9396f7 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 68931d5..7e21068 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 2903e9f..460e453 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 316c27e..85d927a 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 37a1b94..c2af951 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; -- GitLab