进行一个修改
This commit is contained in:
parent
be814a0614
commit
834f027552
@ -123,6 +123,7 @@ mod tests {
|
|||||||
test_conf.cluster_id,
|
test_conf.cluster_id,
|
||||||
test_conf.cluster_secret,
|
test_conf.cluster_secret,
|
||||||
None,
|
None,
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
use {
|
use {
|
||||||
crate::fatal,
|
crate::fatal,
|
||||||
log::{error, info, warn},
|
log::{error, info, warn},
|
||||||
std::{env, fs},
|
serde::{Deserialize, Serialize},
|
||||||
serde::{Serialize, Deserialize}
|
std::{env, fs, path::PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "config.toml";
|
const CONFIG_PATH: &str = "config.toml";
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// http or https
|
|
||||||
/// CENTER_URL
|
/// CENTER_URL
|
||||||
pub center_url: String,
|
pub center_url: String,
|
||||||
/// CLUSTER_IP
|
/// CLUSTER_IP
|
||||||
@ -22,6 +21,8 @@ pub struct Config {
|
|||||||
pub cluster_secret: String,
|
pub cluster_secret: String,
|
||||||
/// NO_DEMAON
|
/// NO_DEMAON
|
||||||
pub no_demaon: bool,
|
pub no_demaon: bool,
|
||||||
|
/// cache dir
|
||||||
|
pub cache_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
@ -32,7 +33,16 @@ impl Config {
|
|||||||
cluster_id: String,
|
cluster_id: String,
|
||||||
cluster_secret: String,
|
cluster_secret: String,
|
||||||
no_demaon: Option<bool>,
|
no_demaon: Option<bool>,
|
||||||
|
cache_dir: Option<PathBuf>,
|
||||||
) -> Self {
|
) -> 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 {
|
Self {
|
||||||
center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()),
|
center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()),
|
||||||
host_ip: host_ip.unwrap_or("0.0.0.0".to_string()),
|
host_ip: host_ip.unwrap_or("0.0.0.0".to_string()),
|
||||||
@ -40,6 +50,7 @@ impl Config {
|
|||||||
cluster_id,
|
cluster_id,
|
||||||
cluster_secret,
|
cluster_secret,
|
||||||
no_demaon: no_demaon.unwrap_or(false),
|
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_ip = env::var("CLUSTER_IP").ok();
|
||||||
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
|
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
|
||||||
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().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
|
// 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");
|
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");
|
fatal!("CLUSTER_SECRET must be set");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -81,6 +93,7 @@ impl Config {
|
|||||||
cluster_id,
|
cluster_id,
|
||||||
cluster_secret,
|
cluster_secret,
|
||||||
no_demaon,
|
no_demaon,
|
||||||
|
cache_dir,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Save config
|
// 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 std::collections::HashMap;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, Query},
|
extract::{Path, Query, State},
|
||||||
http::header::HeaderMap,
|
http::header::HeaderMap,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
@ -10,7 +12,7 @@ use axum::{
|
|||||||
pub enum MeasureRes {
|
pub enum MeasureRes {
|
||||||
Forbidden,
|
Forbidden,
|
||||||
BadResquest,
|
BadResquest,
|
||||||
Data(Vec<u8>)
|
Data(Vec<u8>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for MeasureRes {
|
impl IntoResponse for MeasureRes {
|
||||||
@ -18,7 +20,7 @@ impl IntoResponse for MeasureRes {
|
|||||||
match self {
|
match self {
|
||||||
Self::Forbidden => (StatusCode::FORBIDDEN).into_response(),
|
Self::Forbidden => (StatusCode::FORBIDDEN).into_response(),
|
||||||
Self::BadResquest => (StatusCode::BAD_REQUEST).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
|
/// export default MeasureRoute
|
||||||
/// ```
|
/// ```
|
||||||
///
|
|
||||||
pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> MeasureRes {
|
pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> MeasureRes {
|
||||||
match header.get("x-openbmclapi-secret") {
|
match header.get("x-openbmclapi-secret") {
|
||||||
Some(secret) => {
|
Some(secret) => {
|
||||||
@ -60,12 +61,20 @@ pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> MeasureRes {
|
|||||||
data.fill(114_u8);
|
data.fill(114_u8);
|
||||||
return MeasureRes::Data(data);
|
return MeasureRes::Data(data);
|
||||||
}
|
}
|
||||||
None => MeasureRes::Forbidden
|
None => MeasureRes::Forbidden,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 返回文件的请求函数
|
/// 返回文件的请求函数
|
||||||
/// app.get('/download/:hash(\\w+)', async (req: Request, res: Response, next: NextFunction) => {
|
/// 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!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user