Actix Web

가으라늘·2023년 1월 3일
0

Actix Web

목록 보기
1/3
post-thumbnail

Actix.rs에 나와있는 내용을 보고 따라하면서 Actix Web 프로젝트 시작.

cargo new hello-world를 이용해 새 rust 프로젝트를 시작.
hello-world 경로로 들어가 Cargo.toml 파일 안의 [dependencies]에 actix-web = "4" 추가.
cargo build를 이용해 dependencies 설치.

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

#[post("/echo")] // post 방식
async fn echo(req_body: String) -> impl Responder {
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(echo)
            .route("/hey", web::get().to(manual_hello))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

#[get("/")]#[post("/")]를 이용해 요청 메소드의 방식과 url 주소를 정의할 수 있다. 또는 main 함수 내에서 .route("/", web::get().to(manual_hello)) 식으로 정의할 수도 있다.

cargo run으로 컴파일 후 실행하면 http://127.0.0.1:8080/에서 Hello World! 가 나오는 것을 볼 수 있다.\

📚참고자료

https://actix.rs/docs/application

profile
생각 없는 개발자

0개의 댓글