A production-grade Flask-based chatbot for 3MTT customer support with enterprise-level security, monitoring, and scalability.
- JWT Authentication for admin endpoints
- Rate Limiting with Redis backend
- Input Sanitization and validation
- CORS Protection with configurable origins
- Security Headers (CSP, HSTS, XSS Protection)
- Password Hashing with bcrypt
- SQL Injection Protection with SQLAlchemy ORM
- PostgreSQL Database for persistent storage
- Redis Caching for AI responses and sessions
- Gunicorn WSGI Server with multiple workers
- Nginx Reverse Proxy with load balancing
- Docker Containerization for easy deployment
- Database Migrations with Flask-Migrate
- Prometheus Metrics for performance monitoring
- Structured Logging with JSON format
- Sentry Integration for error tracking
- Health Check Endpoints for uptime monitoring
- Request/Response Tracking with unique IDs
- GitHub Actions for automated testing and deployment
- Docker Compose for local development
- Security Scanning with Bandit and Safety
- Automated Testing with pytest
- Database Migrations in deployment pipeline
- Python 3.11+
- PostgreSQL 15+
- Redis 7+
- Docker & Docker Compose (optional)
- Nginx (for production)
# Copy environment template
cp .env.example .env
# Edit .env with your configuration
nano .env# Install PostgreSQL and create database
sudo apt install postgresql postgresql-contrib
sudo -u postgres createdb chatbot_db
sudo -u postgres createuser chatbot# Install dependencies
pip install -r requirements.txt
# Initialize database
flask db init
flask db migrate -m "Initial migration"
flask db upgrade
# Create admin user
python -c "
from app import create_app
from models import db, AdminUser
from auth import hash_password
app = create_app()
with app.app_context():
admin = AdminUser(username='admin', password_hash=hash_password('your_secure_password'))
db.session.add(admin)
db.session.commit()
"# Using Docker Compose (Recommended)
docker-compose up -d
# Or manual deployment
gunicorn --config gunicorn.conf.py wsgi:app| Variable | Description | Default |
|---|---|---|
FLASK_ENV |
Environment (development/production) | production |
SECRET_KEY |
Flask secret key | Required |
DATABASE_URL |
PostgreSQL connection string | Required |
REDIS_URL |
Redis connection string | redis://localhost:6379/0 |
OPENAI_API_KEY |
OpenAI API key | Optional |
SENTRY_DSN |
Sentry error tracking DSN | Optional |
RATE_LIMIT_PER_MINUTE |
API rate limit | 10 |
# Generate secure secret key
python -c "import secrets; print(secrets.token_hex(32))"
# Hash admin password
python -c "from auth import hash_password; print(hash_password('your_password'))"- HTTP request metrics
- Chat interaction metrics
- AI response times
- Active session counts
Access metrics at: http://localhost:9090/metrics
- Database connectivity
- Redis connectivity
- Disk space monitoring
- AI service status
Access health check at: http://localhost:5000/health
- Conversation analytics
- User sentiment analysis
- Performance metrics
- System status
Access admin at: http://localhost:5000/admin/analytics
# Run all tests
python -m pytest tests/ -v
# Run with coverage
python -m pytest tests/ --cov=app --cov-report=html
# Security scanning
bandit -r . -x tests/
safety check# Build and run locally
docker-compose up -d
# Scale workers
docker-compose up -d --scale web=3# Automated deployment script
./scripts/deploy.sh
# Or manual Terraform deployment
cd terraform && terraform apply# Push to main branch triggers AWS deployment
git push origin mainFor detailed AWS deployment instructions, see AWS_DEPLOYMENT.md
- Change default passwords in production
- Use HTTPS with valid SSL certificates
- Configure firewall to restrict access
- Regular security updates for dependencies
- Monitor logs for suspicious activity
- Backup database regularly
- Use environment variables for secrets
- Redis caching for AI responses
- Database indexing on frequently queried fields
- Nginx compression for static assets
- Connection pooling for database
- Worker process scaling based on load
Database Connection Error
# Check PostgreSQL status
sudo systemctl status postgresql
# Check connection string in .envRedis Connection Error
# Check Redis status
sudo systemctl status redis
# Test connection: redis-cli pingHigh Memory Usage
# Restart workers periodically
# Monitor with: docker stats
# Adjust worker count in gunicorn.conf.pyPOST /chat
Content-Type: application/json
{
"message": "Your question here"
}POST /admin/login
Content-Type: application/json
{
"username": "admin",
"password": "your_password"
}GET /admin/analytics
Authorization: Bearer <jwt_token>- Fork the repository
- Create feature branch
- Add tests for new features
- Run security scans
- Submit pull request
This project is licensed under the MIT License.
For production support:
- Check logs:
docker-compose logs web - Monitor metrics:
http://localhost:9090 - Health status:
http://localhost:5000/health - Error tracking: Sentry dashboard