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

Make route params available on 'params' local table in lua

parent 665cb190
No related branches found
No related tags found
1 merge request!2Scripting
...@@ -10,11 +10,13 @@ r:set_body("This is my only response"); ...@@ -10,11 +10,13 @@ r:set_body("This is my only response");
--return r --return r
r = response(200, "application/json", json_encode({ foo = 123 })); print(params.user_id)
r:unset_body(); r = response(200, "application/json", json_encode({ user_id = params.user_id, path = params.script_path }));
r:set_status(204); --
--r:unset_body();
--r:set_status(204);
return r return r
-- return response(200, "application/json", '{ "foo": ' .. foo .. ' }') -- return response(200, "application/json", '{ "foo": ' .. foo .. ' }')
\ No newline at end of file
...@@ -19,13 +19,24 @@ pub fn run_script<P: AsRef<Path>>(path: P, mut lua: &Lua, params: HashMap<String ...@@ -19,13 +19,24 @@ pub fn run_script<P: AsRef<Path>>(path: P, mut lua: &Lua, params: HashMap<String
.file_name() .file_name()
.and_then(|name| name.to_str()); .and_then(|name| name.to_str());
let params_table = lua.create_table().ok()?; let params_table = serialize_table("params", params);
for (key, value) in params {
params_table.set(key, value); 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. output
lua.eval::<ScriptResponse>(&buf, file_name).map_err(|e| println!("{}", e)).ok() }
}
\ No newline at end of file
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