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
#![recursion_limit = "256"]
mod api;
mod config;
mod constants;
mod context;
mod model;
mod service;
use std::{sync::Arc, time::Duration};
use crate::{
api::router,
context::{AppState, AuthorizationService},
};
use anyhow::{Context, Result};
use axum::http::{
Method,
header::{AUTHORIZATION, CONTENT_TYPE},
};
use config::Config;
use constants::ORIGINS;
use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccessRepository};
use frecency::{
domain::services::{EventIngestorImpl, PullAggregatorImpl},
inbound::polling_aggregator::FrecencyAggregatorWorkerHandle,
outbound::{
postgres::{FrecencyPgProcessor, FrecencyPgStorage},
time::DefaultTime,
},
};
use last_online_tracker::{
domain::services::LastOnlineService,
inbound::LastOnlineWorker,
outbound::{redis::RedisLastOnlineRepo, time::DefaultTime as LastOnlineDefaultTime},
};
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
use macro_authorization::{
InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState,
PgUserApiKeyAuthorizationRepo, PgUserApiKeyAuthorizer,
};
use macro_entrypoint::MacroEntrypoint;
use macro_env::Environment;
use macro_tower_layers::MacroRequestIdAndTracingLayer;
use service::dynamodb::create_dynamo_db_connection_manager;
use service::redis::poll_messages;
use sqlx::postgres::PgPoolOptions;
use stream::outbound::redis_pg::{RedisPostgresStreamManager, RedisPostgresStreamRepo};
use tower_http::cors::CorsLayer;
#[tokio::main]
#[tracing::instrument(ret, err)]
async fn main() -> Result<()> {
MacroEntrypoint::default().init();
let aws_config = macro_aws_config::get_macro_aws_config().await;
let secretsmanager_client = secretsmanager_client::SecretsManager::new(
aws_sdk_secretsmanager::Client::new(&aws_config),
);
// Parse our configuration from the environment.
let config = Config::from_env()?
.resolve_remote_secrets(Environment::new_or_prod(), &secretsmanager_client)
.await?;
let jwt_args =
JwtValidationArgs::new_with_secret_manager(config.environment, &secretsmanager_client)
.await?;
// allow requests from any origin
let cors = CorsLayer::new()
.allow_credentials(true)
.allow_headers(vec![AUTHORIZATION, CONTENT_TYPE])
.allow_methods(vec![
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
])
.allow_origin(ORIGINS);
let dynamodb_client = aws_sdk_dynamodb::Client::new(&aws_config);
let redis_client = Arc::new(
redis::Client::open(config.redis_host.as_ref())
.inspect(|client| {
client
.get_connection()
.map(|_| tracing::trace!("initialized redis connection"))
.inspect_err(|e| {
tracing::error!(error=?e, "failed to connect to redis");
})
.ok();
})
.context("failed to connect to redis")?,
);
let connection_manager = create_dynamo_db_connection_manager(dynamodb_client.clone()).await?;
let redis_connection = redis_client
.get_multiplexed_async_connection()
.await
.context("failed to create shared redis connection")?;
let last_online_worker = Arc::new(LastOnlineWorker::new(LastOnlineService::new(
LastOnlineDefaultTime,
RedisLastOnlineRepo::new(redis_connection.clone()),
)));
let pgpool = PgPoolOptions::new()
.min_connections(3)
.max_connections(20)
.connect(config.macro_db_url.as_ref())
.await?;
let stream_service = RedisPostgresStreamRepo::new((*redis_client).clone(), pgpool.clone());
let stream_manager = RedisPostgresStreamManager::new(stream_service.obj());
let fanout_gateway_id: Arc<str> = Arc::from(uuid::Uuid::new_v4().to_string());
let context = context::ApiContext {
connection_manager,
entity_access: Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
pgpool.clone(),
))),
fanout_gateway_id: Arc::clone(&fanout_gateway_id),
redis_client: Arc::clone(&redis_client),
redis_connection,
frecency_ingestor_service: EventIngestorImpl::new(FrecencyPgStorage::new(pgpool.clone())),
stream_manager,
last_online_worker,
};
tokio::spawn(poll_messages(context.clone()));
tokio::spawn(service::fanout::poll_outbound(context.clone()));
tokio::spawn(service::fanout::heartbeat_loop(
context.redis_connection.clone(),
fanout_gateway_id,
));
let config = Arc::new(config);
let authorization_state = MacroAuthorizationState::new(Arc::new(AuthorizationService::new(
MacroAuthJwtValidator::new(jwt_args.clone()),
InternalAuthConfig {
api_key: config.internal_api_key.to_string(),
default_user_id: None,
},
macro_authorization::NoBotAuthorizer,
PgUserApiKeyAuthorizer::new(PgUserApiKeyAuthorizationRepo::new(pgpool.clone())),
)));
let correlation = agent_correlation::inbound::CorrelationState::new(pgpool.clone(), jwt_args);
let app = router(AppState {
context,
config: Arc::clone(&config),
authorization_state,
frecency_worker: Arc::new(FrecencyAggregatorWorkerHandle::new_worker(
PullAggregatorImpl::new(FrecencyPgProcessor::new(pgpool), DefaultTime),
Duration::from_secs(60),
)),
})
.layer(MacroRequestIdAndTracingLayer::new(Duration::from_millis(200)).into_inner())
.layer(cors)
.layer(axum::middleware::from_fn_with_state(
correlation,
agent_correlation::inbound::propagate,
));
tracing::info!(
"connection gateway is up and running with environment {:?} on port {}",
config.environment,
config.port
);
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.port))
.await
.context("failed to bind to port")?;
axum::serve(listener, app.into_make_service())
.await
.context("failed to serve")?;
Ok(())
}