Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
#[cfg(test)]
mod test;
use std::{borrow::Cow, time::Duration};
use axum::http::{
HeaderName, HeaderValue, Method,
header::{AUTHORIZATION, CONTENT_TYPE},
};
use tower_http::cors::{AllowOrigin, CorsLayer};
static ALLOWED_ORIGINS: [&str; 11] = [
"http://localhost:5173",
"https://dashboarddev.macro.com",
"https://dashboard.macro.com",
"http://host.local:3000",
"https://dev.macro.com",
"https://staging.macro.com",
"https://www.macro.com",
"https://macro.com",
"http://tauri.localhost",
"tauri://localhost",
"https://apollo-testing.macro.com",
];
static EXTRA_HEADERS: [&str; 4] = [
"x-permissions-token",
"traceparent",
"tracestate",
"x-email-link-id",
];
/// Chrome caps preflight caching at two hours. Without a max age it caches for
/// five seconds, so any API call after a short idle pays an extra round trip.
const PREFLIGHT_MAX_AGE: Duration = Duration::from_secs(2 * 60 * 60);
fn get_allowed_origins() -> Vec<Cow<'static, str>> {
#[allow(
clippy::disallowed_methods,
reason = "Used to override ALLOWED_ORIGINS when running locally"
)]
match std::env::var("ALLOWED_ORIGINS") {
Ok(origins) => origins
.split(',')
.map(|origin| origin.trim().to_string())
.map(Cow::Owned)
.collect(),
Err(_) => ALLOWED_ORIGINS.into_iter().map(Cow::Borrowed).collect(),
}
}
/// Generates the Cors layer which can be used in the `ServiceBuilder::layer` method.
pub fn cors_layer() -> CorsLayer {
cors_layer_with_headers(vec![])
}
fn is_allowed_origin(origin: &str) -> bool {
let allowed_origins = get_allowed_origins();
// Check static origins first
if allowed_origins.contains(&Cow::Borrowed(origin)) {
return true;
}
// Allow all preview endpoints `https://*preview.macro.com`
if origin.ends_with("preview.macro.com") && origin.starts_with("https://") {
return true;
}
// `localhost` and `*.localhost` (loopback-reserved per RFC 6761; used
// locally to give each seeded persona its own cookie jar per hostname).
if let Some(rest) = origin.strip_prefix("http://")
&& let Some((host, port_str)) = rest.rsplit_once(':')
&& (host == "localhost" || host.ends_with(".localhost"))
&& let Ok(port) = port_str.parse::<u16>()
{
return (3000..=3999).contains(&port) || (20000..=60000).contains(&port);
}
false
}
/// Generates the Cors layer with additional headers which can be used in the `ServiceBuilder::layer` method.
pub fn cors_layer_with_headers(additional_headers: Vec<HeaderName>) -> CorsLayer {
let mut headers = vec![AUTHORIZATION, CONTENT_TYPE];
headers.extend(additional_headers);
headers.extend(
EXTRA_HEADERS
.iter()
.map(|header| HeaderName::from_static(header)),
);
CorsLayer::new()
.max_age(PREFLIGHT_MAX_AGE)
.allow_credentials(true)
.allow_headers(headers)
.allow_methods(vec![
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
])
.allow_origin(AllowOrigin::predicate(
|origin: &HeaderValue, _request_parts| {
origin.to_str().map(is_allowed_origin).unwrap_or(false)
},
))
}