Skip to content

Commit 3eb3b6e

Browse files
committed
feat(api): implement task management API with handlers for creating, listing, retrieving, and canceling tasks
- Added Handlers struct and methods for task operations - Implemented task progress tracking and storage - Created server setup with middleware for logging and recovery - Added support for Telegram file extraction and Telegraph image extraction - Introduced webhook functionality for task status updates - Defined request and response types for API interactions
1 parent f377ee3 commit 3eb3b6e

File tree

13 files changed

+1612
-1
lines changed

13 files changed

+1612
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ temp/
1111
playwright/
1212
testplugins/
1313
*.exe
14-
tmp-*
14+
tmp-*
15+
saveany-bot

api/auth.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"crypto/subtle"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/krau/SaveAny-Bot/config"
10+
)
11+
12+
// tokenContextKey 用于在 context 中存储 token
13+
type tokenContextKey struct{}
14+
15+
// AuthMiddleware 返回认证中间件
16+
func AuthMiddleware() func(http.Handler) http.Handler {
17+
return func(next http.Handler) http.Handler {
18+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19+
cfg := config.C().API
20+
21+
// 从请求头获取 token
22+
authHeader := r.Header.Get("Authorization")
23+
if authHeader == "" {
24+
WriteError(w, http.StatusUnauthorized, "unauthorized", "missing authorization header")
25+
return
26+
}
27+
28+
// 提取 Bearer token
29+
parts := strings.SplitN(authHeader, " ", 2)
30+
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
31+
WriteError(w, http.StatusUnauthorized, "unauthorized", "invalid authorization header format")
32+
return
33+
}
34+
35+
token := parts[1]
36+
37+
// 验证 token
38+
if subtle.ConstantTimeCompare([]byte(token), []byte(cfg.Token)) != 1 {
39+
WriteError(w, http.StatusUnauthorized, "unauthorized", "invalid token")
40+
return
41+
}
42+
43+
// 将 token 添加到 context
44+
ctx := context.WithValue(r.Context(), tokenContextKey{}, token)
45+
next.ServeHTTP(w, r.WithContext(ctx))
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)