diff --git a/docs/package.json b/docs/package.json index 8fa7abe..67b56e6 100644 --- a/docs/package.json +++ b/docs/package.json @@ -51,5 +51,5 @@ "engines": { "node": ">=18.0" }, - "packageManager": "pnpm@10.12.4" + "packageManager": "pnpm@10.13.0" } diff --git a/docs/pnpm-workspace.yaml b/docs/pnpm-workspace.yaml new file mode 100644 index 0000000..7e8a367 --- /dev/null +++ b/docs/pnpm-workspace.yaml @@ -0,0 +1,4 @@ +onlyBuiltDependencies: + - '@swc/core' + - core-js + - core-js-pure diff --git a/src/http/serve.rs b/src/http/serve.rs index 82079c5..a363edc 100644 --- a/src/http/serve.rs +++ b/src/http/serve.rs @@ -149,7 +149,6 @@ pub async fn serve( .with_context(|| format!("route not found: {parent_path}"))?; debug!("route: {:?}", host_route); // after route found - // TODO: Check auto_index enable // check static file root configuration // if root is None, then return InternalError let Some(ref root) = host_route.root else { @@ -176,6 +175,31 @@ pub async fn serve( }; debug!("request index file {:?}", path_arr); debug!("req_path: {:?}", req_path); + + // 检查是否开启自动生成目录索引 + let uri_path = uri.path(); + debug!("uri_path: {:?}", uri_path); + let uri_path_vec = uri_path.split('/').collect::>(); + let uri_path_last = uri_path_vec.last(); + debug!("uri_path_last: {:?}", uri_path_last); + let uri_path_last = uri_path_last.unwrap_or(&""); + if host_route.auto_index && !uri_path_last.contains('.') { + // HTML 中的标题路径,需要移除掉配置文件中的 root = "./html" 字段 + let host_root = if let Some(root) = &host_route.root { + root + } else { + return custom_page(host_route, request, false).await; + }; + let req_path_str = req_path.to_string_lossy(); + debug!("req_path_str: {:?}", req_path_str); + let host_root = &req_path_str.strip_prefix(host_root).unwrap_or(host_root); + let list = list_dir(&req_path_str, &req_path).await?; + let list_html = render_list_html(host_root, list); + let mut headers = HeaderMap::new(); + headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); + return Ok((headers, list_html).into_response()); + } + // Try each candidate path in order: // - Return the first successfully streamed file. // - If all fail, return a `RouteNotFound` error.