mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
12
basics/hello-world/Cargo.toml
Normal file
12
basics/hello-world/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "hello-world"
|
||||
version = "2.0.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
env_logger = "0.8"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "1"
|
50
basics/hello-world/src/main.rs
Normal file
50
basics/hello-world/src/main.rs
Normal file
@ -0,0 +1,50 @@
|
||||
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<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_web::dev::Service;
|
||||
use actix_web::{http, test, web, App, Error};
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_index() -> Result<(), Error> {
|
||||
let app = App::new().route("/", web::get().to(index));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let req = test::TestRequest::get().uri("/").to_request();
|
||||
let resp = app.call(req).await.unwrap();
|
||||
|
||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||
|
||||
let response_body = match resp.response().body().as_ref() {
|
||||
Some(actix_web::body::Body::Bytes(bytes)) => bytes,
|
||||
_ => panic!("Response error"),
|
||||
};
|
||||
|
||||
assert_eq!(response_body, r##"Hello world!"##);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user