feat: add lua script execture

This commit is contained in:
xfy
2025-06-29 20:24:13 +08:00
parent a5041197c7
commit f0da74a06e
3 changed files with 31 additions and 2 deletions

27
src/http/lua.rs Normal file
View File

@ -0,0 +1,27 @@
pub async fn lua(
req_uri: Uri,
path: Option<Path<String>>,
Host(host): Host,
mut req: Request<Body>,
) -> RouteResult<impl IntoResponse> {
let req_path = req.uri().path();
let path_query = req
.uri()
.path_and_query()
.map(|v| v.as_str())
.unwrap_or(req_path);
let scheme = req.uri().scheme_str().unwrap_or("http");
let port = parse_port_from_host(&host, scheme).ok_or(RouteError::BadRequest())?;
let route_map = &HOSTS.get(&port).ok_or(RouteError::BadRequest())?.route_map;
tracing::debug!("Route map entries: {:?}", route_map);
let parent_path = resolve_parent_path(&req_uri, path.as_ref());
let route_config = route_map
.get(&parent_path)
.ok_or(RouteError::RouteNotFound())?;
let lua_script = route_config
.lua_script
.as_ref()
.ok_or(RouteError::InternalError())?;
}

View File

@ -4,6 +4,7 @@ use anyhow::anyhow;
use axum::{Router, extract::DefaultBodyLimit, middleware, routing::get};
use axum_server::{Handle, tls_rustls::RustlsConfig};
use dashmap::DashMap;
use mlua::Lua;
use tower::ServiceBuilder;
use tower_http::{compression::CompressionLayer, timeout::TimeoutLayer};
use tracing::{debug, info, warn};
@ -33,6 +34,9 @@ pub mod reverse_proxy;
/// }
pub static HOSTS: LazyLock<DashMap<u16, SettingHost>> = LazyLock::new(DashMap::new);
/// lua 脚本执行器
pub static LUA_EXECUTOR: LazyLock<Lua> = LazyLock::new(Lua::new);
pub async fn make_server(host: SettingHost) -> anyhow::Result<()> {
let mut router = Router::new();
let host_to_save = host.clone();

View File

@ -173,7 +173,6 @@ pub async fn serve(
let path_exists = match path_exists {
Some(path_exists) => path_exists,
None => {
error!("request {:?}", request);
let uri_path = uri.path();
// 如果请求路径不以 / 结尾,则返回 301 Moved Permanently 状态码
if !uri_path.ends_with('/') {
@ -625,7 +624,6 @@ async fn list_dir(host_root_str: &str, path: &PathBuf) -> anyhow::Result<Vec<Dir
size,
last_modified,
};
error!("dir: {:?}", dir);
anyhow::Ok(dir)
});
tasks.push(task);