#web #framework

requiem-cors

Cross-origin resource sharing (CORS) for Actix applications

1 unstable release

0.2.0-r1 Jan 18, 2020

#422 in #framework

MIT/Apache

485KB
11K SLoC

Cors Middleware for actix web framework Build Status codecov crates.io Join the chat at https://gitter.im/actix/actix

Documentation & community resources


lib.rs:

Cross-origin resource sharing (CORS) for Actix applications

CORS middleware could be used with application and with resource. Cors middleware could be used as parameter for App::wrap(), Resource::wrap() or Scope::wrap() methods.

Example

use requiem_cors::Cors;
use requiem_web::{http, web, App, HttpRequest, HttpResponse, HttpServer};

async fn index(req: HttpRequest) -> &'static str {
    "Hello world"
}

fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new()
        .wrap(
            Cors::new() // <- Construct CORS middleware builder
              .allowed_origin("https://www.rust-lang.org/")
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .allowed_header(http::header::CONTENT_TYPE)
              .max_age(3600)
              .finish())
        .service(
            web::resource("/index.html")
              .route(web::get().to(index))
              .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
        ))
        .bind("127.0.0.1:8080")?;

    Ok(())
}

In this example custom CORS middleware get registered for "/index.html" endpoint.

Cors middleware automatically handle OPTIONS preflight request.

Dependencies

~21MB
~498K SLoC