mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-28 19:56:41 +08:00
Address PR feedback: remove redundant files, format code, add progress tracking
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
This commit is contained in:
co-authored by
krau
parent
127901fd24
commit
20a5e317ae
@@ -1,26 +0,0 @@
|
|||||||
# API Module
|
|
||||||
|
|
||||||
This module provides a RESTful HTTP API for programmatic file downloads from Telegram.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- **RESTful API endpoints** for creating, querying, and canceling download tasks
|
|
||||||
- **Bearer token authentication** for API access control
|
|
||||||
- **IP whitelist** support for additional security
|
|
||||||
- **Webhook callbacks** for task completion notifications
|
|
||||||
- **Task status tracking** (queued, running, completed, failed, canceled)
|
|
||||||
- **Graceful shutdown** with proper cleanup
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
See the full documentation at:
|
|
||||||
- English: `/docs/content/en/usage/api.md`
|
|
||||||
- Chinese: `/docs/content/zh/usage/api.md`
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
- `server.go` - HTTP server initialization and route registration
|
|
||||||
- `handlers.go` - API endpoint handlers and business logic
|
|
||||||
- `middleware.go` - Authentication and logging middleware
|
|
||||||
|
|
||||||
The API integrates with the existing task queue system and uses the bot's Telegram client to fetch messages.
|
|
||||||
+45
-25
@@ -24,10 +24,10 @@ import (
|
|||||||
|
|
||||||
// Request/Response types
|
// Request/Response types
|
||||||
type CreateTaskRequest struct {
|
type CreateTaskRequest struct {
|
||||||
TelegramURL string `json:"telegram_url"`
|
TelegramURL string `json:"telegram_url"`
|
||||||
StorageName string `json:"storage_name,omitempty"`
|
StorageName string `json:"storage_name,omitempty"`
|
||||||
DirPath string `json:"dir_path,omitempty"`
|
DirPath string `json:"dir_path,omitempty"`
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateTaskResponse struct {
|
type CreateTaskResponse struct {
|
||||||
@@ -36,11 +36,14 @@ type CreateTaskResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TaskStatusResponse struct {
|
type TaskStatusResponse struct {
|
||||||
TaskID string `json:"task_id"`
|
TaskID string `json:"task_id"`
|
||||||
Status string `json:"status"` // queued, running, completed, failed, canceled
|
Status string `json:"status"` // queued, running, completed, failed, canceled
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
|
Downloaded int64 `json:"downloaded,omitempty"` // Bytes downloaded
|
||||||
|
Total int64 `json:"total,omitempty"` // Total bytes
|
||||||
|
ProgressPct float64 `json:"progress_pct,omitempty"` // Progress percentage (0-100)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListTasksResponse struct {
|
type ListTasksResponse struct {
|
||||||
@@ -64,11 +67,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type taskStatus struct {
|
type taskStatus struct {
|
||||||
ID string
|
ID string
|
||||||
Status string
|
Status string
|
||||||
Title string
|
Title string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
Error string
|
Error string
|
||||||
|
Downloaded int64
|
||||||
|
Total int64
|
||||||
|
ProgressPct float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleHealth(w http.ResponseWriter, r *http.Request) {
|
func handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -212,11 +218,14 @@ func handleGetTask(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(TaskStatusResponse{
|
json.NewEncoder(w).Encode(TaskStatusResponse{
|
||||||
TaskID: status.ID,
|
TaskID: status.ID,
|
||||||
Status: status.Status,
|
Status: status.Status,
|
||||||
Title: status.Title,
|
Title: status.Title,
|
||||||
CreatedAt: status.CreatedAt,
|
CreatedAt: status.CreatedAt,
|
||||||
Error: status.Error,
|
Error: status.Error,
|
||||||
|
Downloaded: status.Downloaded,
|
||||||
|
Total: status.Total,
|
||||||
|
ProgressPct: status.ProgressPct,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,7 +310,15 @@ func (a *apiProgressTracker) OnStart(ctx context.Context, info tftask.TaskInfo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *apiProgressTracker) OnProgress(ctx context.Context, info tftask.TaskInfo, downloaded int64, total int64) {
|
func (a *apiProgressTracker) OnProgress(ctx context.Context, info tftask.TaskInfo, downloaded int64, total int64) {
|
||||||
// No-op for API tasks
|
taskStatusesMu.Lock()
|
||||||
|
defer taskStatusesMu.Unlock()
|
||||||
|
if ts, exists := taskStatuses[a.taskID]; exists {
|
||||||
|
ts.Downloaded = downloaded
|
||||||
|
ts.Total = total
|
||||||
|
if total > 0 {
|
||||||
|
ts.ProgressPct = float64(downloaded) / float64(total) * 100.0
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *apiProgressTracker) OnDone(ctx context.Context, info tftask.TaskInfo, err error) {
|
func (a *apiProgressTracker) OnDone(ctx context.Context, info tftask.TaskInfo, err error) {
|
||||||
@@ -332,11 +349,14 @@ func sendWebhook(taskID, status, errorMsg string) {
|
|||||||
logger := log.WithPrefix("webhook")
|
logger := log.WithPrefix("webhook")
|
||||||
|
|
||||||
payload := TaskStatusResponse{
|
payload := TaskStatusResponse{
|
||||||
TaskID: ts.ID,
|
TaskID: ts.ID,
|
||||||
Status: status,
|
Status: status,
|
||||||
Title: ts.Title,
|
Title: ts.Title,
|
||||||
CreatedAt: ts.CreatedAt,
|
CreatedAt: ts.CreatedAt,
|
||||||
Error: errorMsg,
|
Error: errorMsg,
|
||||||
|
Downloaded: ts.Downloaded,
|
||||||
|
Total: ts.Total,
|
||||||
|
ProgressPct: ts.ProgressPct,
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := json.Marshal(payload)
|
body, err := json.Marshal(payload)
|
||||||
|
|||||||
@@ -110,11 +110,11 @@ func TestAuthMiddleware_HealthCheck(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetClientIP(t *testing.T) {
|
func TestGetClientIP(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
remoteAddr string
|
remoteAddr string
|
||||||
xForwardedFor string
|
xForwardedFor string
|
||||||
xRealIP string
|
xRealIP string
|
||||||
expectedIP string
|
expectedIP string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "RemoteAddr only",
|
name: "RemoteAddr only",
|
||||||
|
|||||||
-105
@@ -1,105 +0,0 @@
|
|||||||
#!/bin/bash -euo pipefail
|
|
||||||
|
|
||||||
# API Test Script for SaveAny-Bot HTTP API
|
|
||||||
|
|
||||||
API_URL="http://localhost:8080"
|
|
||||||
TOKEN="test-token-12345"
|
|
||||||
HEADERS=(-H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json")
|
|
||||||
|
|
||||||
echo "=== Testing SaveAny-Bot HTTP API ==="
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 1: Health Check (no auth required)
|
|
||||||
echo "1. Testing health check endpoint..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "${API_URL}/health")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "200" ]; then
|
|
||||||
echo "✓ Health check passed"
|
|
||||||
echo " Response: $BODY"
|
|
||||||
else
|
|
||||||
echo "✗ Health check failed (HTTP $HTTP_STATUS)"
|
|
||||||
echo " Response: $BODY"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 2: Unauthorized request (missing token)
|
|
||||||
echo "2. Testing unauthorized request..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "${API_URL}/api/v1/tasks")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "401" ]; then
|
|
||||||
echo "✓ Correctly rejected unauthorized request"
|
|
||||||
else
|
|
||||||
echo "✗ Unexpected response (HTTP $HTTP_STATUS)"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 3: Invalid token
|
|
||||||
echo "3. Testing invalid token..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
|
|
||||||
-H "Authorization: Bearer wrong-token" \
|
|
||||||
"${API_URL}/api/v1/tasks")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "401" ]; then
|
|
||||||
echo "✓ Correctly rejected invalid token"
|
|
||||||
else
|
|
||||||
echo "✗ Unexpected response (HTTP $HTTP_STATUS)"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 4: List tasks (authorized)
|
|
||||||
echo "4. Testing list tasks endpoint..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
|
|
||||||
"${HEADERS[@]}" \
|
|
||||||
"${API_URL}/api/v1/tasks")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "200" ]; then
|
|
||||||
echo "✓ List tasks successful"
|
|
||||||
echo " Response: $BODY"
|
|
||||||
else
|
|
||||||
echo "✗ List tasks failed (HTTP $HTTP_STATUS)"
|
|
||||||
echo " Response: $BODY"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 5: Create task with missing parameters
|
|
||||||
echo "5. Testing create task with missing parameters..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
|
|
||||||
-X POST \
|
|
||||||
"${HEADERS[@]}" \
|
|
||||||
-d '{"telegram_url":""}' \
|
|
||||||
"${API_URL}/api/v1/tasks")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "400" ]; then
|
|
||||||
echo "✓ Correctly rejected invalid request"
|
|
||||||
else
|
|
||||||
echo "✗ Unexpected response (HTTP $HTTP_STATUS)"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Test 6: Get non-existent task
|
|
||||||
echo "6. Testing get non-existent task..."
|
|
||||||
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
|
|
||||||
"${HEADERS[@]}" \
|
|
||||||
"${API_URL}/api/v1/tasks/nonexistent")
|
|
||||||
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
|
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" = "404" ]; then
|
|
||||||
echo "✓ Correctly returned 404 for non-existent task"
|
|
||||||
else
|
|
||||||
echo "✗ Unexpected response (HTTP $HTTP_STATUS)"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
echo "=== API Tests Complete ==="
|
|
||||||
echo
|
|
||||||
echo "Note: Full integration testing requires:"
|
|
||||||
echo " - Valid Telegram bot token in config"
|
|
||||||
echo " - Bot running and connected to Telegram"
|
|
||||||
echo " - Valid Telegram message URL to test downloads"
|
|
||||||
Reference in New Issue
Block a user