-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
69 lines (58 loc) · 1.73 KB
/
Copy pathapi.go
File metadata and controls
69 lines (58 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// api.go
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/gorilla/mux"
)
// APIHandler 封装了API的依赖,如工作池
type APIHandler struct {
Pool *WorkerPool
}
// CreateTaskHandler 处理创建任务的请求
func (h *APIHandler) CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
sourceID := r.FormValue("source_id")
url := r.FormValue("url")
if sourceID == "" || url == "" {
http.Error(w, "Missing 'source_id' or 'url'", http.StatusBadRequest)
return
}
task := &Task{
SourceID: sourceID,
URL: url,
ModelList: "default_model",
ReconnectCount: 5,
OperationType: 1,
Second: 10,
IsCascade: 0,
Status: "FINISHED", // 初始状态为可运行
CreatedAt: time.Now(),
}
if err := CreateTask(task); err != nil {
http.Error(w, "Failed to create task: "+err.Error(), http.StatusConflict)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte("Task created successfully."))
}
// GetTasksHandler 获取所有任务
func (h *APIHandler) GetTasksHandler(w http.ResponseWriter, r *http.Request) {
tasks, err := GetAllTasks()
if err != nil {
http.Error(w, "Failed to retrieve tasks: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(tasks)
}
// DeleteTaskHandler 删除任务
func (h *APIHandler) DeleteTaskHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sourceID := vars["source_id"]
if err := DeleteTask(sourceID); err != nil {
http.Error(w, "Failed to delete task: "+err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
}