Skip to content

Commit

Permalink
fix: cors, not building in 1.80.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zrll12 committed Jul 26, 2024
1 parent 80d1a77 commit 198f6b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct ConnectionConfig {
pub ssl_key: String,
#[serde_inline_default(2)]
pub max_body_size: usize,
#[serde_inline_default(vec!["http://localhost:3000".into(), "https://itoyou.cc".into()])]
pub origins: Vec<String>,
}

#[serde_inline_default]
Expand Down Expand Up @@ -76,6 +78,7 @@ fn generate_connection_setting() -> ConnectionConfig {
ssl_cert: "./cert.crt".to_string(),
ssl_key: "./private.key".to_string(),
max_body_size: 2,
origins: vec!["http://localhost:3000".into(), "https://itoyou.cc".into()],
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::iter::once;

use axum::{http, Router};
use axum::extract::DefaultBodyLimit;
use axum::http::HeaderValue;
use axum::routing::{get, post};
use axum_server::tls_rustls::RustlsConfig;
use chrono::Local;
Expand Down Expand Up @@ -87,6 +88,7 @@ async fn main() {
.init();

Migrator::up(&*DATABASE, None).await.unwrap();
let origins = CONFIG.connection.origins.clone().iter().map(|x| x.parse().unwrap()).collect::<Vec<HeaderValue>>();

let app = Router::new()
.route("/user", post(register_user).get(login_user))
Expand All @@ -106,7 +108,7 @@ async fn main() {
.layer(TraceLayer::new(
StatusInRangeAsFailures::new(400..=599).into_make_classifier()
))
.layer(CorsLayer::permissive())
.layer(CorsLayer::very_permissive().allow_origin(origins))
.layer(CatchPanicLayer::new())
.layer(DefaultBodyLimit::max(
CONFIG.connection.max_body_size * 1024 * 1024,
Expand Down

0 comments on commit 198f6b6

Please sign in to comment.