Skip to content

Commit

Permalink
Merge pull request #111 from spiralover/refactor/regex
Browse files Browse the repository at this point in the history
feat(http): expose allowed methods
  • Loading branch information
Ahmard authored Jan 25, 2025
2 parents 025ed77 + 735d6e3 commit bfc9065
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "medullah-web"
version = "0.28.0"
version = "0.29.0"
edition = "2021"
license = "MIT"
description = "Micro-Framework Based on Ntex"
Expand All @@ -26,7 +26,7 @@ multipart = ["medullah-multipart"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.94"
anyhow = "1.0.95"

deadpool = { version = "0.12.1", features = ["managed"] }

Expand All @@ -40,7 +40,7 @@ redis = { version = "0.27.6", default-features = false, optional = true, feature
] }
deadpool-redis = { version = "0.18.0", features = ["rt_tokio_1"], optional = true }

uuid = { version = "1.12.0", features = ["v4", "serde"] }
uuid = { version = "1.12.1", features = ["v4", "serde"] }
log = { version = "0.4.25" }
serde = { version = "1.0.217", features = ["derive"] }
tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros", "time"] }
Expand Down
3 changes: 3 additions & 0 deletions src/app_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ use diesel::PgConnection;
use log::info;
#[cfg(feature = "templating")]
use tera::Tera;
use crate::http::Method;

pub struct MedullahSetup {
pub env_prefix: String,
pub private_key: String,
pub public_key: String,
pub auth_iss_public_key: String,
pub allowed_origins: Vec<String>,
pub allowed_methods: Vec<Method>,
}

pub async fn make_app_state(setup: MedullahSetup) -> MedullahState {
Expand Down Expand Up @@ -124,6 +126,7 @@ async fn create_app_state(setup: MedullahSetup) -> MedullahState {
.unwrap(),

allowed_origins: setup.allowed_origins,
allowed_methods: setup.allowed_methods,

#[cfg(feature = "mailer")]
mailer_config: make_mailer_config(&env_prefix),
Expand Down
6 changes: 5 additions & 1 deletion src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::services::cache_service::CacheService;
use redis::Client as RedisClient;
#[cfg(feature = "templating")]
use tera::{Context, Tera};
use crate::http::Method;

#[derive(Clone)]
pub struct MedullahState {
Expand Down Expand Up @@ -59,9 +60,12 @@ pub struct MedullahState {
#[cfg(feature = "jwt")]
pub auth_iss_public_key: String,

/// list of comma-separated allowed origins
/// list of allowed origins
pub allowed_origins: Vec<String>,

/// list of allowed methods
pub allowed_methods: Vec<Method>,

#[cfg(feature = "mailer")]
pub mailer_config: AppMailerConfig,

Expand Down
19 changes: 16 additions & 3 deletions src/http/kernel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::enums::ResponseCode;
use crate::helpers::responder::Responder;
use crate::http::middlewares::Middleware;
use crate::http::Method;
use log::info;
use ntex::http::header;
use ntex::web::middleware::Logger;
Expand Down Expand Up @@ -69,8 +70,8 @@ pub fn setup_logger() -> Logger {
.exclude("/system/docker-health-check")
}

pub fn setup_cors(origins: Vec<String>) -> Cors {
let mut cors = Cors::new();
pub fn setup_cors(origins: Vec<String>, methods: Vec<Method>) -> Cors {
let mut cors = Cors::new().send_wildcard();

for origin in origins {
info!("registering cors origin: {origin}...");
Expand All @@ -84,7 +85,19 @@ pub fn setup_cors(origins: Vec<String>) -> Cors {
cors = cors.allowed_origin(origin.as_str());
}

cors.allowed_methods(vec!["GET", "POST", "PUT", "PATCH", "DELETE"])
let allowed_methods = match methods.is_empty() {
false => methods,
true => vec![
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
],
};

cors.allowed_methods(allowed_methods)
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.max_age(3600)
Expand Down
3 changes: 3 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ pub mod middlewares;
pub mod response;
pub mod server;

pub use ntex_cors::Cors;
pub use ntex::http::Method;

pub type HttpHandler = fn(cfg: &mut ServiceConfig);
15 changes: 13 additions & 2 deletions src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::app_setup::{
};
use crate::env_logger::init_env_logger;
use crate::http::kernel::{ntex_default_service, register_routes, setup_cors, setup_logger, Route};
use crate::http::Method;
use crate::prelude::{AppResult, MedullahState};

pub struct ServerConfig<TB>
Expand All @@ -26,9 +27,12 @@ where
/// whether the app bootstrap has started
pub has_started_bootstrap: bool,

/// list of comma-separated allowed origins
/// list of allowed CORS origins
pub allowed_origins: Vec<String>,

/// list of allowed CORS origins
pub allowed_methods: Vec<Method>,

pub boot_thread: TB,
}

Expand Down Expand Up @@ -63,6 +67,7 @@ where
env_prefix: config.env_prefix.clone(),
auth_iss_public_key: config.auth_iss_public_key,
allowed_origins: config.allowed_origins,
allowed_methods: config.allowed_methods,
})
.await;

Expand All @@ -83,7 +88,13 @@ where
.state(app_state.clone())
.configure(|cfg| register_routes(cfg, routes))
.wrap(setup_logger())
.wrap(setup_cors(app_state.allowed_origins.clone()).finish())
.wrap(
setup_cors(
app_state.allowed_origins.clone(),
app_state.allowed_methods.clone(),
)
.finish(),
)
.default_service(ntex_default_service());

if cfg!(feature = "static") {
Expand Down

0 comments on commit bfc9065

Please sign in to comment.