Skip to content
Snippets Groups Projects
Unverified Commit b3e672c7 authored by Louis's avatar Louis :fire:
Browse files

link up fairing for script running, get params from internal redirect

parent 023ae3c6
No related branches found
No related tags found
1 merge request!2Scripting
...@@ -2,9 +2,9 @@ use std::collections::HashMap; ...@@ -2,9 +2,9 @@ use std::collections::HashMap;
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub struct RouteHandler { pub struct RouteHandler {
route: String, pub route: String,
response: Option<ResponseHandler>, pub response: Option<ResponseHandler>,
script: Option<String>, pub script: Option<String>,
} }
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
......
...@@ -72,6 +72,7 @@ pub mod path { ...@@ -72,6 +72,7 @@ pub mod path {
use std::path as std_path; use std::path as std_path;
use std::option::Option::*; use std::option::Option::*;
#[derive(PartialEq, Eq, Hash)]
pub struct MatchablePath(pub String); pub struct MatchablePath(pub String);
pub type PathMatch = (String, String); pub type PathMatch = (String, String);
pub type MatchList = Vec<PathMatch>; pub type MatchList = Vec<PathMatch>;
......
use routing::request; use routing::request;
use server::LuaRuntime; use server::LuaRuntime;
use rlua::{Lua}; use rlua::{Lua};
use scripting::run_script;
#[get("/__testing__/run-script")] use rocket::request::{FromForm, FormItems};
pub fn route_script(path: request::RequestPath, runtime: LuaRuntime) -> String { use std::collections::HashMap;
let _lua: Lua = runtime.into(); //todo: Use This
let doowap = path.0; #[derive(Debug)]
let foo = request::path::MatchablePath(String::from(doowap)); pub struct ScriptParams {
let matches = foo.matches(String::from("/inspection/123")); pub script_name: String,
println!("{:?}", matches); 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") String::from("Yes")
} }
\ No newline at end of file
...@@ -2,8 +2,9 @@ use std::convert::AsRef; ...@@ -2,8 +2,9 @@ use std::convert::AsRef;
use std::path::Path; use std::path::Path;
use std::fs::File; use std::fs::File;
use std::io::Read; 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 file = File::open(&path).unwrap();
let mut buf = String::new(); let mut buf = String::new();
...@@ -12,5 +13,9 @@ pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> { ...@@ -12,5 +13,9 @@ pub fn run_script<P: AsRef<Path>>(path: P) -> Option<String> {
Err(_) => return None, Err(_) => return None,
} }
println!("{}", buf);
lua.eval::<()>(&buf, None);
Some(buf) Some(buf)
} }
\ No newline at end of file
...@@ -6,7 +6,10 @@ pub fn create_server(args: Args, config: SwerveConfig) -> Rocket { ...@@ -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 server_config = server_config_from_input(args.clone(), config.clone());
let mut server = Rocket::custom(server_config, false) let mut server = Rocket::custom(server_config, false)
.manage(args.clone()) .manage(args.clone())
.manage(config.clone()); .manage(config.clone())
.manage(routing::ScriptMap::from_config(&config.clone()))
.attach(routing::RedirectScripts);
let quiet = args.flag_quiet; let quiet = args.flag_quiet;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment