Compare commits
No commits in common. "a634528d02d435baa158f14d1d50d05e8104fee0" and "84d7e20baa65c807f3cf764b51edfb7606297c0c" have entirely different histories.
a634528d02
...
84d7e20baa
@ -1,12 +1,11 @@
|
||||
local app = create_app()
|
||||
|
||||
app:post("/", function(req,res)
|
||||
if req.parts.myfile == nil then
|
||||
res.status = 400
|
||||
res.body = "Missing myfile field"
|
||||
return res
|
||||
for k,v in pairs(req.parts) do
|
||||
print("name", k)
|
||||
print("filename", v.filename)
|
||||
print("content", v.content)
|
||||
end
|
||||
fs:write("yololo.txt", req.parts.myfile.content)
|
||||
return res
|
||||
end)
|
||||
|
||||
|
||||
14
src/api/builtins.rs
Normal file
14
src/api/builtins.rs
Normal file
@ -0,0 +1,14 @@
|
||||
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();
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
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,22 +1 @@
|
||||
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();
|
||||
}
|
||||
pub mod builtins;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use std::error::Error;
|
||||
use std::{env, fs};
|
||||
|
||||
use tchoutchou::api::{load_apis, load_builtins};
|
||||
use tchoutchou::api::builtins::load_builtins;
|
||||
use tchoutchou::internal::core::runtime::Runtime;
|
||||
|
||||
#[tokio::main]
|
||||
@ -30,7 +30,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
};
|
||||
|
||||
runtime.load(&load_builtins);
|
||||
runtime.load(&load_apis);
|
||||
runtime.start().await;
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -132,13 +132,10 @@ impl Service<Request<Body>> for Svc {
|
||||
.unwrap();
|
||||
|
||||
match handler.call_async::<_, Res>((req, res)).await {
|
||||
Ok(res) => {
|
||||
let body = if res.json != "" { res.json } else { res.body };
|
||||
Ok(Response::builder()
|
||||
.status(res.status)
|
||||
.body(Body::from(body))
|
||||
.unwrap())
|
||||
}
|
||||
Ok(res) => Ok(Response::builder()
|
||||
.status(res.status)
|
||||
.body(Body::from(res.body))
|
||||
.unwrap()),
|
||||
Err(err) => {
|
||||
eprintln!("{}", err);
|
||||
Ok(Response::builder()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user