[dependencies]
axum = "0.6.18"
hyper = { version = "0.14.26", features = ["full"] }
tokio = { version = "1.28.1", features = ["full"] }
tower = "0.4.13"
log = "0.4.17"
env_logger = "0.10.0"
http = "0.2.9"
use axum::{ routing::get, http::HeaderName,Router};
use hyper::{Body, Request};
async fn handle_request(req: Request<Body>) -> &'static str {
// 把这里封装成一个方法,每一个要验证的接口都调用这一个方法
let mut jieguo=true;
if let Some(aa) = req.headers().get(&HeaderName::from_static("aa")) {
// 输出aa字段的值
if aa.to_str().unwrap() != "aa100" {
println!("不是aa100");
jieguo=false;
}
println!("aa: {:?}", aa.to_str().unwrap());
}
else {
// 如果请求头中没有aa字段,则输出错误信息
println!("Missing 'aa' header");
}
if let Some(aa) = req.headers().get(&HeaderName::from_static("bb")) {
// 输出aa字段的值
if aa.to_str().unwrap() != "bb200" {
println!("不是bb200");
jieguo=false;
}
println!("bb: {:?}", aa.to_str().unwrap());
}
println!("jieguo: {:?}", jieguo);
if jieguo {
"正确"
}else {
"错误"
}
}
async fn root() -> &'static str {
println!("哈利路亚2");
"哈利路亚2"
}
async fn get_foo() {}
async fn post_foo() {}
async fn foo_bar() {}
#[tokio::main]
async fn main() {
// our router
let app = Router::new()
.route("/", get(handle_request))
.route("/cc", get(root))
.route("/foo", get(get_foo).post(post_foo))
.route("/foo/bar", get(foo_bar));
println!("\n\n\t\t项目已经启动\n\n");
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}