diff --git a/src/cluster.rs b/src/cluster.rs index fc5117d..9c3a0c0 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -123,6 +123,7 @@ mod tests { test_conf.cluster_id, test_conf.cluster_secret, None, + None, ) } diff --git a/src/config.rs b/src/config.rs index 82bfaff..0da7bb1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,15 +1,14 @@ use { crate::fatal, log::{error, info, warn}, - std::{env, fs}, - serde::{Serialize, Deserialize} + serde::{Deserialize, Serialize}, + std::{env, fs, path::PathBuf}, }; const CONFIG_PATH: &str = "config.toml"; #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Config { - /// http or https /// CENTER_URL pub center_url: String, /// CLUSTER_IP @@ -22,6 +21,8 @@ pub struct Config { pub cluster_secret: String, /// NO_DEMAON pub no_demaon: bool, + /// cache dir + pub cache_dir: PathBuf, } impl Config { @@ -32,7 +33,16 @@ impl Config { cluster_id: String, cluster_secret: String, no_demaon: Option, + cache_dir: Option, ) -> Self { + // cache dir 默认: cwd + 'cache' + let cache_dir = if let Some(cache_dir) = cache_dir { + cache_dir + } else { + env::current_dir() + .unwrap_or(PathBuf::from(".")) + .join("cache") + }; Self { center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), host_ip: host_ip.unwrap_or("0.0.0.0".to_string()), @@ -40,6 +50,7 @@ impl Config { cluster_id, cluster_secret, no_demaon: no_demaon.unwrap_or(false), + cache_dir, } } @@ -49,12 +60,13 @@ impl Config { let host_ip = env::var("CLUSTER_IP").ok(); let host_port = env::var("CLUSTER_PORT").unwrap().parse::().ok(); let no_demaon = env::var("NO_DAEMON").unwrap().parse::().ok(); + let cache_dir = env::var("CACHE_DIR").ok().map(|x| PathBuf::from(x)); // Load from env - let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| { + let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| { fatal!("CLUSTER_ID must be set"); }); - let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { + let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| { fatal!("CLUSTER_SECRET must be set"); }); @@ -81,6 +93,7 @@ impl Config { cluster_id, cluster_secret, no_demaon, + cache_dir, ); // Save config diff --git a/src/serve.rs b/src/serve.rs index 2f7a92b..8ccf23b 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -1,7 +1,9 @@ +use crate::{config::Config, utils::hash_to_filename}; + use std::collections::HashMap; use axum::{ - extract::{Path, Query}, + extract::{Path, Query, State}, http::header::HeaderMap, http::StatusCode, response::{IntoResponse, Response}, @@ -10,7 +12,7 @@ use axum::{ pub enum MeasureRes { Forbidden, BadResquest, - Data(Vec) + Data(Vec), } impl IntoResponse for MeasureRes { @@ -18,7 +20,7 @@ impl IntoResponse for MeasureRes { match self { Self::Forbidden => (StatusCode::FORBIDDEN).into_response(), Self::BadResquest => (StatusCode::BAD_REQUEST).into_response(), - Self::Data(data) => (StatusCode::OK, data).into_response() + Self::Data(data) => (StatusCode::OK, data).into_response(), } } } @@ -45,7 +47,6 @@ impl IntoResponse for MeasureRes { /// /// export default MeasureRoute /// ``` -/// pub async fn measure(header: HeaderMap, Path(size): Path) -> MeasureRes { match header.get("x-openbmclapi-secret") { Some(secret) => { @@ -60,12 +61,20 @@ pub async fn measure(header: HeaderMap, Path(size): Path) -> MeasureRes { data.fill(114_u8); return MeasureRes::Data(data); } - None => MeasureRes::Forbidden + None => MeasureRes::Forbidden, } } /// 返回文件的请求函数 /// app.get('/download/:hash(\\w+)', async (req: Request, res: Response, next: NextFunction) => { -pub async fn res_donwload(header: HeaderMap, Query(param): Query>, Path(hash): Path) -> impl IntoResponse { +pub async fn res_donwload( + State(config): State, + header: HeaderMap, + Query(param): Query>, + Path(hash): Path, +) -> impl IntoResponse { + let hash = hash.to_lowercase(); + let file_path = config.cache_dir.join(hash_to_filename(&hash)); + todo!(); }