### Setup CORS Middleware with ntex Source: https://docs.rs/ntex-cors/latest/ntex_cors/index Demonstrates how to configure and apply CORS middleware in an ntex application. This example shows how to build a Cors middleware instance with specific allowed origins, methods, and headers, and then attach it to the application or specific resources. The middleware automatically handles OPTIONS preflight requests. ```rust use ntex_cors::Cors; use ntex::{http, web}; use ntex::web::{App, HttpRequest, HttpResponse}; async fn index(req: HttpRequest) -> &'static str { "Hello world" } #[ntex::main] async fn main() -> std::io::Result<()> { web::server(|| App::new() .wrap( Cors::new() // <- Construct CORS middleware builder .allowed_origin("https://www.rust-lang.org/") .allowed_methods(vec![http::Method::GET, http::Method::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(|| async { HttpResponse::MethodNotAllowed() })) )) .bind("127.0.0.1:8080")? .run() .await } ``` -------------------------------- ### CorsFactory - Middleware Implementation Source: https://docs.rs/ntex-cors/latest/ntex_cors/struct Documentation for the CorsFactory struct and its implementation of the ntex Middleware trait, detailing how to create a CORS middleware service. ```APIDOC ## CorsFactory - Middleware Implementation ### Description Provides Cross-origin resource sharing (CORS) support through a middleware factory. The `CorsFactory` struct holds settings for validating CORS requests and generating appropriate responses. ### Struct Definition ```rust pub struct CorsFactory { /* private fields */ } ``` ### Trait Implementations #### `impl Middleware for CorsFactory` This implementation allows `CorsFactory` to be used as middleware within the ntex framework. - **`type Service = CorsMiddleware`** The resulting middleware `Service` created by this factory. - **`fn create(&self, service: S) -> Self::Service`** Creates and returns a new middleware `Service` by wrapping the provided inner `service` with CORS capabilities. ``` -------------------------------- ### Rust Blanket Implementations for Generic Types Source: https://docs.rs/ntex-cors/latest/ntex_cors/struct Showcases blanket implementations of various traits for generic types, including `Any`, `Borrow`, `BorrowMut`, `From`, `Instrument`, `Into`, `Same`, `TryFrom`, `TryInto`, and `WithSubscriber`. These implementations provide common functionalities applicable to many types, including `CorsFactory`. ```rust impl Any for T where T: 'static + ?Sized, impl Borrow for T where T: ?Sized, impl BorrowMut for T where T: ?Sized, impl From for T impl Instrument for T impl Into for T where U: From, impl Same for T impl TryFrom for T where U: Into, impl TryInto for T where U: TryFrom, impl WithSubscriber for T ``` -------------------------------- ### Rust Middleware Implementation for CorsFactory Source: https://docs.rs/ntex-cors/latest/ntex_cors/struct Implements the `Middleware` trait for `CorsFactory`. This allows `CorsFactory` to be used as middleware in an ntex service pipeline. It defines the associated `Service` type and the `create` method for instantiating the middleware service. ```rust impl Middleware for CorsFactory where S: Service, Response = WebResponse>, { type Service = CorsMiddleware; fn create(&self, service: S) -> Self::Service } ``` -------------------------------- ### Rust CorsFactory Struct Definition Source: https://docs.rs/ntex-cors/latest/ntex_cors/struct Defines the `CorsFactory` struct, which serves as middleware for Cross-origin resource sharing (CORS) support in ntex web applications. It holds settings for validating CORS requests and generating responses. ```rust pub struct CorsFactory { /* private fields */ } ``` -------------------------------- ### Rust CorsError: impl Any for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Any trait for generic type T. This is part of Rust's trait system for runtime type identification, allowing dynamic checking of types. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Rust CorsError: impl TryInto for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the TryInto trait for generic types T and U, where T can be converted into U, potentially failing. This is the inverse of TryFrom. ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Rust CorsError: impl Debug for CorsError Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Debug trait for the CorsError enum in Rust. This allows CorsError instances to be formatted for debugging purposes using the standard debugging format. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Rust Auto Trait Implementations for CorsFactory Source: https://docs.rs/ntex-cors/latest/ntex_cors/struct Details the auto-trait implementations for `CorsFactory`, indicating its thread safety and memory safety characteristics. This includes implementations for `Freeze`, `RefUnwindSafe`, `Send`, `Sync`, `Unpin`, and `UnwindSafe`. ```rust impl Freeze for CorsFactory impl !RefUnwindSafe for CorsFactory impl !Send for CorsFactory impl !Sync for CorsFactory impl Unpin for CorsFactory where Err: Unpin, impl !UnwindSafe for CorsFactory ``` -------------------------------- ### Rust CorsError: impl Display for CorsError Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Display trait for the CorsError enum in Rust. This enables CorsError instances to be formatted as human-readable strings, typically for user-facing messages. ```rust fn fmt(&self, _derive_more_display_formatter: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Rust CorsError: impl WebResponseError for CorsError Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the WebResponseError trait for the CorsError enum. This provides default error rendering capabilities for web responses, defining how CORS errors are translated into HTTP status codes and response bodies. ```rust fn status_code(&self) -> StatusCode ``` ```rust fn error_response(&self, _: &HttpRequest) -> Response ``` -------------------------------- ### Rust CorsError: impl ToString for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the ToString trait for generic type T where T also implements Display. This allows converting any type that can be displayed into a String. ```rust fn to_string(&self) -> String ``` -------------------------------- ### Rust CorsError: impl Into for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Into trait for generic types T and U where U implements From. This provides a convenient way to convert T into U. ```rust fn into(self) -> U ``` -------------------------------- ### Rust CorsError: impl Same for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Same trait for generic type T. This trait likely indicates that the type is the same as its output type. ```rust type Output = T ``` -------------------------------- ### Rust CorsError: impl From for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the From trait for generic type T. This allows conversion from T to T, which is a trivial identity conversion. ```rust fn from(t: T) -> T ``` -------------------------------- ### Rust CorsError: impl WithSubscriber for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the WithSubscriber trait for generic type T. This allows attaching a subscriber to the type for distributed tracing or event handling. ```rust fn with_subscriber(self, subscriber: S) -> WithDispatch where S: Into ``` ```rust fn with_current_subscriber(self) -> WithDispatch ``` -------------------------------- ### Rust CorsError: impl Instrument for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Instrument trait for generic type T. This allows instrumenting the type with spans for tracing and diagnostic purposes. ```rust fn instrument(self, span: Span) -> Instrumented ``` ```rust fn in_current_span(self) -> Instrumented ``` -------------------------------- ### Rust CorsError: impl Borrow for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the Borrow trait for generic type T. This allows owned values of T to be borrowed immutably as references to T. ```rust fn borrow(&self) -> &T ``` -------------------------------- ### Rust CorsError: impl BorrowMut for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the BorrowMut trait for generic type T. This allows owned values of T to be borrowed mutably as mutable references to T. ```rust fn borrow_mut(&mut self) -> &mut T ``` -------------------------------- ### Rust CorsError Enum Definition Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Defines the CorsError enum, which represents various errors that can occur during the processing of Cross-Origin Resource Sharing (CORS) requests. It lists specific error conditions like missing or invalid origin, request method, and headers. ```rust pub enum CorsError { MissingOrigin, BadOrigin, MissingRequestMethod, BadRequestMethod, BadRequestHeaders, OriginNotAllowed, MethodNotAllowed, HeadersNotAllowed, } ``` -------------------------------- ### Rust CorsError: impl TryFrom for T Source: https://docs.rs/ntex-cors/latest/ntex_cors/enum Implementation of the TryFrom trait for generic types T and U, where U can be converted into T, potentially failing. The error type for this conversion is Infallible, meaning it cannot fail. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.