赞
踩
在看书的时候,用到了actix-web这个框架的案例。
书里面的版本是1.0,但是我看官网最新都4.4了。
为了抹平这种信息差,所以我决定把官方提供的示例代码过一遍。
Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4.4"
env_logger = "0.11"
log = "0.4"
main.rs
use actix_web::{middleware, web, App, HttpRequest, HttpServer}; async fn index(req: HttpRequest) -> &'static str { println!("REQ: {req:?}"); "Hello world!" } #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); log::info!("starting HTTP server at http://192.168.77.129:8000"); let server = HttpServer::new(|| { App::new() .wrap(middleware::Logger::default()) .service(web::resource("/index.html").to(|| async {"Hello html!"})) .service(web::resource("/").to(index)) }); server.bind("0.0.0.0:8000")? .run() .await }
http://192.168.77.129:8000/
http://192.168.77.129:8000/index.html
引入依赖:
use actix_web::{middleware, web, App, HttpRequest, HttpServer};
首页路由:
index(req: HttpRequest)
&'static str
async fn index(req: HttpRequest) -> &'static str {
println!("REQ: {req:?}");
"Hello world!"
}
入口方法:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://192.168.77.129:8000");
let server = HttpServer::new(|| {
.wrap(middleware::Logger::default())
/index.html
:.service(web::resource("/index.html").to(|| async {"Hello html!"}))
/
: .service(web::resource("/").to(index))
server.bind("0.0.0.0:8000")?
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://192.168.77.129:8000");
let server = HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/index.html").to(|| async {"Hello html!"}))
.service(web::resource("/").to(index))
});
server.bind("0.0.0.0:8000")?
.run()
.await
}
actix-web框架还支持对web进行测试。
核心代码如下:
let app = App::new().route("/", web::get().to(index));
let app = test::init_service(app).await;
let req = test::TestRequest::get().uri("/").to_request();
let resp = app.call(req).await?;
assert_eq!(resp.status(), http::StatusCode::OK);
let response_body = resp.into_body();
assert_eq!(to_bytes(response_body).await?, r##"Hello world!"##);
#[cfg(test)] mod tests { use actix_web::{body::to_bytes, dev::Service, http, test, Error}; use super::*; #[actix_web::test] async fn test_index() -> Result<(), Error> { let app = App::new().route("/", web::get().to(index)); let app = test::init_service(app).await; let req = test::TestRequest::get().uri("/").to_request(); let resp = app.call(req).await?; assert_eq!(resp.status(), http::StatusCode::OK); let response_body = resp.into_body(); assert_eq!(to_bytes(response_body).await?, r##"Hello world!"##); Ok(()) } }
完整代码:
use actix_web::{middleware, web, App, HttpRequest, HttpServer}; async fn index(req: HttpRequest) -> &'static str { println!("REQ: {req:?}"); "Hello world!" } #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); log::info!("starting HTTP server at http://192.168.77.129:8000"); let server = HttpServer::new(|| { App::new() .wrap(middleware::Logger::default()) .service(web::resource("/index.html").to(|| async {"Hello html!"})) .service(web::resource("/").to(index)) }); server.bind("0.0.0.0:8000")? .run() .await } #[cfg(test)] mod tests { use actix_web::{body::to_bytes, dev::Service, http, test, Error}; use super::*; #[actix_web::test] async fn test_index() -> Result<(), Error> { let app = App::new().route("/", web::get().to(index)); let app = test::init_service(app).await; let req = test::TestRequest::get().uri("/").to_request(); let resp = app.call(req).await?; assert_eq!(resp.status(), http::StatusCode::OK); let response_body = resp.into_body(); assert_eq!(to_bytes(response_body).await?, r##"Hello world!"##); Ok(()) } }
执行测试:
(base) zhangdapeng@zhangdapeng:~/code/rust/hello$ cargo test
warning: `/home/zhangdapeng/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
warning: `/home/zhangdapeng/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.14s
Running unittests src/main.rs (target/debug/deps/hello-4420b7c0e788b52b)
running 1 test
test tests::test_index ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。