Browse Source

feat: rust assets demo

dev
Dnomd343 2 years ago
parent
commit
7cbecdc5f7
  1. 3
      .gitignore
  2. 1204
      src/assets/Cargo.lock
  3. 12
      src/assets/Cargo.toml
  4. 51
      src/assets/src/main.rs

3
.gitignore

@ -2,7 +2,10 @@
/build/
/.idea/
/assets/*.txt
/cmake-build/
/cmake-build-debug/
/cmake-build-release/
/src/assets/target/
/src/to-json/target/

1204
src/assets/Cargo.lock

File diff suppressed because it is too large

12
src/assets/Cargo.toml

@ -0,0 +1,12 @@
[package]
name = "assets"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
env_logger = "0.10.0"
log = "0.4.17"
reqwest = { version = "0.11.14", features = ["blocking", "deflate", "gzip", "brotli"] }
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] }

51
src/assets/src/main.rs

@ -0,0 +1,51 @@
use reqwest::Client;
use std::env::set_var;
use std::time::Duration;
const TIMEOUT: u64 = 60;
async fn asset_fetch(url: &str) -> Result<String, String> {
let client = Client::builder()
.timeout(Duration::from_secs(TIMEOUT))
.build().unwrap();
match client.get(url).send().await {
Ok(response) => {
match response.text().await {
Ok(text) => {
Ok(text)
},
Err(err) => Err(format!("http content error: {}", err))
}
},
Err(err) => Err(format!("http request failed: {}", err))
}
}
async fn demo() {
println!("demo function start");
match asset_fetch("https://res.343.re/Share/cleardns/gfwlist.txt").await {
Ok(data) => {
// println!("{}", data);
},
Err(err) => println!("error -> {}", err)
}
println!("demo function exit");
}
#[tokio::main]
async fn main() {
// set_var("RUST_LOG", "debug");
set_var("RUST_LOG", "trace");
env_logger::init();
demo().await;
println!("end demo");
}
Loading…
Cancel
Save