graphql_starter/axum/cors.rs
1use anyhow::Result;
2use auto_impl::auto_impl;
3use tower_http::cors::CorsLayer;
4
5/// CORS service
6#[auto_impl(Box, Arc)]
7#[trait_variant::make(Send)]
8pub trait CorsService: Send + Sync + Sized + 'static {
9 /// Retrieves the allowed origins for this API
10 fn allowed_origins(&self) -> &[String];
11 /// Builds the [CorsLayer]
12 fn build_cors_layer(&self) -> Result<CorsLayer>;
13}
14
15/// Trait implemented by the application State to provide cors-related services.
16pub trait CorsState {
17 /// The concrete CORS Service type
18 type Cors: CorsService;
19
20 /// Retrieves the CORS service
21 fn cors(&self) -> &Self::Cors;
22}