handle static file

This commit is contained in:
xfy
2024-04-25 11:50:03 +08:00
parent 52cc5e1047
commit 8128c6fff2
2 changed files with 15 additions and 4 deletions

View File

@ -5,7 +5,9 @@ use serde::Deserialize;
#[derive(Deserialize, Clone, Debug)]
pub struct SettingRoute {
/// The register route
pub location: String,
/// The static assets root folder
pub root: String,
}
@ -16,6 +18,7 @@ pub struct SettingHost {
route: Vec<Option<SettingRoute>>,
#[serde(skip_deserializing, skip_serializing)]
pub route_map: BTreeMap<String, SettingRoute>,
/// Index files format
pub index: Vec<String>,
}

View File

@ -8,7 +8,7 @@ use hyper::{
body::{Bytes, Frame, Incoming as IncomingBody},
server::conn::http1,
service::service_fn,
Request, Response, StatusCode,
Method, Request, Response, StatusCode,
};
use hyper_util::rt::TokioIo;
use tokio::{fs::File, net::TcpListener};
@ -17,6 +17,8 @@ use tracing::{error, warn};
use crate::config::SettingHost;
type CandyBody<T, E = Error> = BoxBody<T, E>;
impl SettingHost {
pub fn mk_server(&'static self) -> impl Future<Output = anyhow::Result<()>> + 'static {
let addr = format!("{}:{}", self.ip, self.port);
@ -66,17 +68,23 @@ async fn handle_connection(
.route_map
.get(req_path)
.ok_or(Error::NotFound(format!("route {} not found", req_path)))?;
let test = find_static_path(req_path, "/");
dbg!(test, router);
let assets_path = find_static_path(req_path, &router.location).unwrap_or("/");
let _index = &host.index;
let path = if assets_path.ends_with('/') {
format!("{}{}{}", router.root, assets_path, host.index[0])
} else {
format!("{}{}/{}", router.root, assets_path, host.index[0])
};
dbg!(&path);
let res = match req_method {
&Method::GET => handle_file(&path).await?,
// Return the 404 Not Found for other routes.
_ => not_found(),
};
Ok(res)
}
type CandyBody<T, E = Error> = BoxBody<T, E>;
async fn handle_file(path: &str) -> Result<Response<CandyBody<Bytes>>> {
// Open file for reading
let file = File::open(path).await;