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;
#[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)]
......
......@@ -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>;
......
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
......@@ -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
......@@ -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;
......
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