feat: create an async fs lua module
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
83527a7f59
commit
a634528d02
@ -1,11 +1,12 @@
|
||||
local app = create_app()
|
||||
|
||||
app:post("/", function(req,res)
|
||||
for k,v in pairs(req.parts) do
|
||||
print("name", k)
|
||||
print("filename", v.filename)
|
||||
print("content", v.content)
|
||||
if req.parts.myfile == nil then
|
||||
res.status = 400
|
||||
res.body = "Missing myfile field"
|
||||
return res
|
||||
end
|
||||
fs:write("yololo.txt", req.parts.myfile.content)
|
||||
return res
|
||||
end)
|
||||
|
||||
|
||||
@ -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
16
src/api/fs.rs
Normal 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(())
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use std::error::Error;
|
||||
use std::{env, fs};
|
||||
|
||||
use tchoutchou::api::builtins::load_builtins;
|
||||
use tchoutchou::api::{load_apis, load_builtins};
|
||||
use tchoutchou::internal::core::runtime::Runtime;
|
||||
|
||||
#[tokio::main]
|
||||
@ -30,6 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
};
|
||||
|
||||
runtime.load(&load_builtins);
|
||||
runtime.load(&load_apis);
|
||||
runtime.start().await;
|
||||
|
||||
Ok(())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user