Spaces:
Runtime error
Runtime error
Development Guide
Complete guide for setting up and developing the Smart LINE Bot + Dashboard + Scraper Pipeline.
Prerequisites
- Python 3.11+
- PostgreSQL 15+
- Redis 7+
- Docker & Docker Compose (optional)
- LINE Developer Account
- OpenAI Account
Local Development Setup
1. Clone Repository
git clone https://github.com/Smiledangers/smart-line-automation-suite.git
cd smart-line-automation-suite
2. Create Virtual Environment
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python -m venv venv
source venv/bin/activate
3. Install Dependencies
# Install all dependencies
pip install -r requirements/base.txt
pip install -r requirements/dev.txt
# Or use make
make install
make install-dev
4. Environment Setup
# Copy environment template
cp .env.example .env
# Edit .env with your settings
# Required:
# - LINE_CHANNEL_ACCESS_TOKEN
# - LINE_CHANNEL_SECRET
# - OPENAI_API_KEY
# - SECRET_KEY
# - DATABASE_URL
# - REDIS_URL
5. Database Setup
# Initialize database
python scripts/init_db.py
# Run migrations
alembic upgrade head
# Create superuser
python scripts/create_superuser.py
# Seed test data (optional)
python scripts/seed_data.py --all
6. Run Development Server
# Start Redis (if not using Docker)
redis-server
# Start PostgreSQL (if not using Docker)
# Or use Docker:
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:15
# Start FastAPI server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Start Celery worker (in separate terminal)
celery -A app.tasks.celery_app worker --loglevel=info
# Start Celery beat (in separate terminal)
celery -A app.tasks.celery_app beat --loglevel=info
7. Access Application
- API Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health: http://localhost:8000/health
Docker Development
Quick Start
# Start all services
docker compose up -d --build
# View logs
docker compose logs -f
# Stop services
docker compose down
Development Commands
# Rebuild containers
docker compose build --no-cache
# Run migrations
docker compose exec web alembic upgrade head
# Create superuser
docker compose exec web python scripts/create_superuser.py
# Access shell
docker compose exec web bash
# Run tests
docker compose exec web pytest
Project Structure
app/
βββ api/
β βββ v1/
β βββ endpoints/ # API route handlers
βββ core/ # Core configuration
βββ models/ # SQLAlchemy models
βββ schemas/ # Pydantic schemas
βββ services/ # Business logic
βββ tasks/ # Celery tasks
βββ utils/ # Utilities
tests/
βββ unit/ # Unit tests
βββ integration/ # Integration tests
βββ conftest.py # Pytest fixtures
deploy/
βββ k8s/ # Kubernetes manifests
βββ helm/ # Helm charts
βββ terraform/ # Terraform configs
βββ cloud/ # Cloud deployment configs
Code Style
Formatting
# Format code with Black
black app/ tests/
# Sort imports with isort
isort app/ tests/
Type Checking
# Run mypy
mypy app/
Linting
# Run flake8
flake8 app/ tests/
Testing
Run All Tests
pytest
Run Specific Tests
# Unit tests
pytest tests/unit/
# Integration tests
pytest tests/integration/
# With coverage
pytest --cov=app --cov-report=term-missing
Test Fixtures
See tests/conftest.py for available fixtures:
client- FastAPI test clientdb_session- Database sessiontest_user- Test userauth_headers- Auth headerssample_line_message- LINE webhook samplesample_scraping_job- Scraping job sample
Common Issues
Database Connection Error
# Check PostgreSQL is running
pg_isready
# Check connection string in .env
Redis Connection Error
# Check Redis is running
redis-cli ping
# Check REDIS_URL in .env
LINE Webhook Not Working
# Verify credentials
python -c "from app.core.config import settings; print(settings.LINE_CHANNEL_ACCESS_TOKEN)"
# Use ngrok for local development
ngrok http 8000
# Update LINE webhook URL to ngrok URL
Import Errors
# Reinstall dependencies
pip install -r requirements/base.txt
# Check PYTHONPATH
echo $PYTHONPATH
Development Tips
- Use Docker for consistent environment
- Use
makecommands for common tasks (see Makefile) - Write tests before implementing features
- Use type hints for better code documentation
- Keep secrets in
.env, never commit them - Use
pre-commithooks for code quality
Contributing
- Create feature branch
- Write tests first
- Implement feature
- Run code formatters
- Run full test suite
- Push and create PR