본문 바로가기
Programming/Rust

Rust 간단한 웹서버 HttpServer 올리기

by Chan_찬 2023. 4. 19.
728x90

제목 그대로 간단하게 Rust로 웹서버 올리기 - hello world! 찍어보기

프로젝트 생성

$ cargo new rust-actix-api

Cargo.toml 에 crate 추가

[dependencies]
actix-web = "3.3.2"

main.rs 수정

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

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

async fn get_data(web::Path(id): web::Path<String>) -> impl Responder {
    let data = format!("get_data {id}");
    HttpResponse::Ok().body(data)
}


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

build 하기

cargo build

run 하기

cargo run

cargo run

웹서버 테스트 curl

curl path

728x90
728x90

'Programming > Rust' 카테고리의 다른 글

docker compose - rust default 컨테이너 만들기  (0) 2023.04.11
Rust 의 await, Future, Poll 에 대해  (2) 2023.03.24
Rust match  (0) 2023.03.16
Rust 매크로 macro_rules!  (0) 2023.03.15
Rust trait - 인터페이스?, 추상클래스?  (0) 2023.03.14
Buy me a coffeeBuy me a coffee

댓글