mirror of
https://github.com/lune-org/lune.git
synced 2025-04-19 03:13:54 +01:00
Merge branch 'main' into feature/cross-compilation
This commit is contained in:
commit
8d09f4d0ec
4 changed files with 54 additions and 11 deletions
|
@ -1,4 +1,7 @@
|
||||||
use std::{collections::HashMap, net::Ipv4Addr};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
net::{IpAddr, Ipv4Addr},
|
||||||
|
};
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
@ -6,7 +9,7 @@ use reqwest::Method;
|
||||||
|
|
||||||
use super::util::table_to_hash_map;
|
use super::util::table_to_hash_map;
|
||||||
|
|
||||||
const DEFAULT_IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
|
const DEFAULT_IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
||||||
|
|
||||||
const WEB_SOCKET_UPDGRADE_REQUEST_HANDLER: &str = r#"
|
const WEB_SOCKET_UPDGRADE_REQUEST_HANDLER: &str = r#"
|
||||||
return {
|
return {
|
||||||
|
@ -155,7 +158,7 @@ impl FromLua<'_> for RequestConfig {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ServeConfig<'a> {
|
pub struct ServeConfig<'a> {
|
||||||
pub address: Ipv4Addr,
|
pub address: IpAddr,
|
||||||
pub handle_request: LuaFunction<'a>,
|
pub handle_request: LuaFunction<'a>,
|
||||||
pub handle_web_socket: Option<LuaFunction<'a>>,
|
pub handle_web_socket: Option<LuaFunction<'a>>,
|
||||||
}
|
}
|
||||||
|
@ -175,7 +178,7 @@ impl<'lua> FromLua<'lua> for ServeConfig<'lua> {
|
||||||
let handle_request: Option<LuaFunction> = t.get("handleRequest")?;
|
let handle_request: Option<LuaFunction> = t.get("handleRequest")?;
|
||||||
let handle_web_socket: Option<LuaFunction> = t.get("handleWebSocket")?;
|
let handle_web_socket: Option<LuaFunction> = t.get("handleWebSocket")?;
|
||||||
if handle_request.is_some() || handle_web_socket.is_some() {
|
if handle_request.is_some() || handle_web_socket.is_some() {
|
||||||
let address: Ipv4Addr = match &address {
|
let address: IpAddr = match &address {
|
||||||
Some(addr) => {
|
Some(addr) => {
|
||||||
let addr_str = addr.to_str()?;
|
let addr_str = addr.to_str()?;
|
||||||
|
|
||||||
|
|
|
@ -18,21 +18,30 @@ impl LuaRequest {
|
||||||
let path = self.head.uri.path().to_string();
|
let path = self.head.uri.path().to_string();
|
||||||
let body = lua.create_string(&self.body)?;
|
let body = lua.create_string(&self.body)?;
|
||||||
|
|
||||||
let query: HashMap<String, String> = self
|
let query: HashMap<LuaString, LuaString> = self
|
||||||
.head
|
.head
|
||||||
.uri
|
.uri
|
||||||
.query()
|
.query()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.split('&')
|
.split('&')
|
||||||
.filter_map(|q| q.split_once('='))
|
.filter_map(|q| q.split_once('='))
|
||||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
.map(|(k, v)| {
|
||||||
.collect();
|
let k = lua.create_string(k)?;
|
||||||
let headers: HashMap<String, Vec<u8>> = self
|
let v = lua.create_string(v)?;
|
||||||
|
Ok((k, v))
|
||||||
|
})
|
||||||
|
.collect::<LuaResult<_>>()?;
|
||||||
|
|
||||||
|
let headers: HashMap<LuaString, LuaString> = self
|
||||||
.head
|
.head
|
||||||
.headers
|
.headers
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
|
.map(|(k, v)| {
|
||||||
.collect();
|
let k = lua.create_string(k.as_str())?;
|
||||||
|
let v = lua.create_string(v.as_bytes())?;
|
||||||
|
Ok((k, v))
|
||||||
|
})
|
||||||
|
.collect::<LuaResult<_>>()?;
|
||||||
|
|
||||||
TableBuilder::new(lua)?
|
TableBuilder::new(lua)?
|
||||||
.with_value("method", method)?
|
.with_value("method", method)?
|
||||||
|
|
|
@ -2,7 +2,7 @@ use mlua::prelude::*;
|
||||||
|
|
||||||
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
|
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
|
||||||
use mlua_luau_scheduler::LuaSpawnExt;
|
use mlua_luau_scheduler::LuaSpawnExt;
|
||||||
use tokio::io::{self, AsyncWriteExt};
|
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
use crate::lune::util::{
|
use crate::lune::util::{
|
||||||
formatting::{
|
formatting::{
|
||||||
|
@ -21,6 +21,7 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable<'_>> {
|
||||||
.with_function("format", stdio_format)?
|
.with_function("format", stdio_format)?
|
||||||
.with_async_function("write", stdio_write)?
|
.with_async_function("write", stdio_write)?
|
||||||
.with_async_function("ewrite", stdio_ewrite)?
|
.with_async_function("ewrite", stdio_ewrite)?
|
||||||
|
.with_async_function("readToEnd", stdio_read_to_end)?
|
||||||
.with_async_function("prompt", stdio_prompt)?
|
.with_async_function("prompt", stdio_prompt)?
|
||||||
.build_readonly()
|
.build_readonly()
|
||||||
}
|
}
|
||||||
|
@ -53,6 +54,21 @@ async fn stdio_ewrite(_: &Lua, s: LuaString<'_>) -> LuaResult<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
FUTURE: Figure out how to expose some kind of "readLine" function using a buffered reader.
|
||||||
|
|
||||||
|
This is a bit tricky since we would want to be able to use **both** readLine and readToEnd
|
||||||
|
in the same script, doing something like readLine, readLine, readToEnd from lua, and
|
||||||
|
having that capture the first two lines and then read the rest of the input.
|
||||||
|
*/
|
||||||
|
|
||||||
|
async fn stdio_read_to_end(lua: &Lua, _: ()) -> LuaResult<LuaString> {
|
||||||
|
let mut input = Vec::new();
|
||||||
|
let mut stdin = io::stdin();
|
||||||
|
stdin.read_to_end(&mut input).await?;
|
||||||
|
lua.create_string(&input)
|
||||||
|
}
|
||||||
|
|
||||||
async fn stdio_prompt(lua: &Lua, options: PromptOptions) -> LuaResult<PromptResult> {
|
async fn stdio_prompt(lua: &Lua, options: PromptOptions) -> LuaResult<PromptResult> {
|
||||||
lua.spawn_blocking(move || prompt(options))
|
lua.spawn_blocking(move || prompt(options))
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -58,6 +58,9 @@ end
|
||||||
stdio.write("World! ")
|
stdio.write("World! ")
|
||||||
stdio.write("All on the same line")
|
stdio.write("All on the same line")
|
||||||
stdio.ewrite("\nAnd some error text, too")
|
stdio.ewrite("\nAnd some error text, too")
|
||||||
|
|
||||||
|
-- Reading the entire input from stdin
|
||||||
|
local input = stdio.readToEnd()
|
||||||
```
|
```
|
||||||
]=]
|
]=]
|
||||||
local stdio = {}
|
local stdio = {}
|
||||||
|
@ -143,4 +146,16 @@ function stdio.write(s: string) end
|
||||||
]=]
|
]=]
|
||||||
function stdio.ewrite(s: string) end
|
function stdio.ewrite(s: string) end
|
||||||
|
|
||||||
|
--[=[
|
||||||
|
@within Stdio
|
||||||
|
@tag must_use
|
||||||
|
|
||||||
|
Reads the entire input from stdin.
|
||||||
|
|
||||||
|
@return The input from stdin
|
||||||
|
]=]
|
||||||
|
function stdio.readToEnd(): string
|
||||||
|
return nil :: any
|
||||||
|
end
|
||||||
|
|
||||||
return stdio
|
return stdio
|
||||||
|
|
Loading…
Add table
Reference in a new issue