0%

初识actix

新建cargo项目

1
cargo new rust-web

编辑Cargo.toml

1
2
[dependencies]
actix-web = "4"

编写main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use actix_web::{get,web, App, HttpServer, Responder,HttpResponse};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello) //如果定义了get,post,直接用service
.route("/index", web::get().to(indexs)) //如果没有定义,需要用route方法
})
.workers(8) // 设置工作线程数量
.bind(("127.0.0.1", 8080))?
.run()
.await
}

async fn indexs() -> impl Responder {
HttpResponse::Ok().body("index")
}

#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}

启动

1
2
3
cargo run
或者
cargo build