-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,695 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 引入需要的库 | ||
use futures::future::join_all; | ||
use tokio::fs::File; | ||
use tokio::io::AsyncWriteExt; | ||
|
||
// 定义异步函数来进行数据处理 | ||
async fn process_data(data: String) -> String { | ||
// 假设这里有一些复杂的数据处理逻辑 | ||
data.to_uppercase() | ||
} | ||
|
||
// 异步函数,用于从网站获取数据 | ||
async fn fetch_data_from_website(url: &str) -> Result<String, reqwest::Error> { | ||
let response = reqwest::get(url).await?.text().await?; | ||
Ok(response) | ||
} | ||
|
||
// 异步函数,用于保存数据到文件 | ||
async fn save_to_file(filename: &str, data: &str) -> std::io::Result<()> { | ||
let mut file = File::create(filename).await?; | ||
file.write_all(data.as_bytes()).await?; | ||
Ok(()) | ||
} | ||
|
||
// 异步主函数,处理所有任务 | ||
async fn main_task() -> Result<(), Box<dyn std::error::Error>> { | ||
let urls = [ | ||
"https://jsonplaceholder.typicode.com/posts/1", | ||
"https://jsonplaceholder.typicode.com/posts/2", | ||
"https://jsonplaceholder.typicode.com/posts/3", | ||
]; | ||
|
||
// 创建一个 Vec 来保存所有任务 | ||
let mut tasks = vec![]; | ||
|
||
// 并发地发起网络请求并处理每个响应 | ||
for (idx, &url) in urls.iter().enumerate() { | ||
let task = async move { | ||
let data = fetch_data_from_website(url).await?; | ||
let processed_data = process_data(data).await; | ||
let filename = format!("output{}.txt", idx); | ||
save_to_file(&filename, &processed_data).await?; | ||
Ok(()) | ||
}; | ||
tasks.push(task); | ||
} | ||
|
||
// 并发地执行所有任务 | ||
let results: Vec<Result<(), Box<dyn std::error::Error>>> = join_all(tasks).await; | ||
for result in results { | ||
if let Err(err) = result { | ||
eprintln!("任务执行出错: {:?}", err); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// 入口函数,调用异步主函数并运行事件循环 | ||
#[tokio::main] | ||
async fn main() { | ||
if let Err(err) = main_task().await { | ||
eprintln!("程序执行出错: {:?}", err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use tokio::task; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let tasks = (0..5) | ||
.map(|i| { | ||
task::spawn(async move { | ||
println!("task {}", i); | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
// 等待所有任务完成 | ||
for task in tasks { | ||
task.await.unwrap(); | ||
} | ||
|
||
// 等待一段时间,以保证所有输出都打印完毕 | ||
sleep(Duration::from_secs(1)).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use axum::{ | ||
http::StatusCode, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// initialize tracing | ||
tracing_subscriber::fmt::init(); | ||
|
||
// build our application with a route | ||
let app = Router::new() | ||
// `GET /` goes to `root` | ||
.route("/", get(root)) | ||
// `POST /users` goes to `create_user` | ||
.route("/users", post(create_user)); | ||
|
||
// run our app with hyper, listening globally on port 3000 | ||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
|
||
// basic handler that responds with a static string | ||
async fn root() -> &'static str { | ||
"Hello, World!" | ||
} | ||
|
||
async fn create_user( | ||
// this argument tells axum to parse the request body | ||
// as JSON into a `CreateUser` type | ||
Json(payload): Json<CreateUser>, | ||
) -> (StatusCode, Json<User>) { | ||
// insert your application logic here | ||
let user = User { | ||
id: 1337, | ||
username: payload.username, | ||
}; | ||
|
||
// this will be converted into a JSON response | ||
// with a status code of `201 Created` | ||
(StatusCode::CREATED, Json(user)) | ||
} | ||
|
||
// the input to our `create_user` handler | ||
#[derive(Deserialize)] | ||
struct CreateUser { | ||
username: String, | ||
} | ||
|
||
// the output to our `create_user` handler | ||
#[derive(Serialize)] | ||
struct User { | ||
id: u64, | ||
username: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
let handles: Vec<_> = (0..5) | ||
.map(|i| { | ||
thread::spawn(move || { | ||
println!("thread {}", i); | ||
}) | ||
}) | ||
.collect(); | ||
|
||
// 等待所有线程完成 | ||
for handle in handles { | ||
handle.join().unwrap(); | ||
} | ||
|
||
// 等待一段时间,以保证所有线程的输出都打印出来 | ||
thread::sleep(Duration::from_secs(1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
|
||
use super::verify_path; | ||
|
||
#[derive(Debug, Parser)] | ||
pub enum HttpSubCommand { | ||
#[command(about = "Serve a directory over HTTP")] | ||
Serve(HttpServeOpts), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct HttpServeOpts { | ||
#[arg(short, long, value_parser = verify_path, default_value = ".")] | ||
pub dir: PathBuf, | ||
#[arg(short, long, default_value_t = 8080)] | ||
pub port: u16, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod b64; | ||
mod csv_convert; | ||
mod gen_pass; | ||
mod http_serve; | ||
mod text; | ||
|
||
pub use b64::{process_decode, process_encode}; | ||
pub use csv_convert::process_csv; | ||
pub use gen_pass::process_genpass; | ||
pub use http_serve::process_http_serve; | ||
pub use text::{process_text_key_generate, process_text_sign, process_text_verify}; |
Oops, something went wrong.