Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8aaccb1
feat(martin-server): add initial support for cors configuration
NINNiT Apr 30, 2025
36a4b6d
feat: refactor
NINNiT May 1, 2025
48d32a0
test: add some cors tests to martin/src/srv/config.rs
NINNiT May 1, 2025
eb41297
docs: add cors to config-file docs
NINNiT May 1, 2025
6b5d309
docs: revert formatting
NINNiT May 1, 2025
4b35b5f
chore: cleanup
NINNiT May 1, 2025
ea56fae
docs: fix origin property
NINNiT May 1, 2025
1c6089c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
c51805c
chore: remove unused import
NINNiT May 1, 2025
b4b82dc
feat(martin-server): change CORS configuration spec
soelder-trafficon May 2, 2025
8881d86
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 2, 2025
99780a5
chore: ignore aider files in gitignore and dockerignore
NINNiT May 2, 2025
0167559
Merge branch 'main' into feat/cors
CommanderStorm May 26, 2025
782bc06
feat: add simple cors property validation
NINNiT May 17, 2025
3a8d478
feat: add error handling for cors
NINNiT May 27, 2025
480e35a
simplify testcase
CommanderStorm May 27, 2025
9d90822
simplify the testcases a bit
CommanderStorm May 27, 2025
00fccef
chore: sort aider string in gitignore and dockerignore
NINNiT May 27, 2025
44e7d21
Merge branch 'main' into feat/cors
NINNiT May 27, 2025
0e4ecfd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2025
9f041a2
docs: update CORS docs in server-config.md
NINNiT May 27, 2025
52af719
fix a few of the issues I introduced
CommanderStorm May 27, 2025
3278e9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2025
64d1133
feat(cors): added separate validation method, call it early
NINNiT May 27, 2025
5ff41e7
docs(cors): cleanup nits
NINNiT May 27, 2025
e0ec65c
chore(cors): minor cleanups
NINNiT May 27, 2025
56e4629
chore(cors): remove assert!(matches!()) in favor of assert_eq!
NINNiT May 27, 2025
f726b1d
fix clippy issues I caused
CommanderStorm May 27, 2025
e0d9eef
fix formatting
CommanderStorm May 27, 2025
2d47add
Remove the second validation
CommanderStorm May 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ demo/
.DS_Store
target/
**/*.rs.bk
.aider*
.idea/
.vscode/
test_log*
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.DS_Store
target/
**/*.rs.bk
.aider*
.idea/
.vscode/
test_log*
Expand Down
91 changes: 25 additions & 66 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions docs/src/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ preferred_encoding: gzip
# Enable or disable Martin web UI. At the moment, only allows `enable-for-all` which enables the web UI for all connections. This may be undesirable in a production environment. [default: disable]
web_ui: disable

# CORS Configuration
#
# Defaults to `cors: true`, which allows all origins.
Comment thread
NINNiT marked this conversation as resolved.
# Sending/Acting on CORS headers can be completely disabled via `cors: false`
cors:
# Sets the `Access-Control-Allow-Origin` header [default: *]
Comment thread
CommanderStorm marked this conversation as resolved.
# '*' will use the requests `ORIGIN` header
origin:
- https://example.org
# Sets `Access-Control-Max-Age` Header. [default: null]
# null means not setting the header for preflight requests
max_age: 3600

# Database configuration. This can also be a list of PG configs.
postgres:
# Database connection string.
Expand Down
64 changes: 64 additions & 0 deletions martin/src/srv/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

use super::cors::CorsConfig;
use crate::args::PreferredEncoding;

pub const KEEP_ALIVE_DEFAULT: u64 = 75;
Expand All @@ -15,13 +16,15 @@ pub struct SrvConfig {
pub preferred_encoding: Option<PreferredEncoding>,
#[cfg(feature = "webui")]
pub web_ui: Option<crate::args::WebUiMode>,
pub cors: Option<CorsConfig>,
}

#[cfg(test)]
mod tests {
use indoc::indoc;

use super::*;
use crate::srv::cors::CorsProperties;
use crate::tests::some;

#[test]
Expand Down Expand Up @@ -73,4 +76,65 @@ mod tests {
}
);
}

#[test]
fn parse_config_cors() {
assert_eq!(
serde_yaml::from_str::<SrvConfig>(indoc! {"
keep_alive: 75
listen_addresses: '0.0.0.0:3000'
worker_processes: 8
cors: false
"})
.unwrap(),
SrvConfig {
keep_alive: Some(75),
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
cors: Some(CorsConfig::SimpleFlag(false)),
..Default::default()
}
);
assert_eq!(
serde_yaml::from_str::<SrvConfig>(indoc! {"
keep_alive: 75
listen_addresses: '0.0.0.0:3000'
worker_processes: 8
cors: true
"})
.unwrap(),
SrvConfig {
keep_alive: Some(75),
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
cors: Some(CorsConfig::SimpleFlag(true)),
..Default::default()
}
);
assert_eq!(
serde_yaml::from_str::<SrvConfig>(indoc! {"
keep_alive: 75
listen_addresses: '0.0.0.0:3000'
worker_processes: 8
cors:
origin:
- https://martin.maplibre.org
- https://example.org
"})
.unwrap(),
SrvConfig {
keep_alive: Some(75),
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
cors: Some(CorsConfig::Properties(CorsProperties {
origin: vec![
"https://martin.maplibre.org".to_string(),
"https://example.org".to_string()
],
max_age: None,
})),
..Default::default()
}
);
}
}
Loading