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:
copilot-swe-agent[bot]
2026-01-19 05:27:12 +00:00
co-authored by krau
parent 127901fd24
commit 20a5e317ae
6 changed files with 63 additions and 174 deletions
-26
View File
@@ -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
View File
@@ -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)
+3 -3
View File
@@ -54,12 +54,12 @@ func loggingMiddleware(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
// Wrap response writer to capture status code // Wrap response writer to capture status code
wrapper := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} wrapper := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(wrapper, r) next.ServeHTTP(wrapper, r)
logger.Infof("%s %s %d %s", r.Method, r.URL.Path, wrapper.statusCode, time.Since(start)) logger.Infof("%s %s %d %s", r.Method, r.URL.Path, wrapper.statusCode, time.Since(start))
}) })
} }
+6 -6
View File
@@ -61,7 +61,7 @@ func TestIsIPAllowed(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := isIPAllowed(tt.clientIP, tt.allowedIPs) result := isIPAllowed(tt.clientIP, tt.allowedIPs)
if result != tt.expected { if result != tt.expected {
t.Errorf("isIPAllowed(%q, %v) = %v, want %v", t.Errorf("isIPAllowed(%q, %v) = %v, want %v",
tt.clientIP, tt.allowedIPs, result, tt.expected) tt.clientIP, tt.allowedIPs, result, tt.expected)
} }
}) })
@@ -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",
+9 -9
View File
@@ -20,15 +20,15 @@ func Init(ctx context.Context) error {
} }
logger := log.FromContext(ctx).WithPrefix("api") logger := log.FromContext(ctx).WithPrefix("api")
mux := http.NewServeMux() mux := http.NewServeMux()
// Register API routes // Register API routes
registerRoutes(mux) registerRoutes(mux)
// Wrap with middleware // Wrap with middleware
handler := loggingMiddleware(logger)(authMiddleware(mux)) handler := loggingMiddleware(logger)(authMiddleware(mux))
server = &http.Server{ server = &http.Server{
Addr: fmt.Sprintf(":%d", cfg.API.Port), Addr: fmt.Sprintf(":%d", cfg.API.Port),
Handler: handler, Handler: handler,
@@ -36,34 +36,34 @@ func Init(ctx context.Context) error {
WriteTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second, IdleTimeout: 60 * time.Second,
} }
go func() { go func() {
logger.Infof("Starting API server on port %d", cfg.API.Port) logger.Infof("Starting API server on port %d", cfg.API.Port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Errorf("API server error: %v", err) logger.Errorf("API server error: %v", err)
} }
}() }()
// Graceful shutdown on context cancellation // Graceful shutdown on context cancellation
go func() { go func() {
<-ctx.Done() <-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil { if err := server.Shutdown(shutdownCtx); err != nil {
logger.Errorf("Failed to shutdown API server: %v", err) logger.Errorf("Failed to shutdown API server: %v", err)
} else { } else {
logger.Info("API server stopped") logger.Info("API server stopped")
} }
}() }()
return nil return nil
} }
func registerRoutes(mux *http.ServeMux) { func registerRoutes(mux *http.ServeMux) {
// Health check endpoint (no auth required) // Health check endpoint (no auth required)
mux.HandleFunc("/health", handleHealth) mux.HandleFunc("/health", handleHealth)
// API v1 endpoints // API v1 endpoints
mux.HandleFunc("POST /api/v1/tasks", handleCreateTask) mux.HandleFunc("POST /api/v1/tasks", handleCreateTask)
mux.HandleFunc("GET /api/v1/tasks/{id}", handleGetTask) mux.HandleFunc("GET /api/v1/tasks/{id}", handleGetTask)
-105
View File
@@ -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"