1. Architecture
MAC is built as a modern, containerised web application using a microservices architecture. All services communicate over a shared Docker network.
1.1. Technology Stack
Layer |
Technology |
|---|---|
Backend API |
Python 3.11, FastAPI 0.115, Uvicorn (async) |
Database |
PostgreSQL 16 via SQLAlchemy 2.0 async (asyncpg) |
Cache / Rate-limit |
Redis 7 |
Vector DB (RAG) |
Qdrant |
Web Search |
SearXNG (self-hosted) |
LLM Inference |
vLLM (GPU) – primary; Ollama fallback |
STT |
faster-whisper (CPU) |
TTS |
Piper TTS via openedai-speech (CPU) |
Frontend |
Vanilla JS SPA – no build step, no framework |
Code Editor |
Monaco Editor 0.52.2 (VS Code engine) |
Reverse Proxy |
Nginx Alpine |
Containerisation |
Docker Compose |
Migrations |
Alembic |
Authentication |
JWT (HS256) + bcrypt + scoped API keys |
1.2. Docker Services
All services run on a shared Docker network mac-net:
Container |
Image |
Port |
Purpose |
|---|---|---|---|
|
|
|
FastAPI backend |
|
|
|
Reverse proxy + static frontend |
|
|
|
PostgreSQL database |
|
|
|
Redis cache |
|
|
|
Vector database |
|
|
|
Web search engine |
|
|
|
GPU inference |
|
|
|
Speech-to-text |
1.3. Project Structure
MAC/
├── mac/ # FastAPI backend
│ ├── main.py # App factory, 30+ routers
│ ├── config.py # Pydantic Settings (env vars)
│ ├── database.py # SQLAlchemy async engine
│ ├── middleware/ # Auth, feature gates, rate limiting
│ ├── models/ # SQLAlchemy ORM models
│ ├── routers/ # REST + WebSocket route modules
│ ├── services/ # Business logic
│ ├── schemas/ # Pydantic request/response schemas
│ └── utils/ # Security utilities
├── frontend/ # Vanilla JS SPA (no build step)
│ ├── index.html # Entry point
│ ├── style.css # All CSS (~109 KB)
│ ├── js/ # Modular JS files
│ │ ├── core.js # Router, state management
│ │ ├── auth.js # Login/signup UI
│ │ ├── chat.js # AI Chat interface
│ │ ├── notebooks.js # Notebook functionality
│ │ ├── mbmbook.js # MBM Book IDE
│ │ ├── admin.js # Admin panel (part 1)
│ │ ├── admin2.js # Admin panel (part 2)
│ │ ├── shell.js # App chrome (sidebar, header)
│ │ ├── dashboard.js # Dashboard rendering
│ │ ├── settings.js # User settings
│ │ ├── i18n.js # 19-language translations
│ │ └── ... # 19 total JS modules
│ ├── libs/ # Bundled JS libraries (offline-ready)
│ ├── sw.js # Service worker (PWA)
│ └── manifest.json # PWA manifest
├── docker/
│ └── workspace/ # MBM Book Docker image
├── alembic/ # Database migrations
├── nginx/ # Nginx configuration
├── docker-compose.yml # Production compose file
├── docker-compose.worker.yml # Worker node compose file
├── Dockerfile # Backend image
├── .env.example # Configuration template
├── start-mac.bat # Windows startup script
└── mac-installer.iss # Inno Setup installer script
1.4. Database Schema
MAC uses PostgreSQL 16 with the following tables:
Table |
Model File |
Description |
|---|---|---|
|
|
Core user accounts with roles and quotas |
|
|
Pre-registered student/faculty/admin entries |
|
|
JWT refresh token storage |
|
|
Per-request token usage logging |
|
|
RAG document collections |
|
|
Individual uploaded documents |
|
|
Notebook metadata |
|
|
Individual notebook cells |
|
|
Forum questions |
|
|
Forum answers |
|
|
Uploaded shared files |
|
|
Feature toggle configuration |
|
|
Content safety rules |
|
|
Cluster worker nodes |
|
|
User notifications |
|
|
Administrative audit trail |
1.5. Nginx Routing
# Frontend SPA
/ → /app (index.html fallback)
/static/* → /app/ (7-day cache)
/static/libs/* → /app/libs/ (1-year immutable cache)
/sw.js → no-cache service worker
# Backend API
/api/* → proxy_pass mac:8000 (rate-limited 10 req/s)
/ws/* → WebSocket proxy_pass mac:8000 (1h timeout)
/docs → FastAPI Swagger UI
/redoc → FastAPI ReDoc
1.6. Security Architecture
JWT Authentication: HS256 algorithm, 24-hour access tokens, 30-day refresh tokens
Password Hashing: bcrypt with automatic salt generation
Account Lockout: Progressive lockout after failed login attempts
Rate Limiting: Redis sliding window – 100 requests/hour, 50K tokens/day per user
Scoped API Keys:
mac_sk_<hex>– limited scope, individually revocableContent Guardrails: Admin-configurable rules checked before LLM calls
Kernel Isolation: Code cells execute in Docker containers with resource limits
CORS: Configurable via
MAC_CORS_ORIGINSenvironment variableXSS Protection: HTML escaping via
esc()helper throughout the frontend