3.3.1 애플리케이션 레벨에서 CORS 헤더를 반환하기

    Warp에서 cors 필터를 사용하고 조정할 수 있다. 예제에서는 모든 출처를 허용하지만, 실제 운영 환경에서는 이렇게 해서는 안 된다. allow_origin 필터(http://mng.bz/lRZy)로 허용하는 출처를 지정할 수 있다.

    코드 3-13 적확한 CORS 헤더를 반환할 수 있도록 애플리케이션 준비하기

    use warp::{Filter, reject::Reject, Rejection, Reply, http::StatusCode, http::Method};
    ...
    #[tokio::main]
    async fn main() {
        let cors = warp::cors()
            .allow_any_origin()
            .allow_header("content-type")
            .allow_methods(&[Method::PUT, Method::DELETE, Method::GET, Method::POST]);
    
        let get_items = warp::get()
            .and(warp::path("questions"))
            .and(warp::path::end())
            .and_then(get_questions);
    
        let routes = get_items.with(cors).recover(return_error);
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.