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.

1.9 KB
Raw Blame
#![recursion_limit = "256"]
mod config;
mod context;
mod handler;

use anyhow::Context;
use aws_lambda_events::event::eventbridge::EventBridgeEvent;
use config::Config;
use handler::handler;
use lambda_runtime::{
    Error, LambdaEvent, run, service_fn,
    tracing::{self},
};
use macro_entrypoint::MacroEntrypoint;
use macro_event_broker::{GlobalSpawner, KafkaEventPublisher};
use sqlx::postgres::PgPoolOptions;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Error> {
    MacroEntrypoint::default().init();
    tracing::trace!("initiating lambda");

    let config = Config::from_env().context("all necessary env vars should be available")?;

    tracing::trace!("initialized config");

    let macro_event_broker = context::PollerEventBroker::new(
        KafkaEventPublisher::new(config.kafka_brokers.as_ref())
            .context("failed to create kafka event publisher")?,
        GlobalSpawner,
    );

    // We should only ever need 1 connection
    let db = PgPoolOptions::new()
        .min_connections(3)
        .max_connections(3) // We want 1 db connection per dss item (document, project, chat)
        .connect(&config.database_url)
        .await
        .context("could not connect to db")?;

    let document_delete_queue = macro_queues::DocumentDeleteQueue::new();
    let chat_delete_queue = macro_queues::ChatDeleteQueue::new();
    let sqs_client = sqs_client::SQS::new(aws_sdk_sqs::Client::new(
        &macro_aws_config::get_macro_aws_config().await,
    ))
    .document_delete_queue(&document_delete_queue)
    .chat_delete_queue(&chat_delete_queue);

    let ctx = context::Context {
        db,
        macro_event_broker,
        sqs_client: Arc::new(sqs_client),
    };

    let func = service_fn(move |event: LambdaEvent<EventBridgeEvent>| {
        let ctx = ctx.clone();

        async move { handler(ctx, event).await }
    });

    run(func).await
}