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

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" zstd = "0.13.0"
log = "0.4.20" log = "0.4.20"
simple_logger = "4.3.3"
# [dependencies.db_logger] # [dependencies.db_logger]
# version = "0.1" # version = "0.1"
# optional = true # 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::utils::avro_data_to_file_list;
use crate::PROTOCOL_VERSION; use crate::PROTOCOL_VERSION;
@ -63,7 +63,11 @@ impl Cluster {
let url = self.config.join_center_url("/openbmclapi/files"); let url = self.config.join_center_url("/openbmclapi/files");
let password = self.config.cluster_secret.clone(); let password = self.config.cluster_secret.clone();
let username = self.config.cluster_id.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 let res = client
.get(url) .get(url)
.basic_auth(username, Some(password)) .basic_auth(username, Some(password))
@ -78,6 +82,7 @@ impl Cluster {
match res.status() { match res.status() {
StatusCode::OK => { StatusCode::OK => {
let body = res.bytes().await.unwrap(); let body = res.bytes().await.unwrap();
info!("got file list len: {}, decompressing", body.len());
let cur = std::io::Cursor::new(body); let cur = std::io::Cursor::new(body);
let raw_data = decode_all(cur); let raw_data = decode_all(cur);
if raw_data.is_err() { if raw_data.is_err() {
@ -117,12 +122,13 @@ mod tests {
test_conf.cluster_port, test_conf.cluster_port,
test_conf.cluster_id, test_conf.cluster_id,
test_conf.cluster_secret, test_conf.cluster_secret,
false, None,
) )
} }
#[tokio::test] #[tokio::test]
async fn test_get_file_list() { async fn test_get_file_list() {
crate::log::init_log_with_cli();
let config = gen_config(); let config = gen_config();
let cluster = Cluster::new(config); let cluster = Cluster::new(config);
cluster.get_file_list().await.unwrap(); cluster.get_file_list().await.unwrap();

View File

@ -1,7 +1,8 @@
use crate::fatal;
use { use {
log::{error, warn}, log::{error, info, warn},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
serde_json::Result,
std::{env, fs}, std::{env, fs},
}; };
@ -27,8 +28,8 @@ pub struct Config {
impl Config { impl Config {
pub fn new( pub fn new(
center_url: Option<String>, center_url: Option<String>,
host_ip: Option<String>, host_ip: String,
host_port: Option<u32>, host_port: u32,
cluster_id: String, cluster_id: String,
cluster_secret: String, cluster_secret: String,
no_demaon: Option<bool>, no_demaon: Option<bool>,
@ -36,29 +37,39 @@ impl Config {
// https://openbmclapi.bangbang93.com // https://openbmclapi.bangbang93.com
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_port: host_port.unwrap_or(8080), host_port,
cluster_id, cluster_id,
cluster_secret, cluster_secret,
no_demaon: no_demaon.unwrap_or(false), no_demaon: no_demaon.unwrap_or(false),
} }
} }
pub fn convert_from_env() { pub fn new_from_env() {
// Load from env // Load from env
let center_url = env::var("CENTER_URL").ok(); let center_url = env::var("CENTER_URL");
let host_ip = env::var("CLUSTER_IP").ok(); let center_url = match center_url {
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok(); 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(); let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
// 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(|err| {
error!("CLUSTER_ID must be set"); fatal!("CLUSTER_ID must be set");
panic!("{}", err);
}); });
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| {
error!("CLUSTER_SECRET must be set"); fatal!("CLUSTER_SECRET must be set");
panic!("{}", err);
}); });
// Decrapated warning // Decrapated warning
@ -103,9 +114,9 @@ impl Config {
}); });
} }
pub fn load() -> Result<Self> { // pub fn load() -> Result<Self> {
todo!("Not implemented yet") // todo!("Not implemented yet")
} // }
pub fn join_center_url(&self, path: &str) -> String { pub fn join_center_url(&self, path: &str) -> String {
format!("{}{}", self.center_url, path) 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"; 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 std::io::Cursor;
use log::{info, warn};
use std::path::PathBuf;
use apache_avro::{from_avro_datum, from_value, types::Value}; use apache_avro::{from_avro_datum, from_value, types::Value};
use log::{info, warn};
use md5::{Digest, Md5}; use md5::{Digest, Md5};
use sha1::Sha1; use sha1::Sha1;
use std::path::PathBuf;
/// import {join} from 'path' /// 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()); files.push(try_item.unwrap());
} }
info!("parsed {} files", len);
Some(files) 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] #[test]
fn test_hash_to_filename() { fn test_hash_to_filename() {
assert_eq!( assert_eq!(