add actix-web

This commit is contained in:
2020-04-18 02:26:07 +08:00
parent ef06866d10
commit 1267c4ed30
3 changed files with 1519 additions and 0 deletions

1475
actix-web/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
actix-web/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "actix-web"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "2"
actix-rt = "1"

32
actix-web/src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
use actix_web::{get, web, App, HttpServer, Responder, HttpRequest, HttpResponse};
#[get("/{id}/{name}/index.html")]
async fn index(info: web::Path<(u32, String)>) -> impl Responder {
format!("Hello {}! id:{}", info.1, info.0)
}
#[get("/name")]
async fn index_name() -> impl Responder {
format!("Hello name!\n")
}
async fn index2(req: HttpRequest) -> HttpResponse {
println!("{:?}", req);
HttpResponse::Ok()
.content_type("text/plain")
.body("Welcome!\n")
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
println!("Listen at: http://127.0.0.1:8080/");
HttpServer::new(|| App::new()
.service(index)
.service(index_name)
.service(web::resource("/").to(index2))
)
.bind("127.0.0.1:8080")?
.run()
.await
}