optimze router finder

This commit is contained in:
xfy
2024-05-21 13:24:29 +08:00
parent 94c4cc7799
commit d76a58d264
3 changed files with 28 additions and 19 deletions

View File

@ -11,16 +11,12 @@ keep_alive = 15
[[host.route]]
# index file name and format
index = ["index.html"]
# route path: GET /
location = "/www"
location = "/"
# static file path
root = "./html"
[[host.route]]
# index file name and format
index = ["index.htm"]
# route path: GET /
location = "/test"
# static file path
location = "/test/"
root = "./html"
# custom error page
[host.route.error_page]

View File

@ -134,10 +134,7 @@ pub async fn handle_connection(
let req_method = req.method();
// find route path
// TODO: router find optimize
let (router, assets_path) = find_route(req_path, &host.route_map)?;
debug!("router = {:?}", router);
debug!("assets_path = {}", assets_path);
// build the response for client
let mut res = Response::builder();

View File

@ -1,3 +1,5 @@
use tracing::debug;
use crate::error::{Error, Result};
use crate::config::{HostRouteMap, SettingRoute};
@ -43,21 +45,35 @@ pub fn find_route<'a>(
req_path: &'a str,
route_map: &'a HostRouteMap,
) -> Result<(&'a SettingRoute, &'a str)> {
let mut index = 1;
let len = req_path.len();
let not_found_err = format!("resource {} not found", &req_path).into();
let (router, assets_path) = loop {
if index > len {
return Err(Error::NotFound(not_found_err));
let not_found_err = format!("resource {} not found", &req_path);
// /public/www/test
// then find all stash's index
let all_stash = &req_path
.bytes()
.enumerate()
.filter(|(_, b)| *b == b'/')
.map(|(index, _)| index + 1)
.collect::<Vec<_>>();
// loop the all_stash
// /public/
// /public/www/
let mut all_stash_index = 0;
let (router, assets_index) = loop {
if all_stash_index >= all_stash.len() {
return Err(Error::NotFound(not_found_err.clone().into()));
}
let check_path = &req_path[..index];
match route_map.get(check_path) {
Some(router) => break (router, &req_path[index..]),
let index = all_stash[all_stash_index];
match route_map.get(&req_path[..index]) {
Some(router) => break (router, index),
None => {
index += 1;
all_stash_index += 1;
}
}
};
// rest path is assets_path /public/test -> test
let assets_path = &req_path[assets_index..];
debug!("router {:?}", &router);
debug!("assets_path {assets_path}");
Ok((router, assets_path))
}