diff --git a/example/.swerve/get_user.lua b/example/.swerve/get_user.lua index b3d66f06d037f99bced4dc535d5e06faf8f57751..207b0db499edaac725b73d48947684c5b4d38b01 100644 --- a/example/.swerve/get_user.lua +++ b/example/.swerve/get_user.lua @@ -10,11 +10,13 @@ r:set_body("This is my only response"); --return r -r = response(200, "application/json", json_encode({ foo = 123 })); +print(params.user_id) -r:unset_body(); -r:set_status(204); +r = response(200, "application/json", json_encode({ user_id = params.user_id, path = params.script_path })); +-- +--r:unset_body(); +--r:set_status(204); return r --- return response(200, "application/json", '{ "foo": ' .. foo .. ' }') \ No newline at end of file +-- return response(200, "application/json", '{ "foo": ' .. foo .. ' }') diff --git a/src/scripting/run_script.rs b/src/scripting/run_script.rs index f459d58231dd3f1408621fd86e6fbfead4c4d746..d9992454a88cdabe2277998c93d2e0f12075f801 100644 --- a/src/scripting/run_script.rs +++ b/src/scripting/run_script.rs @@ -19,13 +19,24 @@ pub fn run_script<P: AsRef<Path>>(path: P, mut lua: &Lua, params: HashMap<String .file_name() .and_then(|name| name.to_str()); - let params_table = lua.create_table().ok()?; - for (key, value) in params { - params_table.set(key, value); - } + let params_table = serialize_table("params", params); + + lua.eval::<ScriptResponse>(&format!("{}\n{}", params_table, buf), file_name) + .map_err(|e| println!("{}", e)) + .ok() +} + +fn serialize_table(name: &'static str, table: HashMap<String, String>) -> String { + let mut output = format!("local {} = {{", name); + + let contents = table.iter() + .fold( + String::new(), + |acc, (key, value)| format!("{} {} = \"{}\",", acc, key, value) + ); - // TODO: Inject params_table in a per-request manner + output.push_str(&contents[0..contents.len() - 1]); + output.push_str(" }"); - params_table. - lua.eval::<ScriptResponse>(&buf, file_name).map_err(|e| println!("{}", e)).ok() -} \ No newline at end of file + output +}