feat(infra): consolidate 21 Matrix bots into Go binary + add Go API gateway

Replace 21 separate NestJS Matrix bot processes (~2.1 GB RAM, ~4.2 GB Docker images)
with a single Go binary using plugin architecture (8.6 MB binary, ~30 MB RAM).

New services:
- services/mana-matrix-bot/ — Go Matrix bot with 21 plugins (mautrix-go, Redis sessions)
- services/mana-api-gateway-go/ — Go API gateway (rate limiting, API keys, credit billing)

Deleted:
- 21 services/matrix-*-bot/ directories
- packages/bot-services/ and packages/matrix-bot-common/
- Legacy deploy scripts and CI build jobs

Updated:
- docker-compose.macmini.yml: new Go services, legacy bots removed
- CI/CD: change detection + build jobs for Go services
- Root package.json: new dev:matrix, build:matrix, test:matrix scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-03-27 21:03:00 +01:00
parent ce51fd5fe2
commit 819568c3df
503 changed files with 9927 additions and 47044 deletions

View file

@ -103,6 +103,7 @@ if [ $# -eq 0 ]; then
echo " $0 todo-web todo-backend # Build & restart both"
echo " $0 --base # Rebuild base images"
echo " $0 --all-web # Rebuild all web apps"
echo " $0 mana-matrix-bot # Build & restart consolidated Matrix bot (Go)"
exit 1
fi

View file

@ -1,134 +0,0 @@
#!/bin/bash
# Deploy Matrix Mana Bot (Gateway) to Mac Mini
# This script handles the complete deployment process
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
echo "============================================"
echo " Matrix Mana Bot - Full Deployment"
echo "============================================"
echo ""
cd "$PROJECT_DIR"
# Check if .env exists and has the token
if ! grep -q "MATRIX_MANA_BOT_TOKEN" .env 2>/dev/null; then
echo -e "${YELLOW}Warning: MATRIX_MANA_BOT_TOKEN not found in .env${NC}"
echo "Run ./scripts/mac-mini/setup-mana-bot.sh first to register the bot."
echo ""
read -p "Continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Step 1: Pull latest code
echo -e "${CYAN}Step 1: Pulling latest code...${NC}"
git pull --ff-only || {
echo -e "${YELLOW}Git pull failed. You may have local changes.${NC}"
read -p "Continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
}
# Step 2: Build shared package
echo ""
echo -e "${CYAN}Step 2: Building @manacore/bot-services...${NC}"
cd "$PROJECT_DIR/packages/bot-services"
pnpm install --frozen-lockfile 2>/dev/null || pnpm install
pnpm build || {
echo -e "${RED}Failed to build bot-services package${NC}"
exit 1
}
echo -e "${GREEN}bot-services built successfully${NC}"
# Step 3: Build gateway bot
echo ""
echo -e "${CYAN}Step 3: Building matrix-mana-bot...${NC}"
cd "$PROJECT_DIR/services/matrix-mana-bot"
pnpm install --frozen-lockfile 2>/dev/null || pnpm install
pnpm build || {
echo -e "${RED}Failed to build matrix-mana-bot${NC}"
exit 1
}
echo -e "${GREEN}matrix-mana-bot built successfully${NC}"
# Step 4: Build Docker image
echo ""
echo -e "${CYAN}Step 4: Building Docker image...${NC}"
cd "$PROJECT_DIR"
docker build -t matrix-mana-bot:latest ./services/matrix-mana-bot || {
echo -e "${RED}Failed to build Docker image${NC}"
exit 1
}
echo -e "${GREEN}Docker image built successfully${NC}"
# Step 5: Stop existing container if running
echo ""
echo -e "${CYAN}Step 5: Stopping existing container...${NC}"
docker compose -f docker-compose.macmini.yml stop matrix-mana-bot 2>/dev/null || true
docker compose -f docker-compose.macmini.yml rm -f matrix-mana-bot 2>/dev/null || true
# Step 6: Start new container
echo ""
echo -e "${CYAN}Step 6: Starting matrix-mana-bot...${NC}"
docker compose -f docker-compose.macmini.yml up -d matrix-mana-bot || {
echo -e "${RED}Failed to start container${NC}"
exit 1
}
# Step 7: Wait for health check
echo ""
echo -e "${CYAN}Step 7: Waiting for health check...${NC}"
for i in {1..30}; do
if curl -s http://localhost:3310/health > /dev/null 2>&1; then
echo -e "${GREEN}Health check passed!${NC}"
break
fi
if [ $i -eq 30 ]; then
echo -e "${RED}Health check failed after 30 seconds${NC}"
echo "Check logs with: docker logs manacore-matrix-mana-bot"
exit 1
fi
echo -n "."
sleep 1
done
# Step 8: Show status
echo ""
echo "============================================"
echo -e "${GREEN} Deployment Complete!${NC}"
echo "============================================"
echo ""
echo "Container Status:"
docker ps --filter "name=manacore-matrix-mana-bot" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
echo "Health Check:"
curl -s http://localhost:3310/health | jq . 2>/dev/null || curl -s http://localhost:3310/health
echo ""
echo ""
echo "Next Steps:"
echo "1. Invite the bot to a Matrix room:"
echo " /invite @mana:mana.how"
echo ""
echo "2. Test with:"
echo " hilfe"
echo " !todo Test aufgabe"
echo " !list"
echo ""
echo "3. View logs with:"
echo " docker logs -f manacore-matrix-mana-bot"
echo ""

View file

@ -1,160 +0,0 @@
#!/bin/bash
# Register and setup Matrix Mana Bot (Gateway)
# Run this after Matrix Synapse is running
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
echo "============================================"
echo " Matrix Mana Bot Setup"
echo "============================================"
echo ""
# Default values
HOMESERVER_URL="${MATRIX_HOMESERVER_URL:-http://localhost:8008}"
BOT_USERNAME="mana"
BOT_DISPLAY_NAME="Mana"
# Check if Synapse is running
echo "Checking Synapse..."
if ! curl -s "${HOMESERVER_URL}/health" > /dev/null 2>&1; then
echo -e "${RED}Error: Synapse is not reachable at ${HOMESERVER_URL}${NC}"
echo "Start it with: docker compose -f docker-compose.macmini.yml up -d synapse"
exit 1
fi
echo -e "${GREEN}Synapse is running${NC}"
echo ""
# Check if registration secret is available
if [ -z "$SYNAPSE_REGISTRATION_SECRET" ]; then
echo -e "${YELLOW}SYNAPSE_REGISTRATION_SECRET not set.${NC}"
echo "Please provide the registration secret from your .env file:"
read -sp "Registration Secret: " SYNAPSE_REGISTRATION_SECRET
echo ""
fi
# Generate bot password
BOT_PASSWORD=$(openssl rand -base64 24)
echo "Registering bot user @${BOT_USERNAME}..."
# Generate HMAC for registration
generate_mac() {
local nonce=$1
local user=$2
local password=$3
local user_type=$4
local admin=$5
local mac_input="${nonce}\x00${user}\x00${password}\x00${user_type}\x00${admin}"
echo -n "$mac_input" | openssl dgst -sha1 -hmac "$SYNAPSE_REGISTRATION_SECRET" | cut -d' ' -f2
}
# Get nonce
NONCE=$(curl -s "${HOMESERVER_URL}/_synapse/admin/v1/register" | jq -r '.nonce')
if [ -z "$NONCE" ] || [ "$NONCE" = "null" ]; then
echo -e "${RED}Failed to get registration nonce. Is admin registration enabled?${NC}"
exit 1
fi
# Calculate MAC
MAC=$(generate_mac "$NONCE" "$BOT_USERNAME" "$BOT_PASSWORD" "bot" "false")
# Register user
REGISTER_RESPONSE=$(curl -s -X POST "${HOMESERVER_URL}/_synapse/admin/v1/register" \
-H "Content-Type: application/json" \
-d "{
\"nonce\": \"${NONCE}\",
\"username\": \"${BOT_USERNAME}\",
\"password\": \"${BOT_PASSWORD}\",
\"displayname\": \"${BOT_DISPLAY_NAME}\",
\"user_type\": \"bot\",
\"admin\": false,
\"mac\": \"${MAC}\"
}")
# Check if registration was successful
if echo "$REGISTER_RESPONSE" | jq -e '.access_token' > /dev/null 2>&1; then
ACCESS_TOKEN=$(echo "$REGISTER_RESPONSE" | jq -r '.access_token')
USER_ID=$(echo "$REGISTER_RESPONSE" | jq -r '.user_id')
echo -e "${GREEN}Bot registered successfully!${NC}"
echo ""
echo -e "${CYAN}User ID:${NC} ${USER_ID}"
echo ""
else
ERROR=$(echo "$REGISTER_RESPONSE" | jq -r '.error // .errcode // "Unknown error"')
# Check if user already exists
if echo "$ERROR" | grep -qi "user.*exists\|already.*registered\|M_USER_IN_USE"; then
echo -e "${YELLOW}User @${BOT_USERNAME} already exists. Getting access token via login...${NC}"
echo "Please enter the existing bot password:"
read -sp "Password: " EXISTING_PASSWORD
echo ""
LOGIN_RESPONSE=$(curl -s -X POST "${HOMESERVER_URL}/_matrix/client/r0/login" \
-H "Content-Type: application/json" \
-d "{
\"type\": \"m.login.password\",
\"user\": \"${BOT_USERNAME}\",
\"password\": \"${EXISTING_PASSWORD}\"
}")
if echo "$LOGIN_RESPONSE" | jq -e '.access_token' > /dev/null 2>&1; then
ACCESS_TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.access_token')
USER_ID=$(echo "$LOGIN_RESPONSE" | jq -r '.user_id')
echo -e "${GREEN}Login successful!${NC}"
else
echo -e "${RED}Login failed. Please check the password.${NC}"
exit 1
fi
else
echo -e "${RED}Registration failed: ${ERROR}${NC}"
exit 1
fi
fi
echo ""
echo "============================================"
echo " Add to .env file"
echo "============================================"
echo ""
echo -e "${CYAN}# Matrix Mana Bot (Gateway)${NC}"
echo "MATRIX_MANA_BOT_TOKEN=${ACCESS_TOKEN}"
echo ""
# Optional: Set display name and avatar
echo "Setting display name..."
curl -s -X PUT "${HOMESERVER_URL}/_matrix/client/r0/profile/${USER_ID}/displayname" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"displayname\": \"🤖 ${BOT_DISPLAY_NAME}\"}" > /dev/null
echo ""
echo "============================================"
echo " Next Steps"
echo "============================================"
echo ""
echo "1. Add the MATRIX_MANA_BOT_TOKEN to your .env file"
echo ""
echo "2. Build the bot image:"
echo " docker build -t matrix-mana-bot ./services/matrix-mana-bot"
echo ""
echo "3. Start the bot:"
echo " docker compose -f docker-compose.macmini.yml up -d matrix-mana-bot"
echo ""
echo "4. Invite the bot to a room in Element:"
echo " /invite @mana:mana.how"
echo ""
echo -e "${GREEN}Setup complete!${NC}"