mirror of
https://github.com/DefectingCat/candy
synced 2025-07-15 16:51:34 +00:00
feat: add lua script route
This commit is contained in:
@ -1,27 +1,49 @@
|
|||||||
|
use anyhow::Context;
|
||||||
|
use axum::{
|
||||||
|
body::Body,
|
||||||
|
extract::{Path, Request},
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
use axum_extra::extract::Host;
|
||||||
|
use http::Uri;
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
http::{HOSTS, error::RouteError, serve::resolve_parent_path},
|
||||||
|
utils::parse_port_from_host,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::error::RouteResult;
|
||||||
|
|
||||||
pub async fn lua(
|
pub async fn lua(
|
||||||
req_uri: Uri,
|
req_uri: Uri,
|
||||||
path: Option<Path<String>>,
|
path: Option<Path<String>>,
|
||||||
Host(host): Host,
|
Host(host): Host,
|
||||||
mut req: Request<Body>,
|
req: Request<Body>,
|
||||||
) -> RouteResult<impl IntoResponse> {
|
) -> RouteResult<impl IntoResponse> {
|
||||||
let req_path = req.uri().path();
|
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 scheme = req.uri().scheme_str().unwrap_or("http");
|
||||||
let port = parse_port_from_host(&host, scheme).ok_or(RouteError::BadRequest())?;
|
let port = parse_port_from_host(&host, scheme).ok_or(RouteError::BadRequest())?;
|
||||||
let route_map = &HOSTS.get(&port).ok_or(RouteError::BadRequest())?.route_map;
|
let route_map = &HOSTS
|
||||||
|
.get(&port)
|
||||||
|
.ok_or(RouteError::BadRequest())
|
||||||
|
.with_context(|| {
|
||||||
|
format!("Hosts not found for port: {port}, host: {host}, scheme: {scheme}")
|
||||||
|
})?
|
||||||
|
.route_map;
|
||||||
tracing::debug!("Route map entries: {:?}", route_map);
|
tracing::debug!("Route map entries: {:?}", route_map);
|
||||||
|
|
||||||
let parent_path = resolve_parent_path(&req_uri, path.as_ref());
|
let parent_path = resolve_parent_path(&req_uri, path.as_ref());
|
||||||
let route_config = route_map
|
let route_config = route_map
|
||||||
.get(&parent_path)
|
.get(&parent_path)
|
||||||
.ok_or(RouteError::RouteNotFound())?;
|
.ok_or(RouteError::RouteNotFound())
|
||||||
|
.with_context(|| format!("route not found: {parent_path}"))?;
|
||||||
let lua_script = route_config
|
let lua_script = route_config
|
||||||
.lua_script
|
.lua_script
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(RouteError::InternalError())?;
|
.ok_or(RouteError::InternalError())
|
||||||
|
.with_context(|| "lua script not found")?;
|
||||||
|
error!("Lua script: {lua_script}");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ pub mod error;
|
|||||||
pub mod serve;
|
pub mod serve;
|
||||||
// handle reverse proxy
|
// handle reverse proxy
|
||||||
pub mod reverse_proxy;
|
pub mod reverse_proxy;
|
||||||
|
// handle lua script
|
||||||
|
pub mod lua;
|
||||||
|
|
||||||
/// Host configuration
|
/// Host configuration
|
||||||
/// use virtual host port as key
|
/// use virtual host port as key
|
||||||
@ -45,8 +47,17 @@ pub async fn make_server(host: SettingHost) -> anyhow::Result<()> {
|
|||||||
// register routes
|
// register routes
|
||||||
for host_route in &host.route {
|
for host_route in &host.route {
|
||||||
// lua script
|
// lua script
|
||||||
if let Some(lua_path) = &host_route.lua_script {
|
if host_route.lua_script.is_some() {
|
||||||
// papare lua script
|
// papare lua script
|
||||||
|
router = router.route(host_route.location.as_ref(), get(lua::lua));
|
||||||
|
let route_path = format!("{}{{*path}}", host_route.location);
|
||||||
|
router = router.route(route_path.as_ref(), get(lua::lua));
|
||||||
|
// save route path to map
|
||||||
|
{
|
||||||
|
host_to_save
|
||||||
|
.route_map
|
||||||
|
.insert(host_route.location.clone(), host_route.clone());
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user