Crates.io | actix-cors |
lib.rs | actix-cors |
version | |
source | src |
created_at | 2019-06-15 03:49:20.451043+00 |
updated_at | 2025-03-11 03:11:48.529622+00 |
description | Cross-Origin Resource Sharing (CORS) controls for Actix Web |
homepage | https://actix.rs |
repository | https://github.com/actix/actix-extras |
max_upload_size | |
id | 141256 |
Cargo.toml error: | TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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.
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.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(())
}