1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-08-30 11:08:08 +02:00
Files
actix-extras/actix-cors/README.md
dependabot[bot] 1fee1a47b4 build(deps): bump deadpool-redis from 0.20.0 to 0.22.0 (#543)
* build(deps): bump deadpool-redis from 0.20.0 to 0.21.1

Bumps [deadpool-redis](https://github.com/bikeshedder/deadpool) from 0.20.0 to 0.21.1.
- [Changelog](https://github.com/deadpool-rs/deadpool/blob/master/CHANGELOG.md)
- [Commits](https://github.com/bikeshedder/deadpool/compare/deadpool-redis-v0.20.0...deadpool-redis-v0.21.1)

---
updated-dependencies:
- dependency-name: deadpool-redis
  dependency-version: 0.21.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Update redis crate

* Update MSRV

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2025-08-22 07:52:57 +00:00

2.5 KiB

actix-cors

crates.io Documentation Version MIT or Apache 2.0 licensed
Dependency Status Download Chat on Discord

Cross-Origin Resource Sharing (CORS) controls for Actix Web.

This middleware can be applied to both applications and resources. Once built, a [Cors] builder can be used as an argument for Actix Web's App::wrap(), Scope::wrap(), or Resource::wrap() methods.

This CORS middleware automatically handles OPTIONS preflight requests.

Crate Features

  • draft-private-network-access: ⚠️ Unstable. Adds opt-in support for the Private Network Access spec extensions. This feature is unstable since it will follow breaking changes in the draft spec until it is finalized.

Example

use actix_cors::Cors;
use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};

#[get("/index.html")]
async fn index(req: HttpRequest) -> &'static str {
    "<p>Hello World!</p>"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let cors = Cors::default()
            .allowed_origin("https://www.rust-lang.org")
            .allowed_origin_fn(|origin, _req_head| {
                origin.as_bytes().ends_with(b".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);

        App::new()
            .wrap(cors)
            .service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await;

    Ok(())
}

Documentation & Resources