|
| 1 | +# SurfSense Backend |
| 2 | + |
| 3 | +Python/FastAPI backend service for SurfSense. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Python 3.12+ |
| 10 | +- PostgreSQL with PGVector extension |
| 11 | +- Redis (for Celery task queue) |
| 12 | +- Docker & Docker Compose (recommended) |
| 13 | + |
| 14 | +### Development Setup |
| 15 | + |
| 16 | +1. **Clone and navigate to backend** |
| 17 | + ```bash |
| 18 | + cd surfsense_backend |
| 19 | + ``` |
| 20 | + |
| 21 | +2. **Install dependencies with UV** |
| 22 | + ```bash |
| 23 | + uv sync |
| 24 | + ``` |
| 25 | + |
| 26 | +3. **Set up environment variables** |
| 27 | + ```bash |
| 28 | + cp .env.example .env |
| 29 | + # Edit .env with your configuration |
| 30 | + ``` |
| 31 | + |
| 32 | +4. **Run database migrations** |
| 33 | + ```bash |
| 34 | + uv run alembic upgrade head |
| 35 | + ``` |
| 36 | + |
| 37 | +5. **Start the development server** |
| 38 | + ```bash |
| 39 | + uv run uvicorn app.app:app --reload |
| 40 | + ``` |
| 41 | + |
| 42 | +## Testing |
| 43 | + |
| 44 | +We use pytest for testing with Docker Compose for dependencies. |
| 45 | + |
| 46 | +### Running Tests |
| 47 | + |
| 48 | +```bash |
| 49 | +# Start dependencies |
| 50 | +docker compose up -d db redis |
| 51 | + |
| 52 | +# Run all tests |
| 53 | +docker compose run --rm -e TESTING=true backend pytest tests/ -v --tb=short |
| 54 | + |
| 55 | +# Run with coverage |
| 56 | +docker compose run --rm -e TESTING=true backend pytest tests/ -v --tb=short --cov=app --cov-report=html |
| 57 | + |
| 58 | +# Run specific test file |
| 59 | +docker compose run --rm -e TESTING=true backend pytest tests/test_celery_tasks.py -v |
| 60 | + |
| 61 | +# Run tests matching a pattern |
| 62 | +docker compose run --rm -e TESTING=true backend pytest tests/ -v -k "test_slack" |
| 63 | + |
| 64 | +# Stop services |
| 65 | +docker compose down -v |
| 66 | +``` |
| 67 | + |
| 68 | +### Test Categories |
| 69 | + |
| 70 | +Tests are organized by markers: |
| 71 | +- `@pytest.mark.unit` - Fast, isolated unit tests |
| 72 | +- `@pytest.mark.integration` - Tests requiring external services |
| 73 | +- `@pytest.mark.slow` - Slow running tests |
| 74 | + |
| 75 | +### Running Locally (without Docker) |
| 76 | + |
| 77 | +```bash |
| 78 | +# Ensure PostgreSQL and Redis are running locally |
| 79 | +export TESTING=true |
| 80 | +uv run pytest tests/ -v --tb=short |
| 81 | +``` |
| 82 | + |
| 83 | +## Project Structure |
| 84 | + |
| 85 | +``` |
| 86 | +surfsense_backend/ |
| 87 | +├── alembic/ # Database migrations |
| 88 | +│ ├── versions/ # Migration files |
| 89 | +│ └── env.py # Alembic configuration |
| 90 | +├── app/ |
| 91 | +│ ├── __init__.py |
| 92 | +│ ├── app.py # FastAPI application |
| 93 | +│ ├── celery_app.py # Celery configuration |
| 94 | +│ ├── db.py # Database models |
| 95 | +│ ├── users.py # User authentication |
| 96 | +│ ├── agents/ # LLM agents (researcher, podcaster) |
| 97 | +│ ├── config/ # Configuration management |
| 98 | +│ ├── connectors/ # External service connectors |
| 99 | +│ ├── prompts/ # LLM prompts |
| 100 | +│ ├── retriever/ # Search and retrieval logic |
| 101 | +│ ├── routes/ # API endpoints |
| 102 | +│ ├── schemas/ # Pydantic models |
| 103 | +│ ├── services/ # Business logic services |
| 104 | +│ ├── tasks/ # Celery tasks |
| 105 | +│ └── utils/ # Utility functions |
| 106 | +├── tests/ # Test suite |
| 107 | +│ ├── conftest.py # Pytest fixtures |
| 108 | +│ ├── test_*.py # Test modules |
| 109 | +│ └── __init__.py |
| 110 | +├── Dockerfile # Container configuration |
| 111 | +├── pyproject.toml # Project dependencies |
| 112 | +├── pytest.ini # Pytest configuration |
| 113 | +└── alembic.ini # Alembic configuration |
| 114 | +``` |
| 115 | + |
| 116 | +## API Documentation |
| 117 | + |
| 118 | +When running the development server, API documentation is available at: |
| 119 | +- Swagger UI: http://localhost:8000/docs |
| 120 | +- ReDoc: http://localhost:8000/redoc |
| 121 | + |
| 122 | +## Configuration |
| 123 | + |
| 124 | +Key environment variables: |
| 125 | + |
| 126 | +| Variable | Description | Default | |
| 127 | +|----------|-------------|---------| |
| 128 | +| `DATABASE_URL` | PostgreSQL connection string | Required | |
| 129 | +| `CELERY_BROKER_URL` | Redis URL for Celery | Required | |
| 130 | +| `SECRET_KEY` | Application secret key | Required | |
| 131 | +| `TESTING` | Enable test mode | `false` | |
| 132 | + |
| 133 | +See `.env.example` for full configuration options. |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +See [CONTRIBUTING.md](../CONTRIBUTING.md) for development guidelines. |
0 commit comments