|
| 1 | +use crate::prelude::PathParam; |
| 2 | +use crate::{Handler, Request, Response, SilentError, StatusCode}; |
| 3 | +use async_trait::async_trait; |
| 4 | + |
| 5 | +struct HandlerWrapperStatic { |
| 6 | + path: String, |
| 7 | +} |
| 8 | + |
| 9 | +impl Default for HandlerWrapperStatic { |
| 10 | + fn default() -> Self { |
| 11 | + Self::new(".") |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +impl HandlerWrapperStatic { |
| 16 | + fn new(path: &str) -> Self { |
| 17 | + let mut path = path; |
| 18 | + if path.ends_with('/') { |
| 19 | + path = &path[..path.len() - 1]; |
| 20 | + } |
| 21 | + if !std::path::Path::new(path).is_dir() { |
| 22 | + panic!("Path not exists: {}", path); |
| 23 | + } |
| 24 | + Self { |
| 25 | + path: path.to_string(), |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[async_trait] |
| 31 | +impl Handler for HandlerWrapperStatic { |
| 32 | + async fn call(&self, req: Request) -> Result<Response, SilentError> { |
| 33 | + if let PathParam::Path(file_path) = req.get_path_params("path").unwrap() { |
| 34 | + let mut path = format!("{}/{}", self.path, file_path); |
| 35 | + if path.ends_with('/') { |
| 36 | + path.push_str("index.html"); |
| 37 | + } |
| 38 | + if let Ok(contents) = tokio::fs::read(path).await { |
| 39 | + return Ok(contents.into()); |
| 40 | + } |
| 41 | + }; |
| 42 | + Err(SilentError::BusinessError { |
| 43 | + code: StatusCode::NOT_FOUND, |
| 44 | + msg: "Not Found".to_string(), |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub fn static_handler(path: &str) -> impl Handler { |
| 50 | + HandlerWrapperStatic::new(path) |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::HandlerWrapperStatic; |
| 56 | + use crate::prelude::*; |
| 57 | + use crate::Handler; |
| 58 | + use crate::Request; |
| 59 | + use crate::SilentError; |
| 60 | + use crate::StatusCode; |
| 61 | + use bytes::Bytes; |
| 62 | + use http_body_util::BodyExt; |
| 63 | + |
| 64 | + static CONTENT: &str = r#"<!DOCTYPE html> |
| 65 | +<html> |
| 66 | +<head> |
| 67 | + <meta charset="utf-8"> |
| 68 | + <title>Silent</title> |
| 69 | +</head> |
| 70 | +<body> |
| 71 | +
|
| 72 | +<h1>我的第一个标题</h1> |
| 73 | +
|
| 74 | +<p>我的第一个段落。</p> |
| 75 | +
|
| 76 | +</body> |
| 77 | +</html>"#; |
| 78 | + |
| 79 | + fn create_static(path: &str) { |
| 80 | + if !std::path::Path::new(path).is_dir() { |
| 81 | + std::fs::create_dir(path).unwrap(); |
| 82 | + std::fs::write(format!("./{}/index.html", path), CONTENT).unwrap(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn clean_static(path: &str) { |
| 87 | + if std::path::Path::new(path).is_dir() { |
| 88 | + std::fs::remove_file(format!("./{}/index.html", path)).unwrap(); |
| 89 | + std::fs::remove_dir(path).unwrap(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + #[tokio::test] |
| 94 | + async fn test_static() { |
| 95 | + let path = "test_static"; |
| 96 | + create_static(path); |
| 97 | + let handler = HandlerWrapperStatic::new(path); |
| 98 | + let mut req = Request::default(); |
| 99 | + req.set_path_params("path".to_owned(), PathParam::Path("index.html".to_string())); |
| 100 | + let mut res = handler.call(req).await.unwrap(); |
| 101 | + clean_static(path); |
| 102 | + assert_eq!(res.status(), StatusCode::OK); |
| 103 | + assert_eq!( |
| 104 | + res.frame().await.unwrap().unwrap().data_ref().unwrap(), |
| 105 | + &Bytes::from(CONTENT) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + #[tokio::test] |
| 110 | + async fn test_static_default() { |
| 111 | + let path = "test_static_default"; |
| 112 | + create_static(path); |
| 113 | + let handler = HandlerWrapperStatic::new(path); |
| 114 | + let mut req = Request::default(); |
| 115 | + req.set_path_params("path".to_owned(), PathParam::Path("".to_string())); |
| 116 | + let mut res = handler.call(req).await.unwrap(); |
| 117 | + clean_static(path); |
| 118 | + assert_eq!(res.status(), StatusCode::OK); |
| 119 | + assert_eq!( |
| 120 | + res.frame().await.unwrap().unwrap().data_ref().unwrap(), |
| 121 | + &Bytes::from(CONTENT) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + #[tokio::test] |
| 126 | + async fn test_static_not_found() { |
| 127 | + let path = "test_static_not_found"; |
| 128 | + create_static(path); |
| 129 | + let handler = HandlerWrapperStatic::new(path); |
| 130 | + let mut req = Request::default(); |
| 131 | + req.set_path_params( |
| 132 | + "path".to_owned(), |
| 133 | + PathParam::Path("not_found.html".to_string()), |
| 134 | + ); |
| 135 | + let res = handler.call(req).await.unwrap_err(); |
| 136 | + clean_static(path); |
| 137 | + if let SilentError::BusinessError { code, .. } = res { |
| 138 | + assert_eq!(code, StatusCode::NOT_FOUND); |
| 139 | + } else { |
| 140 | + panic!(); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments