# Arquitetura Completa - Aggios ## πŸ—οΈ Diagrama de Arquitetura ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ INTERNET / CLIENTES β”‚ β”‚ (Web Browsers, Mobile Apps, Third-party Integrations) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ TRAEFIK (Reverse Proxy) β”‚ β”‚ - Load Balancing β”‚ β”‚ - SSL/TLS (Let's Encrypt) β”‚ β”‚ - Domain Routing β”‚ β”‚ - Rate Limiting β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚Frontendβ”‚ β”‚Frontendβ”‚ β”‚Backend β”‚ β”‚Inst. β”‚ β”‚Dash β”‚ β”‚API (Go)β”‚ β”‚(Next) β”‚ β”‚(Next) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ PostgreSQL β”‚ β”‚ Redis β”‚ β”‚ MinIO β”‚ β”‚ (Banco) β”‚ β”‚ (Cache) β”‚ β”‚ (Storage) β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ - Users β”‚ β”‚ - Sessions β”‚ β”‚ - Documentos β”‚ β”‚ - Tenants β”‚ β”‚ - Cache β”‚ β”‚ - Images β”‚ β”‚ - Data β”‚ β”‚ - Rate Limit β”‚ β”‚ - Backups β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸ”„ Fluxo de RequisiΓ§Γ΅es ### 1. Acesso Web (Navegador) ``` Navegador (usuario.aggios.app) ↓ Traefik (DNS: usuario.aggios.app) ↓ Frontend Next.js ↓ (fetch /api/*) Traefik ↓ Backend API Go ↓ PostgreSQL/Redis/MinIO ``` ### 2. Acesso Multi-Tenant ``` Cliente de AgΓͺncia A (acme.aggios.app) ↓ Traefik (wildcard *.aggios.app) ↓ Backend API (extrai tenant_id do JWT) ↓ Query com filtro: WHERE tenant_id = 'acme' ↓ PostgreSQL (isolamento garantido) ``` ### 3. Fluxo de AutenticaΓ§Γ£o ``` 1. POST /api/auth/login β†’ Validar email/password β†’ Gerar JWT com tenant_id β†’ Salvar refresh_token em Redis 2. RequisiΓ§Γ£o autenticada β†’ Bearer {JWT} β†’ Middleware valida JWT β†’ Extrai user_id, email, tenant_id β†’ Passa ao handler 3. Acesso a recurso β†’ Backend filtra: SELECT * FROM users WHERE tenant_id = ? AND ... β†’ Garante isolamento de dados ``` ## πŸ“Š Estrutura de Dados (PostgreSQL) ```sql -- Tenants (Multi-tenant) tenants β”œβ”€β”€ id (UUID) β”œβ”€β”€ name β”œβ”€β”€ domain β”œβ”€β”€ subdomain β”œβ”€β”€ is_active β”œβ”€β”€ created_at └── updated_at -- UsuΓ‘rios (isolados por tenant) users β”œβ”€β”€ id (UUID) β”œβ”€β”€ email (UNIQUE) β”œβ”€β”€ password_hash β”œβ”€β”€ first_name β”œβ”€β”€ last_name β”œβ”€β”€ tenant_id (FK β†’ tenants) β”œβ”€β”€ is_active β”œβ”€β”€ created_at └── updated_at -- Refresh Tokens (sessΓ΅es) refresh_tokens β”œβ”€β”€ id (UUID) β”œβ”€β”€ user_id (FK β†’ users) β”œβ”€β”€ token_hash β”œβ”€β”€ expires_at └── created_at -- Índices para performance β”œβ”€β”€ users.email β”œβ”€β”€ users.tenant_id β”œβ”€β”€ tenants.domain β”œβ”€β”€ tenants.subdomain └── refresh_tokens.expires_at ``` ## πŸ” Modelo de SeguranΓ§a ### JWT Token Structure ``` Header: { "alg": "HS256", "typ": "JWT" } Payload: { "user_id": "123e4567-e89b-12d3-a456-426614174000", "email": "user@example.com", "tenant_id": "acme", "exp": 1733462400, "iat": 1733376000, "jti": "unique-token-id" } Signature: HMACSHA256(base64(header) + "." + base64(payload), JWT_SECRET) ``` ### Camadas de SeguranΓ§a ``` 1. TRANSPORT (Traefik) β”œβ”€β”€ HTTPS/TLS (Let's Encrypt) β”œβ”€β”€ HSTS Headers └── Rate Limiting 2. APPLICATION (Backend) β”œβ”€β”€ JWT Validation β”œβ”€β”€ CORS Checking β”œβ”€β”€ Input Validation β”œβ”€β”€ Password Hashing (Argon2) └── SQL Injection Prevention 3. DATABASE (PostgreSQL) β”œβ”€β”€ Prepared Statements β”œβ”€β”€ Row-level Security (RLS) β”œβ”€β”€ Encrypted Passwords └── Audit Logging 4. DATA (Storage) β”œβ”€β”€ Tenant Isolation β”œβ”€β”€ Access Control β”œβ”€β”€ Encryption at rest (MinIO) └── Versioning ``` ## 🌍 Multi-Tenant Architecture ### Routing Pattern ``` Domain Pattern: {subdomain}.aggios.app Examples: - api.aggios.app β†’ General API - acme.aggios.app β†’ Tenant ACME - empresa1.aggios.app β†’ Tenant Empresa1 - usuario2.aggios.app β†’ Tenant Usuario2 Traefik Rule: HostRegexp(`{subdomain:[a-z0-9-]+}\.aggios\.app`) ``` ### Data Isolation ``` Level 1: Network β”œβ”€β”€ Traefik routes by subdomain └── Passes to single backend instance Level 2: Application β”œβ”€β”€ JWT contains tenant_id β”œβ”€β”€ Every query filtered by tenant_id └── Cross-tenant access impossible Level 3: Database β”œβ”€β”€ Indexes on (tenant_id, field) β”œβ”€β”€ Foreign key constraints └── Audit trail per tenant Level 4: Storage β”œβ”€β”€ MinIO bucket: aggios/{tenant_id}/* β”œβ”€β”€ Separate namespaces └── Access control per tenant ``` ## πŸ“¦ Docker Stack (Compose) ```yaml Services: β”œβ”€β”€ Traefik (1 instance) β”‚ β”œβ”€β”€ Port: 80, 443 β”‚ β”œβ”€β”€ Dashboard: :8080 β”‚ └── Provider: Docker β”‚ β”œβ”€β”€ Backend (1+ instances) β”‚ β”œβ”€β”€ Port: 8080 β”‚ β”œβ”€β”€ Replicas: configurable β”‚ └── Load balanced by Traefik β”‚ β”œβ”€β”€ PostgreSQL (1 primary + optional replicas) β”‚ β”œβ”€β”€ Port: 5432 β”‚ β”œβ”€β”€ Persistence: volume β”‚ └── Health check: enabled β”‚ β”œβ”€β”€ Redis (1 instance) β”‚ β”œβ”€β”€ Port: 6379 β”‚ β”œβ”€β”€ Persistence: optional (RDB/AOF) β”‚ └── Password: required β”‚ β”œβ”€β”€ MinIO (1+ instances) β”‚ β”œβ”€β”€ API: 9000 β”‚ β”œβ”€β”€ Console: 9001 β”‚ β”œβ”€β”€ Replicas: configurable β”‚ └── Persistence: volume β”‚ β”œβ”€β”€ Frontend Institucional (Next.js) β”‚ └── Port: 3000 β”‚ └── Frontend Dashboard (Next.js) └── Port: 3000 ``` ## πŸ”„ Scaling Strategy ### Horizontal Scaling ``` Fase 1 (Development) β”œβ”€β”€ 1x Backend β”œβ”€β”€ 1x PostgreSQL β”œβ”€β”€ 1x Redis └── 1x MinIO Fase 2 (Small Production) β”œβ”€β”€ 2x Backend (load balanced) β”œβ”€β”€ 1x PostgreSQL + 1x Read Replica β”œβ”€β”€ 1x Redis (ou Redis Cluster) └── 1x MinIO (ou MinIO Cluster) Fase 3 (Large Production) β”œβ”€β”€ 3-5x Backend β”œβ”€β”€ 1x PostgreSQL (primary) + 2x Replicas β”œβ”€β”€ Redis Cluster (3+ nodes) β”œβ”€β”€ MinIO Cluster (4+ nodes) └── Kubernetes (optional) ``` ## πŸ“± API Clients ### Web (JavaScript/TypeScript) ```javascript // fetch com JWT const response = await fetch('/api/users/me', { headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }); ``` ### Mobile (React Native / Flutter) ```javascript // NΓ£o diferente de web // Salvar tokens em AsyncStorage/SecureStorage // Usar interceptors para auto-refresh ``` ### Third-party Integration ```bash # Via API Key ou OAuth2 curl -X GET https://api.aggios.app/api/data \ -H "Authorization: Bearer {api_key}" \ -H "X-API-Version: v1" ``` ## πŸš€ Pipeline de Deploy ``` 1. Git Push ↓ 2. CI/CD (GitHub Actions / GitLab CI) β”œβ”€β”€ Build Backend β”œβ”€β”€ Run Tests β”œβ”€β”€ Build Docker Image └── Push to Registry ↓ 3. Deploy (Docker Compose / Kubernetes) β”œβ”€β”€ Pull Image β”œβ”€β”€ Run Migrations β”œβ”€β”€ Health Check └── Traffic Switch ↓ 4. Monitoring β”œβ”€β”€ Logs (ELK / Datadog) β”œβ”€β”€ Metrics (Prometheus) β”œβ”€β”€ Errors (Sentry) └── Alerts ``` ## πŸ“ˆ Monitoring & Observability ``` Logs β”œβ”€β”€ Traefik Access Logs β”œβ”€β”€ Backend Application Logs β”œβ”€β”€ PostgreSQL Slow Queries └── MinIO Request Logs ↓ ELK / Datadog / CloudWatch Metrics β”œβ”€β”€ Request Rate / Latency β”œβ”€β”€ DB Connection Pool β”œβ”€β”€ Redis Memory / Ops β”œβ”€β”€ MinIO Throughput └── Docker Container Stats ↓ Prometheus / Grafana Tracing (Distributed) β”œβ”€β”€ Request ID propagation β”œβ”€β”€ Service-to-service calls └── Database queries ↓ Jaeger / OpenTelemetry Errors β”œβ”€β”€ Panics β”œβ”€β”€ Validation Errors β”œβ”€β”€ DB Errors └── 5xx Responses ↓ Sentry / Rollbar ``` ## πŸ”§ ManutenΓ§Γ£o ### Backups ``` PostgreSQL β”œβ”€β”€ Full backup (diΓ‘rio) β”œβ”€β”€ Incremental (a cada 6h) └── WAL archiving MinIO β”œβ”€β”€ Bucket replication β”œβ”€β”€ Cross-region backup └── Versioning enabled Redis β”œβ”€β”€ RDB snapshots (diΓ‘rio) └── AOF opcional ``` ### Updates ``` 1. Traefik └── In-place upgrade (zero-downtime) 2. Backend β”œβ”€β”€ Blue-green deployment β”œβ”€β”€ Canary releases └── Automatic rollback 3. PostgreSQL β”œβ”€β”€ Replica first β”œβ”€β”€ Failover test └── Maintenance window 4. Redis └── Cluster rebalance (zero-downtime) 5. MinIO └── Rolling update ``` --- **Diagrama criado**: Dezembro 2025 **VersΓ£o**: 1.0.0