Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(email_service): migrate email service to gateway (#6202)

Will Hutchinson58a3527d68df
diff --git a/apps/web/scripts/services.ts b/apps/web/scripts/services.ts
index beba7a5411..334322cfa7 100644
--- a/apps/web/scripts/services.ts
+++ b/apps/web/scripts/services.ts
@@ -90,8 +90,8 @@ export const services: Service[] = [
},
{
name: "email-service",
- dev: "https://email-service-dev.macro.com/api-doc/openapi.json",
- prod: "https://email-service.macro.com/api-doc/openapi.json",
+ dev: "https://dev-gateway.macro.com/email/api-doc/openapi.json",
+ prod: "https://gateway.macro.com/email/api-doc/openapi.json",
local: "http://localhost:8087/api-doc/openapi.json",
output: "../src/lib/service-clients/service-email/",
orvalKey: "emailService",
diff --git a/apps/web/src/lib/core/constant/servers.ts b/apps/web/src/lib/core/constant/servers.ts
index eed009dec2..5d632441d2 100644
--- a/apps/web/src/lib/core/constant/servers.ts
+++ b/apps/web/src/lib/core/constant/servers.ts
@@ -40,7 +40,7 @@ const serverHostRemote = {
'static-file': `https://static-file-service${devServerSuffix}.macro.com`,
'unfurl-service': `${gatewayHost}/unfurl`,
contacts: `${gatewayHost}/contacts`,
- 'email-service': `https://email-service${devServerSuffix}.macro.com`,
+ 'email-service': `${gatewayHost}/email`,
'image-proxy-service': `${gatewayHost}/image-proxy`,
'scheduled-action': `${gatewayHost}/scheduled-action`,
'agent-harness': `${gatewayHost}/agent-harness`,
diff --git a/crates/macro_service_urls/src/lib.rs b/crates/macro_service_urls/src/lib.rs
index 80da09a0f0..98b8a77ba1 100644
--- a/crates/macro_service_urls/src/lib.rs
+++ b/crates/macro_service_urls/src/lib.rs
@@ -576,8 +576,8 @@ service_url! {
/// Email service API URL.
pub EmailServiceUrl {
local: "http://localhost:8087",
- dev: "https://email-service-dev.macro.com",
- prod: "https://email-service.macro.com",
+ dev: "https://dev-gateway.macro.com/email",
+ prod: "https://gateway.macro.com/email",
},
/// Image proxy service API URL.
pub ImageProxyServiceUrl {
diff --git a/crates/macro_service_urls/src/test.rs b/crates/macro_service_urls/src/test.rs
index aaec3af8f3..da78b835e3 100644
--- a/crates/macro_service_urls/src/test.rs
+++ b/crates/macro_service_urls/src/test.rs
@@ -176,6 +176,18 @@ fn email_service_url_parses() {
assert_parses_for_all_environments(EmailServiceUrl::default_for_environment);
}
+#[test]
+fn email_service_url_has_no_trailing_slash() {
+ for environment in ENVS {
+ let url = EmailServiceUrl::default_for_environment(environment);
+ assert!(
+ !url.as_ref().ends_with('/'),
+ "clients concatenate paths, so {} must not end with /",
+ url.as_ref()
+ );
+ }
+}
+
#[test]
fn image_proxy_service_url_parses() {
assert_parses_for_all_environments(ImageProxyServiceUrl::default_for_environment);
@@ -302,8 +314,8 @@ crate::service_url! {
#[derive(Debug, Clone)]
pub TestEmailServiceUrl {
local: "http://localhost:8087",
- dev: "https://email-service-dev.macro.com",
- prod: "https://email-service.macro.com",
+ dev: "https://dev-gateway.macro.com/email",
+ prod: "https://gateway.macro.com/email",
},
}
}
@@ -341,7 +353,7 @@ fn grouped_defaults_do_not_check_overrides() {
);
assert_eq!(
service_urls.test_email_service_url.as_ref(),
- "https://email-service.macro.com",
+ "https://gateway.macro.com/email",
);
}
@@ -473,7 +485,7 @@ fn exported_service_urls_match_dev_values() {
);
assert_eq!(
service_urls.email_service_url.as_ref(),
- "https://email-service-dev.macro.com",
+ "https://dev-gateway.macro.com/email",
);
assert_eq!(
service_urls.image_proxy_service_url.as_ref(),
@@ -544,7 +556,7 @@ fn exported_service_urls_match_prod_values() {
);
assert_eq!(
service_urls.email_service_url.as_ref(),
- "https://email-service.macro.com",
+ "https://gateway.macro.com/email",
);
assert_eq!(
service_urls.image_proxy_service_url.as_ref(),
diff --git a/infra/packages/shared/src/gateway_priorities.ts b/infra/packages/shared/src/gateway_priorities.ts
index 633f176601..b768bbaac7 100644
--- a/infra/packages/shared/src/gateway_priorities.ts
+++ b/infra/packages/shared/src/gateway_priorities.ts
@@ -14,6 +14,7 @@ export enum GatewayService {
AGENT_SCHEDULE_SERVICE = 'AGENT_SCHEDULE_SERVICE',
CONNECTION_GATEWAY = 'CONNECTION_GATEWAY',
AUTHENTICATION_SERVICE = 'AUTHENTICATION_SERVICE',
+ EMAIL_SERVICE = 'EMAIL_SERVICE',
}
/**
@@ -36,6 +37,7 @@ export const GATEWAY_PRIORITIES: GatewayPriorityMap = {
[GatewayService.AGENT_SCHEDULE_SERVICE]: 80,
[GatewayService.CONNECTION_GATEWAY]: 90,
[GatewayService.AUTHENTICATION_SERVICE]: 100,
+ [GatewayService.EMAIL_SERVICE]: 110,
[GatewayService.CONVERT_SERVICE]: 3000,
};
diff --git a/infra/packages/shared/src/service_urls.ts b/infra/packages/shared/src/service_urls.ts
index b0b0e15ba1..f7476cc685 100644
--- a/infra/packages/shared/src/service_urls.ts
+++ b/infra/packages/shared/src/service_urls.ts
@@ -30,7 +30,7 @@ type ServiceUrlMap = {
const DEV_SERVICE_URLS: ServiceUrlMap = {
[ServiceUrl.SYNC_SERVICE_URL]:
'https://sync-service-dev3.macroverse.workers.dev',
- [ServiceUrl.EMAIL_SERVICE_URL]: 'https://email-service-dev.macro.com',
+ [ServiceUrl.EMAIL_SERVICE_URL]: 'https://dev-gateway.macro.com/email',
[ServiceUrl.STATIC_FILE_SERVICE_URL]:
'https://static-file-service-dev.macro.com',
[ServiceUrl.NOTIFICATION_SERVICE_URL]:
@@ -55,7 +55,7 @@ const DEV_SERVICE_URLS: ServiceUrlMap = {
const PROD_SERVICE_URLS: ServiceUrlMap = {
[ServiceUrl.SYNC_SERVICE_URL]:
'https://sync-service-prod2.macroverse.workers.dev',
- [ServiceUrl.EMAIL_SERVICE_URL]: 'https://email-service.macro.com',
+ [ServiceUrl.EMAIL_SERVICE_URL]: 'https://gateway.macro.com/email',
[ServiceUrl.STATIC_FILE_SERVICE_URL]: 'https://static-file-service.macro.com',
[ServiceUrl.NOTIFICATION_SERVICE_URL]:
'https://gateway.macro.com/notification',
diff --git a/infra/stacks/email-service/service.ts b/infra/stacks/email-service/service.ts
index a41136d9bf..a0b24f4e58 100644
--- a/infra/stacks/email-service/service.ts
+++ b/infra/stacks/email-service/service.ts
@@ -8,15 +8,20 @@ import {
datadogAgentContainer,
fargateLogRouterSidecarContainer,
serviceLoadBalancer,
+ ServiceTargetGroup,
} from '../../packages/resources';
import { EcrImage } from '../../packages/service';
import {
BASE_DOMAIN,
CLOUD_TRAIL_SNS_TOPIC_ARN,
type DopplerEcsEnvironment,
+ getGatewayAlb,
+ GatewayService,
stack,
} from '../../packages/shared';
+const gatewayLoadBalancer = getGatewayAlb();
+
const BASE_NAME = 'email-service';
const REPO_ROOT = '../../..';
@@ -105,6 +110,22 @@ export class EmailService extends pulumi.ComponentResource {
this.serviceAlbSg = sg.serviceAlbSg;
this.serviceSg = sg.serviceSg;
+ const gatewayTargetGroup = new ServiceTargetGroup(
+ `${stack}-${BASE_NAME}`,
+ {
+ tags: this.tags,
+ listenerArn: gatewayLoadBalancer.httpsListenerArn,
+ vpcId: vpc.vpcId,
+ containerPort: serviceContainerPort,
+ service: GatewayService.EMAIL_SERVICE,
+ healthCheckPath,
+ pathPatterns: ['/email', '/email/*'],
+ serviceSecurityGroupId: this.serviceSg.id,
+ albSecurityGroupId: gatewayLoadBalancer.albSecurityGroupId,
+ },
+ { parent: this }
+ );
+
// lb
const { targetGroup, lb, listener } = serviceLoadBalancer(this, {
serviceName: BASE_NAME, // service name
@@ -134,6 +155,21 @@ export class EmailService extends pulumi.ComponentResource {
enable: true,
rollback: true,
},
+ // An explicit `loadBalancers` replaces the list awsx derives from
+ // `portMappings.targetGroup`, so the legacy entry must be listed here
+ // too.
+ loadBalancers: [
+ {
+ targetGroupArn: targetGroup.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ {
+ targetGroupArn: gatewayTargetGroup.target_group.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ ],
taskDefinitionArgs: {
taskRole: {
roleArn: this.role.arn,
@@ -188,12 +224,19 @@ export class EmailService extends pulumi.ComponentResource {
},
{
parent: this,
+ // ECS refuses a service whose target group is not yet associated with
+ // a load balancer; it is the listener rule that creates that
+ // association
+ dependsOn: [gatewayTargetGroup.listener_rule],
}
);
this.service = service;
- this.setupAutoScaling();
+ this.setupAutoScaling({
+ gatewayAlbArnSuffix: gatewayLoadBalancer.albArnSuffix,
+ gatewayTargetGroup: gatewayTargetGroup.target_group,
+ });
this.setupServiceAlarms();
@@ -321,7 +364,13 @@ export class EmailService extends pulumi.ComponentResource {
return { serviceAlbSg, serviceSg };
}
- setupAutoScaling() {
+ setupAutoScaling({
+ gatewayAlbArnSuffix,
+ gatewayTargetGroup,
+ }: {
+ gatewayAlbArnSuffix: pulumi.Output<string>;
+ gatewayTargetGroup: aws.lb.TargetGroup;
+ }) {
if (!this.service) return;
const serviceScalableTarget = new aws.appautoscaling.Target(
@@ -337,19 +386,7 @@ export class EmailService extends pulumi.ComponentResource {
{ parent: this }
);
- const lbPortion: pulumi.Output<string> = this.lb.arn.apply((arn) => {
- const parts = arn.split(':loadbalancer/');
- return parts[1];
- });
-
- const tgPortion: pulumi.Output<string> = this.targetGroup.arn.apply(
- (arn) => {
- const parts = arn.split(':');
- return parts[parts.length - 1];
- }
- );
-
- const resourceLabel = pulumi.interpolate`${lbPortion}/${tgPortion}`;
+ const resourceLabel = pulumi.interpolate`${gatewayAlbArnSuffix}/${gatewayTargetGroup.arnSuffix}`;
// Create an Auto Scaling policy for request count.
new aws.appautoscaling.Policy(
diff --git a/packages/sdk/src/config.ts b/packages/sdk/src/config.ts
index 21fa86bd99..a9450aaa04 100644
--- a/packages/sdk/src/config.ts
+++ b/packages/sdk/src/config.ts
@@ -28,7 +28,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {
'agent-harness': 'https://dev-gateway.macro.com/agent-harness',
storage: 'https://dev-gateway.macro.com/dss',
auth: 'https://dev-gateway.macro.com/auth',
- email: 'https://email-service-dev.macro.com',
+ email: 'https://dev-gateway.macro.com/email',
cognition: 'https://document-cognition-dev.macro.com',
notification: 'https://dev-gateway.macro.com/notification',
properties: 'https://dev-gateway.macro.com/dss',
@@ -43,7 +43,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {
'agent-harness': 'https://gateway.macro.com/agent-harness',
storage: 'https://gateway.macro.com/dss',
auth: 'https://gateway.macro.com/auth',
- email: 'https://email-service.macro.com',
+ email: 'https://gateway.macro.com/email',
cognition: 'https://document-cognition.macro.com',
notification: 'https://gateway.macro.com/notification',
properties: 'https://gateway.macro.com/dss',
diff --git a/services/email_service/src/api.rs b/services/email_service/src/api.rs
index 32c4494885..aaf8875b51 100644
--- a/services/email_service/src/api.rs
+++ b/services/email_service/src/api.rs
@@ -20,20 +20,22 @@ mod internal;
mod middleware;
pub(crate) mod swagger;
+#[cfg(test)]
+mod test;
+
+const GATEWAY_PATH_PREFIX: &str = "/email";
+
pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {
let env = state.config.environment;
let port = state.config.port;
- let app = api_router(state.clone())
- .with_state(state)
- .merge(health::router())
- .layer(
- ServiceBuilder::new()
- .layer(TraceLayer::new_for_http())
- .layer(macro_cors::cors_layer())
- .layer(CompressionLayer::new().gzip(true)),
- )
- // The health router is attached here so we don't attach the logging middleware to it
- .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()));
+ let traced_api = api_router(state.clone()).with_state(state).layer(
+ ServiceBuilder::new()
+ .layer(TraceLayer::new_for_http())
+ .layer(macro_cors::cors_layer())
+ .layer(CompressionLayer::new().gzip(true)),
+ );
+ let health = health::router();
+ let app = mount_at_root_and_prefix(traced_api.merge(health)).merge(swagger_ui());
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
.await
@@ -49,6 +51,21 @@ pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {
.context("error starting service")
}
+fn mount_at_root_and_prefix(inner: Router) -> Router {
+ Router::new()
+ .merge(inner.clone())
+ .nest(GATEWAY_PATH_PREFIX, inner)
+}
+
+fn swagger_ui() -> Router {
+ Router::new()
+ .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))
+ .merge(
+ SwaggerUi::new("/email/docs")
+ .url("/email/api-doc/openapi.json", swagger::ApiDoc::openapi()),
+ )
+}
+
fn api_router(state: ApiContext) -> Router<ApiContext> {
// Calendar mutations follow the calendar sync kill switch: without sync
// a provider write would never be reflected locally.
diff --git a/services/email_service/src/api/test.rs b/services/email_service/src/api/test.rs
new file mode 100644
index 0000000000..2de4855deb
--- /dev/null
+++ b/services/email_service/src/api/test.rs
@@ -0,0 +1,113 @@
+use super::{health, mount_at_root_and_prefix, swagger_ui};
+use axum::{
+ Router,
+ body::Body,
+ http::{Request, StatusCode},
+ routing::get,
+};
+use tower::ServiceExt;
+
+async fn ok() -> &'static str {
+ "ok"
+}
+
+fn sample_app() -> Router {
+ mount_at_root_and_prefix(
+ Router::new()
+ .route("/health", get(ok))
+ .route("/email/messages", get(ok))
+ .route("/gmail/webhook", get(ok))
+ .route("/calendar/notifications", get(ok))
+ .route("/internal/ping", get(ok)),
+ )
+}
+
+async fn get_status(app: Router, path: &str) -> StatusCode {
+ app.oneshot(
+ Request::builder()
+ .uri(path)
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap()
+ .status()
+}
+
+#[tokio::test]
+async fn health_is_reachable_at_root_and_gateway_prefix() {
+ for path in ["/health", "/email/health"] {
+ let response = mount_at_root_and_prefix(health::router())
+ .oneshot(
+ Request::builder()
+ .uri(path)
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK, "{path}");
+ }
+}
+
+#[tokio::test]
+async fn existing_paths_stay_and_are_also_served_under_the_prefix() {
+ for path in [
+ "/health",
+ "/email/health",
+ "/email/messages",
+ "/email/email/messages",
+ "/gmail/webhook",
+ "/email/gmail/webhook",
+ "/calendar/notifications",
+ "/email/calendar/notifications",
+ "/internal/ping",
+ "/email/internal/ping",
+ ] {
+ assert_eq!(
+ get_status(sample_app(), path).await,
+ StatusCode::OK,
+ "{path}"
+ );
+ }
+}
+
+#[tokio::test]
+async fn unprefixed_unknown_path_is_not_rewritten_onto_the_prefix() {
+ let response = mount_at_root_and_prefix(health::router())
+ .oneshot(
+ Request::builder()
+ .uri("/missing")
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
+}
+
+#[tokio::test]
+async fn openapi_is_served_at_root_and_gateway_prefix() {
+ let api = swagger_ui();
+
+ for uri in ["/api-doc/openapi.json", "/email/api-doc/openapi.json"] {
+ let response = api
+ .clone()
+ .oneshot(
+ Request::builder()
+ .uri(uri)
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK, "{uri}");
+ }
+}