feat: create an async fs lua module
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
ato 2023-02-16 19:04:50 +01:00
parent 83527a7f59
commit a634528d02
5 changed files with 45 additions and 20 deletions

View File

@ -1,11 +1,12 @@
local app = create_app() local app = create_app()
app:post("/", function(req,res) app:post("/", function(req,res)
for k,v in pairs(req.parts) do if req.parts.myfile == nil then
print("name", k) res.status = 400
print("filename", v.filename) res.body = "Missing myfile field"
print("content", v.content) return res
end end
fs:write("yololo.txt", req.parts.myfile.content)
return res return res
end) end)

View File

@ -1,14 +0,0 @@
use mlua::Lua;
use std::time::Duration;
use tokio::time::sleep;
pub fn load_builtins(lua: &Lua) {
let sleep = lua
.create_async_function(|_l, n: u64| async move {
sleep(Duration::from_secs(n)).await;
Ok(())
})
.unwrap();
lua.globals().set("sleep", sleep).unwrap();
}

16
src/api/fs.rs Normal file
View File

@ -0,0 +1,16 @@
use mlua::UserData;
#[derive(Clone)]
pub struct FS {}
impl UserData for FS {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method(
"write",
|_l, _this, (filename, contents): (String, String)| async move {
tokio::fs::write(filename, contents).await.unwrap();
Ok(())
},
)
}
}

View File

@ -1 +1,22 @@
pub mod builtins; pub mod fs;
use mlua::Lua;
use std::time::Duration;
use tokio::time::sleep;
use self::fs::FS;
pub fn load_builtins(lua: &Lua) {
let sleep = lua
.create_async_function(|_l, n: u64| async move {
sleep(Duration::from_secs(n)).await;
Ok(())
})
.unwrap();
lua.globals().set("sleep", sleep).unwrap();
}
pub fn load_apis(lua: &Lua) {
lua.globals().set("fs", FS {}).unwrap();
}

View File

@ -1,7 +1,7 @@
use std::error::Error; use std::error::Error;
use std::{env, fs}; use std::{env, fs};
use tchoutchou::api::builtins::load_builtins; use tchoutchou::api::{load_apis, load_builtins};
use tchoutchou::internal::core::runtime::Runtime; use tchoutchou::internal::core::runtime::Runtime;
#[tokio::main] #[tokio::main]
@ -30,6 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}; };
runtime.load(&load_builtins); runtime.load(&load_builtins);
runtime.load(&load_apis);
runtime.start().await; runtime.start().await;
Ok(()) Ok(())