CORS
CORS stands for Cross-Origin Resource Sharing, and it is a mechanism used by web browsers to determine if a request to a resource on a different domain should be allowed or not. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.
Enabling it in Graphul​
Graphul has a built-in CORS middleware that can be enabled.
We use for this case the CorsLayer
of Tower and then we can add it as a middleware in our instance of Graphul.
Besides we can define:
It's not too recommended use the following methods but maybe to get a better development experience is easier to configure using it:
Example​
- Enabling CORS for anything
- Custom CORS
Here is a CodeSanbox Example
use graphul::middleware::tower::cors;
use graphul::{http::Methods, Graphul};
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.get("/", || async { "Hello World 👋!" });
app.middleware(cors::CorsLayer::permissive());
// ...
app.run("127.0.0.1:8000").await;
}
Here is a CodeSanbox Example
use graphul::middleware::tower::cors;
use graphul::{
http::{utils::Method, Methods},
Graphul,
};
#[tokio::main]
async fn main() {
let mut app = Graphul::new();
app.get("/", || async { "Hello World 👋!" });
app.middleware(
cors::CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([Method::GET, Method::POST])
// allow requests from any origin
.allow_origin(cors::Any),
);
// ...
app.run("127.0.0.1:8000").await;
}