Skip to main content

Crate salvo_cors

Crate salvo_cors 

Source
Expand description

CORS (Cross-Origin Resource Sharing) protection for Salvo web framework.

§Important

The CORS handler must be added to Service via .hoop(), not to Router. This is because browsers send OPTIONS preflight requests that don’t match any route, and only Service-level middleware can intercept them.

use salvo_core::http::Method;
use salvo_core::prelude::*;
use salvo_cors::Cors;

#[handler]
async fn hello() -> &'static str {
    "Hello World"
}

fn main() {
    let cors = Cors::new()
        .allow_origin("http://localhost:3000")
        .allow_methods([Method::GET, Method::POST, Method::DELETE])
        .allow_headers("authorization")
        .into_handler();

    let router = Router::new().get(hello);
    // CORS must be on Service, NOT on Router
    let _service = Service::new(router).hoop(cors);
}

Structs§

AllowCredentials
Holds configuration for how to set the Access-Control-Allow-Credentials header.
AllowHeaders
Holds configuration for how to set the Access-Control-Allow-Headers header.
AllowMethods
Holds configuration for how to set the Access-Control-Allow-Methods header.
AllowOrigin
Holds configuration for how to set the Access-Control-Allow-Origin header.
AllowPrivateNetwork
Holds configuration for how to set the Access-Control-Allow-Private-Network header.
Any
Represents a wildcard value (*) used with some CORS headers such as Cors::allow_methods.
Cors
Cors middleware which adds headers for CORS.
CorsHandler
CorsHandler
ExposeHeaders
Holds configuration for how to set the Access-Control-Expose-Headers header.
MaxAge
Holds configuration for how to set the Access-Control-Max-Age header.
Vary
Holds configuration for how to set the Vary header.

Enums§

CallNext
Enum to control when to call next handler.

Functions§

preflight_request_headers
Iterator over the three request headers that may be involved in a CORS preflight request.