Skip to content

Commit fdf10a5

Browse files
authored
Merge pull request #487 from JnyJny/feature/fastapi-best-practices
FastAPI Best Practices Implementation and Logging Integration Fix
2 parents 420703f + 8f6c7b2 commit fdf10a5

29 files changed

+2914
-527
lines changed

docs/api/endpoints.md

Lines changed: 239 additions & 204 deletions
Large diffs are not rendered by default.

docs/api/index.md

Lines changed: 117 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Web API Overview
22

33
The BusyLight HTTP API provides RESTful endpoints for controlling USB LED
4-
lights. The API returns JSON responses and supports the same functionality
5-
as the command-line interface.
4+
lights. Built with FastAPI, it features proper HTTP methods, domain-based
5+
organization, API versioning, and comprehensive OpenAPI documentation.
66

77
## Quick Start
88

@@ -25,82 +25,107 @@ busyserve -D
2525
### Basic Usage
2626

2727
```bash
28-
# Get server status
28+
# Get API information
2929
curl http://localhost:8000/
3030

31-
# Turn light on (green)
32-
curl http://localhost:8000/light/0/on
31+
# Check system health
32+
curl http://localhost:8000/system/health
3333

34-
# Turn light red
35-
curl "http://localhost:8000/light/0/on?color=red"
34+
# Turn light on (green) - REST endpoint
35+
curl -X POST http://localhost:8000/api/v1/lights/0/on \
36+
-H "Content-Type: application/json" \
37+
-d '{"color": "green", "dim": 1.0, "led": 0}'
3638

37-
# Blink light blue 3 times
38-
curl "http://localhost:8000/light/0/blink?color=blue&count=3"
39+
# Turn light red - backwards compatible
40+
curl "http://localhost:8000/light/0/on?color=red"
3941

40-
# Turn light off
41-
curl http://localhost:8000/light/0/off
42+
# Start rainbow effect
43+
curl -X POST http://localhost:8000/api/v1/effects/rainbow \
44+
-H "Content-Type: application/json" \
45+
-d '{"dim": 0.8, "speed": "fast"}'
4246
```
4347

4448
## API Features
4549

46-
- **RESTful design** with consistent JSON responses
50+
- **Domain-based organization** - lights, effects, system endpoints
51+
- **API versioning** - v1 endpoints with backwards compatibility
52+
- **Proper HTTP methods** - POST for actions, GET for queries
53+
- **JSON request/response** with Pydantic validation
4754
- **Device targeting** by index or all devices
4855
- **LED targeting** for multi-LED devices
49-
- **Color specification** via query parameters
50-
- **Effect control** with customizable parameters
5156
- **Authentication** via HTTP Basic Auth (optional)
5257
- **CORS support** for browser-based applications
5358
- **Interactive documentation** at `/docs` and `/redoc`
59+
- **OpenAPI 3.0** specification for code generation
60+
61+
## API Endpoints
62+
63+
The API provides multiple access patterns:
5464

55-
## Base URL
65+
### Versioned API (Recommended)
66+
```
67+
http://localhost:8000/api/v1/
68+
```
5669

70+
### Backwards Compatible
5771
```
58-
http://localhost:8000
72+
http://localhost:8000/
5973
```
6074

6175
Default host is `localhost` and port is `8000`. Configure with command-line
6276
options:
6377

6478
```bash
65-
busyserve --host 0.0.0.0 --port 8080
79+
busyserve --host 0.0.0.0 --port 8080 --debug # Enable debug logging
6680
```
6781

6882
## Response Format
6983

7084
All endpoints return JSON responses with consistent structure:
7185

72-
### Success Response
86+
### Light Operation Response
7387

7488
```json
7589
{
76-
"action": "on",
77-
"light_id": 0,
78-
"color": "red",
79-
"rgb": [255, 0, 0],
80-
"led": 0
90+
"success": true,
91+
"action": "turned on",
92+
"lights_affected": 1,
93+
"details": [
94+
{
95+
"light_id": 0,
96+
"action": "turned on",
97+
"color": "red",
98+
"rgb": [255, 0, 0],
99+
"dim": 1.0,
100+
"led": 0
101+
}
102+
]
81103
}
82104
```
83105

84106
### Error Response
85107

86108
```json
87109
{
88-
"message": "Light index 5 not found"
110+
"detail": "Light index 5 not found"
89111
}
90112
```
91113

92114
## Content Types
93115

94-
- **Request**: Query parameters or JSON body
95-
- **Response**: `application/json`
116+
- **V1 Endpoints**: JSON request/response (`application/json`)
117+
- **Compatibility Endpoints**: Query parameters with JSON response
118+
- **All Responses**: `application/json`
96119

97120
## Status Codes
98121

99122
| Code | Description |
100123
|------|-------------|
101124
| 200 | Success |
125+
| 401 | Authentication required |
102126
| 404 | Device not found |
103-
| 422 | Invalid parameter |
127+
| 422 | Validation error |
128+
| 503 | No lights available |
104129
| 500 | Server error |
105130

106131
## Authentication
@@ -121,8 +146,13 @@ busyserve
121146
### Usage
122147

123148
```bash
124-
# Authenticated request
125-
curl -u admin:secret http://localhost:8000/light/0/on
149+
# V1 authenticated request
150+
curl -u admin:secret -X POST http://localhost:8000/api/v1/lights/0/on \
151+
-H "Content-Type: application/json" \
152+
-d '{"color": "red"}'
153+
154+
# Compatibility authenticated request
155+
curl -u admin:secret http://localhost:8000/light/0/on?color=red
126156
```
127157

128158
!!! warning "Security Warning"
@@ -165,20 +195,70 @@ These interfaces allow testing endpoints directly from the browser.
165195
The API does not impose rate limiting, but devices may have physical
166196
limitations on how quickly they can change colors or effects.
167197

168-
## Health Check
198+
## System Endpoints
169199

170200
```bash
171-
# Check server status
201+
# API information
172202
curl http://localhost:8000/
173203

174-
# Response
175-
[
176-
{"path": "/"},
177-
{"path": "/lights"},
178-
{"path": "/light/{light_id}"}
179-
]
204+
# System health check
205+
curl http://localhost:8000/system/health
206+
207+
# System information
208+
curl http://localhost:8000/system/info
180209
```
181210

211+
### Health Response
212+
```json
213+
{
214+
"status": "healthy",
215+
"lights_available": 2,
216+
"timestamp": "2023-01-01T12:00:00Z"
217+
}
218+
```
219+
220+
## Backwards Compatibility
221+
222+
The API maintains full backwards compatibility with existing integrations:
223+
224+
- **Original endpoints** work unchanged (GET requests with query parameters)
225+
- **Original response format** preserved for compatibility endpoints
226+
- **Gradual migration** - use v1 endpoints for new features, keep existing code working
227+
- **Deprecation warnings** in OpenAPI docs for legacy endpoints
228+
229+
### Migration Path
230+
231+
1. **Keep existing code working** - no immediate changes needed
232+
2. **New features** - use `/api/v1/` endpoints with JSON payloads
233+
3. **Gradual adoption** - migrate endpoints one at a time
234+
4. **Full migration** - eventually adopt v1 endpoints for all operations
235+
236+
## Logging
237+
238+
The API uses integrated logging that properly displays both FastAPI and uvicorn events:
239+
240+
```bash
241+
# Normal logging
242+
busyserve
243+
244+
# Debug logging with detailed output
245+
busyserve --debug
246+
```
247+
248+
**Features:**
249+
- **Integrated logging** - All API, uvicorn, and application events appear in a unified log
250+
- **Color-coded output** - Different log levels are visually distinct
251+
- **Request tracing** - HTTP requests and responses are logged when debug is enabled
252+
- **Structured format** - Timestamps, levels, module names, and line numbers included
253+
254+
**Log Levels:**
255+
- **INFO**: Server startup, configuration, API operations
256+
- **DEBUG**: Detailed request/response data, internal operations
257+
- **WARNING**: Configuration issues, recoverable errors
258+
- **ERROR**: Request failures, server errors
259+
260+
**Previous Issue Resolved:** Earlier versions had logging compatibility issues between loguru and uvicorn - this has been fixed with integrated logging handlers.
261+
182262
## Error Handling
183263

184264
The API provides detailed error messages for troubleshooting:

0 commit comments

Comments
 (0)