Skip to content

Commit

Permalink
add cors max age
Browse files Browse the repository at this point in the history
  • Loading branch information
morphy2k committed Aug 31, 2023
1 parent bf81475 commit 1b8d4f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const fn default_health_latency_treshold() -> u64 {
100
}

const fn default_cors_max_age() -> u64 {
1800
}

#[derive(Debug, Clone, Deserialize)]
pub struct AppConfig {
// HTTP server
Expand Down Expand Up @@ -81,8 +85,11 @@ pub struct AppConfig {
#[serde(default = "default_hibp_check")]
pub hibp_check: bool,

// CORS
#[serde(default, deserialize_with = "deserialize_vec_from_string")]
pub cors_allowed_origins: Vec<HeaderValue>,
#[serde(default = "default_cors_max_age")]
pub cors_max_age: u64,
}

#[derive(Debug, Clone)]
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ async fn main() -> Result<()> {
);

let route_config = RouteConfig {
cors: CorsLayer::new().allow_origin(app_config.cors_allowed_origins),
cors: CorsLayer::new()
.allow_origin(app_config.cors_allowed_origins)
.max_age(Duration::from_secs(app_config.cors_max_age)),
};

let svc_routes: Router<()> = services::routes(state, route_config);
Expand Down

0 comments on commit 1b8d4f1

Please sign in to comment.