API Endpoints¶
Swagger UI¶
FastAPI tự động generate API documentation. Truy cập:
- Local: http://localhost:8000/docs
- Production: https://api.chienle.dev/docs
Try it out
Swagger UI cho phép test trực tiếp các endpoint ngay trên trình duyệt.
Endpoints Reference¶
Root¶
| Method | Path | Mô tả |
|---|---|---|
GET | / | Health check — trả {"message": "Welcome to Course API"} |
Courses¶
| Method | Path | Mô tả | Request Body |
|---|---|---|---|
POST | /courses | Tạo khóa học mới | CourseCreate |
GET | /courses | Danh sách khóa học (có pagination) | — |
POST | /courses/{id}/upload-url | Lấy presigned URL để upload file | UploadUrlRequest |
POST | /courses/{id}/finalize-upload | Xác nhận upload hoàn tất | FinalizeUploadRequest |
Chi tiết từng Endpoint¶
POST /courses — Tạo khóa học¶
Request:
Response (201):
{
"id": 1,
"title": "Khóa học Python cơ bản",
"slug": "khoa-hoc-python-co-ban",
"content_html": "<p>Nội dung khóa học...</p>",
"thumbnail_key": null,
"thumbnail_url": null
}
GET /courses — Danh sách khóa học¶
Query Parameters:
| Param | Type | Default | Mô tả |
|---|---|---|---|
cursor | int | null | ID bắt đầu (cursor-based pagination) |
limit | int | 10 (max 50) | Số lượng kết quả |
Response:
[
{
"id": 5,
"title": "Khóa học React",
"slug": "khoa-hoc-react",
"thumbnail_url": "https://cdn.chienle.dev/courses/5/abc.jpg"
}
]
POST /courses/{id}/upload-url — Lấy Presigned URL¶
Request:
Response:
{
"upload_url": "https://r2.cloudflarestorage.com/...(presigned)...",
"file_key": "courses/1/uuid-abc.png"
}
POST /courses/{id}/finalize-upload — Xác nhận Upload¶
Request:
Response: CourseResponse với thumbnail_url đã cập nhật.
Upload Flow¶
sequenceDiagram
participant FE as Frontend
participant BE as Backend (FastAPI)
participant R2 as Cloudflare R2
FE->>BE: POST /courses/{id}/upload-url
BE->>BE: Generate presigned URL (boto3)
BE-->>FE: { upload_url, file_key }
FE->>R2: PUT upload_url (binary file)
R2-->>FE: 200 OK
FE->>BE: POST /courses/{id}/finalize-upload
BE->>BE: Update course.thumbnail_key
BE-->>FE: Updated CourseResponse Direct Upload
File được upload trực tiếp từ browser lên R2, không đi qua backend. Giảm tải cho server và tăng tốc upload.