refactor handle files

add handle proxy
This commit is contained in:
xfy
2024-05-28 17:05:19 +08:00
parent bb0565181a
commit fd00cba57a
5 changed files with 55 additions and 14 deletions

View File

@ -28,5 +28,5 @@ page = "404.html"
[[host.route]]
index = ["index.html"]
location = "/www/test/"
root = "./html"
location = "/proxy/"
proxy_pass = "http://localhost:3000/"

View File

@ -20,11 +20,13 @@ pub struct SettingRoute {
/// The register route
pub location: String,
/// The static assets root folder
pub root: String,
pub root: Option<String>,
/// Index files format
#[serde(default = "host_index")]
pub index: Vec<String>,
pub error_page: Option<ErrorRoute>,
// reverse proxy url
pub proxy_pass: Option<String>,
}
pub type HostRouteMap = BTreeMap<String, SettingRoute>;

View File

@ -187,9 +187,12 @@ pub async fn handle_not_found(
) -> Result<Response<CandyBody<Bytes>>> {
let res = if let Some(err_page) = &router.error_page {
let res = res.status(err_page.status);
let path = parse_assets_path(assets_path, &router.root, &err_page.page);
// let path = format!("{}/{}", &assets_path, &err_page.page);
handle_get(req, res, &path).await?
if let Some(root) = &router.root {
let path = parse_assets_path(assets_path, root, &err_page.page);
handle_get(req, res, &path).await?
} else {
not_found()
}
} else {
not_found()
};

View File

@ -8,7 +8,7 @@ use std::{
};
use crate::{
config::SettingHost,
config::{SettingHost, SettingRoute},
consts::{NAME, VERSION},
error::{Error, Result},
http::{handle_get, handle_not_found, internal_server_error, not_found, CandyBody},
@ -17,7 +17,7 @@ use crate::{
use anyhow::anyhow;
use futures_util::Future;
use http::{Method, Request, Response};
use http::{response::Builder, Method, Request, Response};
use hyper::{
body::{Bytes, Incoming},
server::conn::http1,
@ -131,7 +131,6 @@ pub async fn handle_connection(
use Error::*;
let req_path = req.uri().path();
let req_method = req.method();
// find route path
let (router, assets_path) = find_route(req_path, &host.route_map)?;
@ -150,13 +149,49 @@ pub async fn handle_connection(
}
}
// reverse proxy
if router.proxy_pass.is_some() {
handle_proxy(router, assets_path).await
} else {
// static file
handle_file(router, assets_path, req, res).await
}
}
/// Handle reverse proxy
///
/// Only use with the `proxy_pass` field in config
async fn handle_proxy(
router: &SettingRoute,
assets_path: &str,
) -> Result<Response<CandyBody<Bytes>>> {
// check on outside
let proxy = router.proxy_pass.as_ref().ok_or(Error::Empty)?;
dbg!(proxy);
todo!()
}
/// Handle static files,
/// try find static file from local path
///
/// Only use with the `proxy_pass` field not in config
async fn handle_file(
router: &SettingRoute,
assets_path: &str,
req: &Request<Incoming>,
res: Builder,
) -> Result<Response<CandyBody<Bytes>>> {
let req_method = req.method();
// find resource local file path
let mut path = None;
for index in router.index.iter() {
let p = parse_assets_path(assets_path, &router.root, index);
if Path::new(&p).exists() {
path = Some(p);
break;
if let Some(root) = &router.root {
let p = parse_assets_path(assets_path, root, index);
if Path::new(&p).exists() {
path = Some(p);
break;
}
}
}
let path = match path {

View File

@ -85,9 +85,10 @@ mod tests {
fn find_route_works() {
let setting_route = SettingRoute {
location: "/".to_string(),
root: "./public".to_string(),
root: Some("./public".to_string()),
index: vec!["index.html".into()],
error_page: None,
proxy_pass: None,
};
let map = BTreeMap::from([("/".to_string(), setting_route)]);
let (_, assets_path) = find_route("/docs/home", &map).unwrap();