整个大的,继续收拾东西去了

This commit is contained in:
shenjack 2024-01-28 00:20:05 +08:00
parent c98acf824d
commit 837b6bbbbd
Signed by: shenjack
GPG Key ID: 7B1134A979775551
6 changed files with 77 additions and 25 deletions

View File

@ -27,6 +27,7 @@ sha1 = "0.10.6"
zstd = "0.13.0"
log = "0.4.20"
simple_logger = "4.3.3"
# [dependencies.db_logger]
# version = "0.1"
# optional = true

View File

@ -1,4 +1,4 @@
use crate::config::{Config};
use crate::config::Config;
use crate::utils::avro_data_to_file_list;
use crate::PROTOCOL_VERSION;
@ -63,7 +63,11 @@ impl Cluster {
let url = self.config.join_center_url("/openbmclapi/files");
let password = self.config.cluster_secret.clone();
let username = self.config.cluster_id.clone();
let client = Client::builder().user_agent(self.ua.clone()).build().unwrap();
let client = Client::builder()
.user_agent(self.ua.clone())
.build()
.unwrap();
info!("getting file list from: {}", url);
let res = client
.get(url)
.basic_auth(username, Some(password))
@ -78,6 +82,7 @@ impl Cluster {
match res.status() {
StatusCode::OK => {
let body = res.bytes().await.unwrap();
info!("got file list len: {}, decompressing", body.len());
let cur = std::io::Cursor::new(body);
let raw_data = decode_all(cur);
if raw_data.is_err() {
@ -117,12 +122,13 @@ mod tests {
test_conf.cluster_port,
test_conf.cluster_id,
test_conf.cluster_secret,
false,
None,
)
}
#[tokio::test]
async fn test_get_file_list() {
crate::log::init_log_with_cli();
let config = gen_config();
let cluster = Cluster::new(config);
cluster.get_file_list().await.unwrap();

View File

@ -1,7 +1,8 @@
use crate::fatal;
use {
log::{error, warn},
log::{error, info, warn},
serde::{Deserialize, Serialize},
serde_json::Result,
std::{env, fs},
};
@ -27,8 +28,8 @@ pub struct Config {
impl Config {
pub fn new(
center_url: Option<String>,
host_ip: Option<String>,
host_port: Option<u32>,
host_ip: String,
host_port: u32,
cluster_id: String,
cluster_secret: String,
no_demaon: Option<bool>,
@ -36,29 +37,39 @@ impl Config {
// https://openbmclapi.bangbang93.com
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()),
host_port: host_port.unwrap_or(8080),
host_ip,
host_port,
cluster_id,
cluster_secret,
no_demaon: no_demaon.unwrap_or(false),
}
}
pub fn convert_from_env() {
pub fn new_from_env() {
// Load from env
let center_url = env::var("CENTER_URL").ok();
let host_ip = env::var("CLUSTER_IP").ok();
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
let center_url = env::var("CENTER_URL");
let center_url = match center_url {
Ok(url) => Some(url),
Err(_) => {
info!("center url not set, use default");
None
}
};
let host_ip: String = env::var("CLUSTER_IP").unwrap_or("0.0.0.0".to_string());
let host_port = env::var("CLUSTER_PORT")
.unwrap_or("8080".to_string())
.parse::<u32>()
.unwrap_or_else(|_| {
fatal!("CLUSTER_PORT must be a number");
});
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
// Load from env
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| {
error!("CLUSTER_ID must be set");
panic!("{}", err);
fatal!("CLUSTER_ID must be set");
});
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| {
error!("CLUSTER_SECRET must be set");
panic!("{}", err);
fatal!("CLUSTER_SECRET must be set");
});
// Decrapated warning
@ -103,9 +114,9 @@ impl Config {
});
}
pub fn load() -> Result<Self> {
todo!("Not implemented yet")
}
// pub fn load() -> Result<Self> {
// todo!("Not implemented yet")
// }
pub fn join_center_url(&self, path: &str) -> String {
format!("{}{}", self.center_url, path)

View File

@ -1 +1,22 @@
pub fn init_logging() {}
pub fn init_log_with_cli() {
// 命令行参数
// --warn
// --debug
// --trace
// 从低级开始判断
let log_level;
if std::env::args().any(|x| x == "--trace") {
log_level = log::LevelFilter::Trace;
} else if std::env::args().any(|x| x == "--debug") {
log_level = log::LevelFilter::Debug;
} else if std::env::args().any(|x| x == "--warn") {
log_level = log::LevelFilter::Warn;
} else {
log_level = log::LevelFilter::Info;
}
simple_logger::SimpleLogger::new()
.with_level(log_level)
.env()
.init()
.unwrap();
}

View File

@ -6,4 +6,6 @@ mod utils;
pub const PROTOCOL_VERSION: &str = "1.6.11";
fn main() {}
fn main() {
log::init_log_with_cli();
}

View File

@ -1,12 +1,12 @@
use crate::cluster::{SyncFile};
use crate::cluster::SyncFile;
use std::io::Cursor;
use log::{info, warn};
use std::path::PathBuf;
use apache_avro::{from_avro_datum, from_value, types::Value};
use log::{info, warn};
use md5::{Digest, Md5};
use sha1::Sha1;
use std::path::PathBuf;
/// import {join} from 'path'
///
@ -92,6 +92,7 @@ pub fn avro_data_to_file_list(data: Vec<u8>) -> Option<Vec<SyncFile>> {
}
files.push(try_item.unwrap());
}
info!("parsed {} files", len);
Some(files)
}
_ => {
@ -101,6 +102,16 @@ pub fn avro_data_to_file_list(data: Vec<u8>) -> Option<Vec<SyncFile>> {
}
}
#[macro_export]
macro_rules! fatal {
($($arg:tt)+) => {
use log::error;
// error!() + panic!()
error!($($arg)+);
panic!($($arg)+);
};
}
#[test]
fn test_hash_to_filename() {
assert_eq!(