# Zest

Python programming challenge platform with secure code execution.

## Structure

```
zest/
├── website/           # Flask web application
│   ├── auth.py       # User authentication
│   ├── models.py     # Database models
│   ├── questions.py  # Question routes
│   ├── api.py        # Submission API
│   └── templates/    # HTML templates
├── aws-lambda/       # Code executor (Docker container)
├── terraform/        # Infrastructure as code
├── tests/            # Pytest test suite
└── src/zest/         # Python package
```

## Local Development

```bash
# Install dependencies
pip install -e ".[dev]"

# Run development server
python run.py

# Run tests
pytest
```

## Deployment

### AWS Lambda (Code Executor)

```bash
cd terraform
terraform init
terraform apply -target=aws_ecr_repository.executor

# Build and push Docker image
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $(terraform output -raw ecr_repository_url | cut -d'/' -f1)
cd ../aws-lambda && docker build -t zest-code-executor .
docker tag zest-code-executor:latest $(cd ../terraform && terraform output -raw ecr_repository_url):latest
docker push $(cd ../terraform && terraform output -raw ecr_repository_url):latest

# Deploy Lambda + API Gateway
cd ../terraform && terraform apply

# Save these outputs
terraform output api_gateway_url
terraform output -raw api_key_value
```

### PythonAnywhere (Web App)

```bash
# Upload code via Git or Files tab
git clone https://github.com/yourusername/zest.git

# Create virtualenv in PythonAnywhere console
mkvirtualenv --python=/usr/bin/python3.10 zest
pip install -e .

# Configure web app:
# - Source code: /home/username/zest
# - Working directory: /home/username/zest
# - Virtualenv: /home/username/.virtualenvs/zest
# - WSGI file: from website import create_app; application = create_app()

# Set environment variables in WSGI file:
import os
os.environ['AWS_LAMBDA_URL'] = 'your-api-gateway-url'
os.environ['AWS_API_KEY'] = 'your-api-key'
os.environ['SECRET_KEY'] = 'generate-random-key'

# Reload web app
```
aws logs tail /aws/lambda/zest-code-executor --follow
```

View API Gateway metrics in AWS Console or CloudWatch.

## Cost Estimates

For low-moderate usage (~10,000 submissions/month):
- **Lambda**: ~$0.50/month
- **API Gateway**: ~$0.04/month
- **ECR**: ~$0.05/month
- **CloudWatch**: ~$0.50/month

**Total**: ~$1-2/month

## Troubleshooting

### Code execution fails
1. Check `.env` configuration
2. Test locally: `python scripts/test_executor_local.py`
3. Check Lambda logs in CloudWatch

### Database errors
1. Ensure `DATABSE_DEV` setting matches your database
2. Check database credentials
3. Run migrations if needed

### AWS deployment issues
See [terraform/README.md](terraform/README.md) troubleshooting section.
