|
1 | 1 | # Byte API Service |
2 | 2 |
|
3 | | -REST API service for the Byte Bot application. |
| 3 | +> **Version**: 0.2.0 **Python**: 3.12+ **Framework**: Litestar 2.4.3+ |
4 | 4 |
|
5 | 5 | ## Overview |
6 | 6 |
|
7 | | -This service provides the web API and dashboard functionality including: |
| 7 | +The **Byte API Service** is the REST API microservice for Byte Bot, providing HTTP endpoints for guild management, |
| 8 | +GitHub integration, and system operations. This service replaces the monolithic web service and operates independently |
| 9 | +of the Discord bot. |
8 | 10 |
|
9 | | -- Litestar REST API |
10 | | -- Web dashboard with Jinja2 templates |
11 | | -- TailwindCSS + DaisyUI frontend |
12 | | -- Database management and migrations |
13 | | -- Authentication and authorization |
| 11 | +## Purpose |
14 | 12 |
|
15 | | -## Technology Stack |
| 13 | +- **REST API Endpoints**: Guild CRUD, GitHub config, forum settings |
| 14 | +- **Database Operations**: Primary data store interface via Advanced Alchemy ORM |
| 15 | +- **OpenAPI Documentation**: Swagger/ReDoc UI for API exploration |
| 16 | +- **Health Monitoring**: System health checks and status endpoints |
| 17 | +- **Web Dashboard**: Server-side rendered dashboard (Jinja2 templates) |
16 | 18 |
|
17 | | -- Python 3.12+ |
18 | | -- Litestar framework |
19 | | -- PostgreSQL (via Advanced Alchemy) |
20 | | -- Jinja2 templates |
21 | | -- TailwindCSS + DaisyUI |
22 | | -- Shared code from `byte-common` package |
| 19 | +## Architecture |
23 | 20 |
|
24 | | -## Status |
| 21 | +The API service is built on: |
25 | 22 |
|
26 | | -**Phase 0.1**: Directory structure created. Implementation pending. |
| 23 | +- **Litestar**: High-performance ASGI framework |
| 24 | +- **Advanced Alchemy**: Repository pattern over SQLAlchemy 2.0 |
| 25 | +- **PostgreSQL**: Primary database (async via asyncpg) |
| 26 | +- **Pydantic**: Request/response validation and settings management |
27 | 27 |
|
28 | | -## Future Structure |
| 28 | +## Dependencies |
29 | 29 |
|
| 30 | +### Core Dependencies |
| 31 | + |
| 32 | +- `byte-common`: Shared models, utilities, and database schemas |
| 33 | +- `litestar[full]>=2.4.3`: Web framework with full plugin support |
| 34 | +- `advanced-alchemy>=0.6.1`: ORM and repository pattern |
| 35 | +- `asyncpg>=0.29.0`: Async PostgreSQL driver |
| 36 | +- `alembic>=1.13.0`: Database migrations |
| 37 | + |
| 38 | +### Integration Dependencies |
| 39 | + |
| 40 | +- `githubkit[auth-app]`: GitHub API client |
| 41 | +- `httpx`: Async HTTP client |
| 42 | +- `pydantic>=2.5.2`: Data validation |
| 43 | +- `pydantic-settings>=2.1.0`: Configuration management |
| 44 | +- `PyJWT>=2.8.0`: JWT token handling |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +```bash |
| 49 | +# From repository root |
| 50 | +cd services/api |
| 51 | + |
| 52 | +# Install dependencies with uv |
| 53 | +uv sync |
| 54 | + |
| 55 | +# Or install as editable package |
| 56 | +uv pip install -e . |
30 | 57 | ``` |
31 | | -services/api/ |
32 | | -├── byte_api/ # API source code |
33 | | -│ ├── domain/ # Domain models |
34 | | -│ ├── lib/ # API utilities |
35 | | -│ ├── templates/ # Jinja2 templates |
36 | | -│ └── static/ # Static assets |
37 | | -├── tests/ # API-specific tests |
38 | | -├── pyproject.toml # API service configuration |
39 | | -└── Dockerfile # API service container |
| 58 | + |
| 59 | +## Configuration |
| 60 | + |
| 61 | +Set the following environment variables (or use `.env` file): |
| 62 | + |
| 63 | +```bash |
| 64 | +# Database |
| 65 | +DB_URL=postgresql+asyncpg://user:pass@localhost:5432/byte |
| 66 | +DB_POOL_DISABLE=True # Use NullPool in production |
| 67 | + |
| 68 | +# Server |
| 69 | +SERVER_HOST=0.0.0.0 |
| 70 | +SERVER_PORT=8000 |
| 71 | +ENVIRONMENT=dev # dev, test, or prod |
| 72 | +SECRET_KEY=your-secret-key |
| 73 | + |
| 74 | +# GitHub Integration |
| 75 | +GITHUB_APP_ID=your-app-id |
| 76 | +GITHUB_APP_PRIVATE_KEY=your-private-key |
| 77 | +GITHUB_APP_CLIENT_ID=your-client-id |
| 78 | +GITHUB_APP_CLIENT_SECRET=your-client-secret |
| 79 | + |
| 80 | +# Logging |
| 81 | +LOG_LEVEL=INFO |
| 82 | +DEBUG=False |
| 83 | +``` |
| 84 | + |
| 85 | +## Running the Service |
| 86 | + |
| 87 | +```bash |
| 88 | +# Development mode with auto-reload |
| 89 | +uv run python -m byte_api --reload --debug |
| 90 | + |
| 91 | +# Production mode |
| 92 | +uv run python -m byte_api --http-workers 4 |
| 93 | + |
| 94 | +# With custom host/port |
| 95 | +uv run python -m byte_api --host 0.0.0.0 --port 8080 |
40 | 96 | ``` |
41 | 97 |
|
42 | 98 | ## Development |
43 | 99 |
|
44 | | -To be populated during Phase 0.3 (API Service Migration). |
| 100 | +### Database Migrations |
| 101 | + |
| 102 | +```bash |
| 103 | +# Apply migrations |
| 104 | +uv run alembic upgrade head |
| 105 | + |
| 106 | +# Create new migration |
| 107 | +uv run alembic revision --autogenerate -m "description" |
| 108 | + |
| 109 | +# Rollback |
| 110 | +uv run alembic downgrade -1 |
| 111 | +``` |
| 112 | + |
| 113 | +### Testing |
| 114 | + |
| 115 | +```bash |
| 116 | +# Run tests |
| 117 | +uv run pytest |
| 118 | + |
| 119 | +# With coverage |
| 120 | +uv run pytest --cov=byte_api --cov-report=html |
| 121 | +``` |
| 122 | + |
| 123 | +### Code Quality |
| 124 | + |
| 125 | +```bash |
| 126 | +# Linting |
| 127 | +uv run ruff check . |
| 128 | + |
| 129 | +# Formatting |
| 130 | +uv run ruff format . |
| 131 | + |
| 132 | +# Type checking |
| 133 | +uv run ty check byte_api |
| 134 | +``` |
| 135 | + |
| 136 | +## API Endpoints |
| 137 | + |
| 138 | +### Guild Management |
| 139 | + |
| 140 | +- `GET /api/guilds` - List all guilds (paginated) |
| 141 | +- `POST /api/guilds` - Create guild |
| 142 | +- `GET /api/guilds/{guild_id}` - Get guild details |
| 143 | +- `PATCH /api/guilds/{guild_id}` - Update guild |
| 144 | +- `DELETE /api/guilds/{guild_id}` - Delete guild |
| 145 | + |
| 146 | +### Configuration |
| 147 | + |
| 148 | +- `GET /api/guilds/{guild_id}/github` - GitHub config |
| 149 | +- `GET /api/guilds/{guild_id}/sotags` - Stack Overflow tags |
| 150 | +- `GET /api/guilds/{guild_id}/allowed-users` - Allowed users |
| 151 | +- `GET /api/guilds/{guild_id}/forum` - Forum config |
| 152 | + |
| 153 | +### System |
| 154 | + |
| 155 | +- `GET /health` - Health check |
| 156 | +- `GET /api/system/info` - System information |
| 157 | + |
| 158 | +### Documentation |
| 159 | + |
| 160 | +- `GET /api/swagger` - Swagger UI |
| 161 | +- `GET /api/redoc` - ReDoc UI |
| 162 | +- `GET /api/schema/openapi.json` - OpenAPI schema |
| 163 | + |
| 164 | +## Project Structure |
| 165 | + |
| 166 | +``` |
| 167 | +services/api/ |
| 168 | +├── src/ |
| 169 | +│ └── byte_api/ |
| 170 | +│ ├── __init__.py |
| 171 | +│ ├── __main__.py # Entry point |
| 172 | +│ ├── app.py # Litestar app factory |
| 173 | +│ ├── lib/ # Infrastructure |
| 174 | +│ │ ├── settings.py |
| 175 | +│ │ ├── cors.py |
| 176 | +│ │ ├── openapi.py |
| 177 | +│ │ ├── db/ |
| 178 | +│ │ └── log/ |
| 179 | +│ └── domain/ # Business logic |
| 180 | +│ ├── guilds/ |
| 181 | +│ ├── github/ |
| 182 | +│ ├── system/ |
| 183 | +│ └── web/ |
| 184 | +├── tests/ |
| 185 | +├── pyproject.toml |
| 186 | +└── README.md |
| 187 | +``` |
| 188 | + |
| 189 | +## Migration Notes |
| 190 | + |
| 191 | +This service is part of Phase 1 (API Layer Extraction) of the microservices migration. It consolidates functionality |
| 192 | +from: |
| 193 | + |
| 194 | +- `byte_bot/app.py` - Litestar app factory |
| 195 | +- `byte_bot/server/` - Web service logic |
| 196 | +- `byte_bot/server/domain/` - Business domains |
| 197 | + |
| 198 | +The Discord bot will communicate with this service via HTTP rather than direct database access. |
| 199 | + |
| 200 | +## Deployment |
| 201 | + |
| 202 | +### Docker |
| 203 | + |
| 204 | +```bash |
| 205 | +# Build image |
| 206 | +docker build -t byte-api:latest . |
| 207 | + |
| 208 | +# Run container |
| 209 | +docker run -p 8000:8000 --env-file .env byte-api:latest |
| 210 | +``` |
| 211 | + |
| 212 | +### Railway |
| 213 | + |
| 214 | +Deploy as a standalone service with PostgreSQL database plugin attached. |
| 215 | + |
| 216 | +## Troubleshooting |
| 217 | + |
| 218 | +### Database Connection Issues |
| 219 | + |
| 220 | +- Verify `DB_URL` format: `postgresql+asyncpg://user:pass@host:port/db` |
| 221 | +- Ensure PostgreSQL is running and accessible |
| 222 | +- Check firewall/network settings |
| 223 | + |
| 224 | +### Migration Errors |
| 225 | + |
| 226 | +- Ensure database is up to date: `uv run alembic upgrade head` |
| 227 | +- Check migration scripts in `byte_common` package |
| 228 | +- Verify no other processes are holding locks |
| 229 | + |
| 230 | +### Import Errors |
| 231 | + |
| 232 | +- Install dependencies: `uv sync` |
| 233 | +- Verify `byte-common` is installed in same environment |
| 234 | +- Check Python version: `python --version` (should be 3.12+) |
| 235 | + |
| 236 | +## Additional Resources |
| 237 | + |
| 238 | +- [Litestar Documentation](https://docs.litestar.dev/) |
| 239 | +- [Advanced Alchemy Documentation](https://docs.advanced-alchemy.litestar.dev/) |
| 240 | +- [Main Repository](https://github.com/JacobCoffee/byte) |
| 241 | +- [Migration Plan](../../PLAN.md) |
| 242 | + |
| 243 | +## License |
| 244 | + |
| 245 | +MIT License - See LICENSE file in repository root. |
| 246 | + |
| 247 | +## Maintainer |
| 248 | + |
| 249 | +Jacob Coffee ( @JacobCoffee) - [email protected] |
0 commit comments