luarocks support

This commit is contained in:
2025-01-27 21:20:59 +01:00
parent 1f017dc24b
commit d765dd9404
5 changed files with 165 additions and 301 deletions

View File

@@ -1,13 +1,12 @@
mod icon;
use std::{collections::HashMap, fs, io::ErrorKind, path::PathBuf, str::FromStr};
use std::{collections::HashMap, fs, io::ErrorKind, path::PathBuf, str::FromStr, sync::LazyLock};
use icon::extract_icon;
use mlua::{
FromLua, Lua, Number, Table,
Lua, Number, Table,
Value::{self as LuaValue},
};
use once_cell::sync::{Lazy, OnceCell};
use axum::{
extract::Request,
@@ -17,7 +16,6 @@ use axum::{
routing::get,
};
use base64::prelude::*;
use reqwest::Method;
use serde::Serialize;
use tera::{to_value, Context, Result as TeraResult, Tera, Value};
use time::OffsetDateTime;
@@ -28,37 +26,38 @@ use tower_http::services::ServeDir;
pub type AError = Box<dyn std::error::Error>;
pub type AResult<T> = Result<T, AError>;
pub static CWD: Lazy<PathBuf> = Lazy::new(|| std::env::current_dir().unwrap());
pub static CWD: LazyLock<PathBuf> = LazyLock::new(|| std::env::current_dir().unwrap());
#[derive(Debug, Clone)]
struct ArrayBuffer(Vec<u8>);
// use reqwest::Method;
// #[derive(Debug, Clone)]
// struct ArrayBuffer(Vec<u8>);
impl mlua::FromLua for ArrayBuffer {
fn from_lua(value: LuaValue, _: &Lua) -> mlua::Result<Self> {
if let Some(data) = value.as_str() {
return Ok(Self(data.as_bytes().to_owned()));
}
// impl mlua::FromLua for ArrayBuffer {
// fn from_lua(value: LuaValue, _: &Lua) -> mlua::Result<Self> {
// if let Some(data) = value.as_str() {
// return Ok(Self(data.as_bytes().to_owned()));
// }
if let Some(data) = value.as_table() {
let mut out = vec![];
for pair in data.pairs::<mlua::Value, mlua::Value>() {
let (_, v) = pair?;
if let Some(value) = v.as_integer() {
out.push(value as u8);
}
}
return Ok(Self(out));
}
// if let Some(data) = value.as_table() {
// let mut out = vec![];
// for pair in data.pairs::<mlua::Value, mlua::Value>() {
// let (_, v) = pair?;
// if let Some(value) = v.as_integer() {
// out.push(value as u8);
// }
// }
// return Ok(Self(out));
// }
Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "ArrayBuffer".to_string(),
message: None,
})
}
}
// Err(mlua::Error::FromLuaConversionError {
// from: value.type_name(),
// to: "ArrayBuffer".to_string(),
// message: None,
// })
// }
// }
pub static TERA: Lazy<Tera> = Lazy::new(|| {
pub static TERA: LazyLock<Tera> = LazyLock::new(|| {
let mut tera = Tera::default();
tera.add_raw_template(
"index.html.jinja",
@@ -74,7 +73,7 @@ pub static TERA: Lazy<Tera> = Lazy::new(|| {
tera
});
pub static ICONS: Lazy<HashMap<&str, String>> = Lazy::new(|| {
pub static ICONS: LazyLock<HashMap<&str, String>> = LazyLock::new(|| {
let mut map = HashMap::new();
map.insert(
"unknown",
@@ -188,7 +187,39 @@ fn md(value: &Value, _args: &HashMap<String, Value>) -> TeraResult<Value> {
Ok(to_value(value).unwrap())
}
pub static BASE_DIR: OnceCell<PathBuf> = OnceCell::new();
pub static LUA_CPATH: LazyLock<String> = LazyLock::new(|| {
String::from_utf8_lossy(
&std::process::Command::new("luarocks")
.arg("path")
.arg("--lr-cpath")
.arg("--full")
.output()
.map_err::<std::vec::Vec<u8>, _>(|_| vec![])
.map(|r| r.stdout)
.unwrap_or_default(),
)
.trim()
.to_string()
});
pub static LUA_PATH: LazyLock<String> = LazyLock::new(|| {
String::from_utf8_lossy(
&std::process::Command::new("luarocks")
.arg("path")
.arg("--lr-path")
.arg("--full")
.output()
.map_err::<std::vec::Vec<u8>, _>(|_| vec![])
.map(|r| r.stdout)
.unwrap_or_default(),
)
.trim()
.to_string()
});
pub static BASE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
PathBuf::from_str(&std::env::var("BASE_DIR").unwrap_or(String::from("./public"))).unwrap()
});
#[derive(Debug, Serialize)]
struct FileInfo {
@@ -201,61 +232,62 @@ struct FileInfo {
description: Option<String>,
}
#[derive(Debug, Clone)]
struct FetchOptions {
method: String,
headers: HashMap<String, String>,
body: Option<ArrayBuffer>,
}
// #[derive(Debug, Clone)]
// struct FetchOptions {
// method: String,
// headers: HashMap<String, String>,
// body: Option<ArrayBuffer>,
// }
impl Default for FetchOptions {
fn default() -> Self {
Self {
method: "GET".to_string(),
headers: HashMap::new(),
body: None,
}
}
}
// impl Default for FetchOptions {
// fn default() -> Self {
// Self {
// method: "GET".to_string(),
// headers: HashMap::new(),
// body: None,
// }
// }
// }
impl FromLua for FetchOptions {
fn from_lua(value: LuaValue, _lua: &Lua) -> mlua::Result<Self> {
let Some(options) = value.as_table() else {
return Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "FetchOptions".to_string(),
message: Some("options must be a table".to_string()),
});
};
// impl mlua::FromLua for FetchOptions {
// fn from_lua(value: LuaValue, _lua: &Lua) -> mlua::Result<Self> {
// let Some(options) = value.as_table() else {
// return Err(mlua::Error::FromLuaConversionError {
// from: value.type_name(),
// to: "FetchOptions".to_string(),
// message: Some("options must be a table".to_string()),
// });
// };
let mut result = FetchOptions::default();
if let Ok(method) = options.get::<String>("method") {
result.method = method;
}
if let Ok(headers) = options.get::<HashMap<String, String>>("headers") {
result.headers = headers;
}
if let Ok(body) = options.get::<Option<ArrayBuffer>>("body") {
result.body = body;
}
// let mut result = FetchOptions::default();
// if let Ok(method) = options.get::<String>("method") {
// result.method = method;
// }
// if let Ok(headers) = options.get::<HashMap<String, String>>("headers") {
// result.headers = headers;
// }
// if let Ok(body) = options.get::<Option<ArrayBuffer>>("body") {
// result.body = body;
// }
Ok(result)
}
}
// Ok(result)
// }
// }
#[tokio::main]
async fn main() -> AResult<()> {
let listener = tokio::net::TcpListener::bind("[::]:3000").await.unwrap();
println!("Server running on port 3000");
Ok(axum::serve(listener, file_handler.into_make_service()).await?)
}
async fn file_handler(request: Request) -> impl IntoResponse {
std::env::set_current_dir(&*CWD).unwrap();
let base_dir: &PathBuf = BASE_DIR.get_or_init(|| {
PathBuf::from_str(&std::env::var("BASE_DIR").unwrap_or(String::from("./public"))).unwrap()
});
// let base_dir: &PathBuf = BASE_DIR.get_or_init(|| {
// PathBuf::from_str(&std::env::var("BASE_DIR").unwrap_or(String::from("./public"))).unwrap()
// });
let base_dir = base_dir.canonicalize().map_err(|e| {
let base_dir = BASE_DIR.canonicalize().map_err(|e| {
dbg!(e);
(StatusCode::NOT_FOUND, "404: Not Found".to_string())
})?;
@@ -282,142 +314,21 @@ async fn file_handler(request: Request) -> impl IntoResponse {
.map_err(|_| (StatusCode::NOT_FOUND, "404: Not Found".to_string()));
};
let lua = Lua::new();
let lua = unsafe { Lua::unsafe_new() };
// Fix load paths
lua.load(format!(
"package.path = \"{}\"\npackage.cpath = \"{}\"",
&*LUA_PATH, &*LUA_CPATH
))
.exec()
.map_err(|e| {
eprintln!("Lua Error: {:#?}", e);
render_lua_error(e)
})?;
let globals = lua.globals();
use mlua::{ExternalResult, LuaSerdeExt};
let fetch = lua
.create_function(|lua, params: (String, Option<FetchOptions>)| {
let uri = params.0;
let options = params.1;
let options = options.unwrap_or_default();
let Ok(method) = Method::from_bytes(options.method.as_bytes()) else {
return Err(mlua::Error::RuntimeError("Invalid method".to_string()));
};
let Ok(uri) = reqwest::Url::parse(&uri) else {
return Err(mlua::Error::RuntimeError("Invalid uri".to_string()));
};
let mut request = reqwest::blocking::Request::new(method, uri);
// let mut headers = ;
for (k, v) in &options.headers {
let Ok(k) = reqwest::header::HeaderName::from_str(k) else {
return Err(mlua::Error::RuntimeError("Invalid header name".to_string()));
};
let Ok(v) = reqwest::header::HeaderValue::from_str(v) else {
return Err(mlua::Error::RuntimeError(
"Invalid header value".to_string(),
));
};
let _ = request.headers_mut().try_append(k, v);
}
*request.body_mut() = options.body.map(|f| f.0.into());
let client = reqwest::blocking::Client::new();
let resp = client
.execute(request)
.and_then(|resp| resp.error_for_status())
.into_lua_err()?;
let json = resp.bytes().into_lua_err()?;
lua.to_value(&json.to_vec())
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let base64_encode = lua
.create_function(|lua, bytes: ArrayBuffer| {
let b64 = BASE64_STANDARD.encode(&bytes.0);
lua.to_value(&b64)
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let base64_decode = lua
.create_function(|lua, data: ArrayBuffer| {
let b64 = BASE64_STANDARD.decode(&data.0).into_lua_err()?;
lua.to_value(&b64)
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let json_decode = lua
.create_function(|lua, data: ArrayBuffer| {
let data = String::from_utf8_lossy(&data.0);
let json = serde_json::from_str::<serde_json::Value>(&data).into_lua_err()?; //data.json::<serde_json::Value>().into_lua_err()?;
lua.to_value(&json)
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let to_string = lua
.create_function(|lua, data: ArrayBuffer| {
let data = String::from_utf8_lossy(&data.0);
lua.to_value(&data)
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let dbg = lua
.create_function(|_, value: LuaValue| {
dbg!(value);
Ok(())
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
globals.set("fetch", fetch).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
globals.set("base64_encode", base64_encode).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
globals.set("base64_decode", base64_decode).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
globals.set("json_decode", json_decode).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
// globals.set("fetch_b64", fetch_b64).map_err(|e| {
// eprintln!("Lua Error: {:?}", e);
// render_lua_error(e)
// })?;
globals.set("dbg", dbg).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
globals.set("to_string", to_string).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let request_table = lua.create_table().map_err(|e| {
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
@@ -537,6 +448,25 @@ async fn file_handler(request: Request) -> impl IntoResponse {
// TODO: FIX THIS, THIS IS DISGUSTING
std::env::set_current_dir(full_base_path).unwrap();
// Before executing user-code, make sure they can't screw with `global`
lua.load(
r#"
setmetatable(_G, {
__newindex = function (_, n)
error("attempt to write to undeclared variable "..n, 2)
end,
__index = function (_, n)
error("attempt to read undeclared variable "..n, 2)
end,
})"#,
)
.exec()
.map_err(|e| {
std::env::set_current_dir(&*CWD).unwrap();
eprintln!("Lua Error: {:?}", e);
render_lua_error(e)
})?;
let script = lua.load(script).eval::<LuaValue>().map_err(|e| {
std::env::set_current_dir(&*CWD).unwrap();
eprintln!("Lua Error: {:?}", e);
@@ -687,8 +617,7 @@ fn render_lua_error(e: mlua::Error) -> (StatusCode, String) {
async fn handler(request: Request) -> Result<Html<String>, (StatusCode, String)> {
let start = Instant::now();
let base_dir = BASE_DIR.get().unwrap();
let mut path = base_dir.clone();
let mut path = BASE_DIR.clone();
let uri = urlencoding::decode(&request.uri().path()[1..]).map_err(|e| {
println!("{:?}", e);
@@ -696,12 +625,12 @@ async fn handler(request: Request) -> Result<Html<String>, (StatusCode, String)>
})?;
path.push(&*uri);
let full_path = fs::canonicalize(&path).map_err(|e| {
let full_path = path.canonicalize().map_err(|e| {
println!("{:?}", e);
(StatusCode::NOT_FOUND, "404: Not Found".to_string())
})?;
let full_base_path = fs::canonicalize(base_dir).map_err(|e| {
let full_base_path = BASE_DIR.canonicalize().map_err(|e| {
println!("{:?}", e);
(StatusCode::NOT_FOUND, "404: Not Found".to_string())
})?;
@@ -732,9 +661,8 @@ async fn handler(request: Request) -> Result<Html<String>, (StatusCode, String)>
}
async fn render_dir(path: &PathBuf, time: Instant) -> AResult<String> {
let base_dir = BASE_DIR.get().unwrap();
let time2 = Instant::now();
let base = base_dir.display().to_string();
let base = BASE_DIR.display().to_string();
let dirname = path
.display()
.to_string()