2025년 4월 20일 일요일

5-4-2강: 네트워킹과 IPC(웹서버)

 1. 간단한 웹 서버 만들기

/ 에 접근하면 Hello World 출력하고, 다른 리소스 접근시 404 not found 출력하는 예제

//간단한 웹서버 만들기 예제
/*의존성 추가
[dependencies]
tokio = { version = "1.25.0", features = ["full"] }
hyper = { version = "0.14", features = ["full"] }
 */
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Server, StatusCode};

// 에러 처리를 위한 타입 정의
type GenericError = Box<dyn std::error::Error + Send + Sync>;
type Result<T> = std::result::Result<T, GenericError>;

// 클라이언트의 요청에 따라 적절한 응답을 반환하는 비동기 함수
async fn response_examples(req: Request<Body>) -> Result<Response<Body>> {
    // 기본 페이지 내용
    let index_html = String::from("<h1>Hello World!</h>");
    // 없는 페이지 요청 시 출력할 내용
    let notfound_html = String::from("<h1>404 not found</h>");

    // HTTP 메서드(GET)와 요청 경로(/)에 따라 응답 처리
    match (req.method(), req.uri().path()) {
        // GET / 요청이면 index 페이지 반환
        (&Method::GET, "/") => Ok(Response::new(index_html.into())),
        _ => {
            // 그 외의 요청은 404 Not Found 응답
            Ok(Response::builder()
                .status(StatusCode::NOT_FOUND)
                .body(notfound_html.into())
                .unwrap())
        }
    }
}

// 비동기 main 함수로 서버 구동
#[tokio::main]
async fn main() -> Result<()> {
    // 바인딩할 IP 주소와 포트
    let addr = "127.0.0.1:20000".parse().unwrap();

    // 클라이언트 연결이 생길 때마다 서비스 인스턴스를 생성하는 함수
    let new_service = make_service_fn(move |_| {
        async {
            Ok::<_, GenericError>(service_fn(move |req| {
                // 요청이 오면 response_examples 함수로 처리
                response_examples(req)
            }))
        }
    });

    // 서버를 해당 주소에서 실행
    let server = Server::bind(&addr).serve(new_service);
    println!("Listening on http://{}", addr); // 서버 실행 로그 출력
    server.await?; // 서버 실행을 대기
    Ok(())
}

/* 실행결과
127.0.0.1:20000/ 에 접속시  Hello World! 출력
그 외 에는 404 not found 출력
*/

댓글 없음:

댓글 쓰기