进行一个修改
This commit is contained in:
parent
be814a0614
commit
834f027552
@ -123,6 +123,7 @@ mod tests {
|
||||
test_conf.cluster_id,
|
||||
test_conf.cluster_secret,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -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<bool>,
|
||||
cache_dir: Option<PathBuf>,
|
||||
) -> 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::<u32>().ok();
|
||||
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().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
|
||||
|
21
src/serve.rs
21
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<u8>)
|
||||
Data(Vec<u8>),
|
||||
}
|
||||
|
||||
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<u32>) -> MeasureRes {
|
||||
match header.get("x-openbmclapi-secret") {
|
||||
Some(secret) => {
|
||||
@ -60,12 +61,20 @@ pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> 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<HashMap<String, String>>, Path(hash): Path<String>) -> impl IntoResponse {
|
||||
pub async fn res_donwload(
|
||||
State(config): State<Config>,
|
||||
header: HeaderMap,
|
||||
Query(param): Query<HashMap<String, String>>,
|
||||
Path(hash): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
let hash = hash.to_lowercase();
|
||||
let file_path = config.cache_dir.join(hash_to_filename(&hash));
|
||||
|
||||
todo!();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user