mirror of
https://github.com/DefectingCat/candy
synced 2025-07-15 16:51:34 +00:00
feat: add lua script execture
This commit is contained in:
27
src/http/lua.rs
Normal file
27
src/http/lua.rs
Normal 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())?;
|
||||||
|
}
|
@ -4,6 +4,7 @@ use anyhow::anyhow;
|
|||||||
use axum::{Router, extract::DefaultBodyLimit, middleware, routing::get};
|
use axum::{Router, extract::DefaultBodyLimit, middleware, routing::get};
|
||||||
use axum_server::{Handle, tls_rustls::RustlsConfig};
|
use axum_server::{Handle, tls_rustls::RustlsConfig};
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
|
use mlua::Lua;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::{compression::CompressionLayer, timeout::TimeoutLayer};
|
use tower_http::{compression::CompressionLayer, timeout::TimeoutLayer};
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
@ -33,6 +34,9 @@ pub mod reverse_proxy;
|
|||||||
/// }
|
/// }
|
||||||
pub static HOSTS: LazyLock<DashMap<u16, SettingHost>> = LazyLock::new(DashMap::new);
|
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<()> {
|
pub async fn make_server(host: SettingHost) -> anyhow::Result<()> {
|
||||||
let mut router = Router::new();
|
let mut router = Router::new();
|
||||||
let host_to_save = host.clone();
|
let host_to_save = host.clone();
|
||||||
|
@ -173,7 +173,6 @@ pub async fn serve(
|
|||||||
let path_exists = match path_exists {
|
let path_exists = match path_exists {
|
||||||
Some(path_exists) => path_exists,
|
Some(path_exists) => path_exists,
|
||||||
None => {
|
None => {
|
||||||
error!("request {:?}", request);
|
|
||||||
let uri_path = uri.path();
|
let uri_path = uri.path();
|
||||||
// 如果请求路径不以 / 结尾,则返回 301 Moved Permanently 状态码
|
// 如果请求路径不以 / 结尾,则返回 301 Moved Permanently 状态码
|
||||||
if !uri_path.ends_with('/') {
|
if !uri_path.ends_with('/') {
|
||||||
@ -625,7 +624,6 @@ async fn list_dir(host_root_str: &str, path: &PathBuf) -> anyhow::Result<Vec<Dir
|
|||||||
size,
|
size,
|
||||||
last_modified,
|
last_modified,
|
||||||
};
|
};
|
||||||
error!("dir: {:?}", dir);
|
|
||||||
anyhow::Ok(dir)
|
anyhow::Ok(dir)
|
||||||
});
|
});
|
||||||
tasks.push(task);
|
tasks.push(task);
|
||||||
|
Reference in New Issue
Block a user