-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
335 lines (282 loc) · 9.83 KB
/
Copy pathapi.go
File metadata and controls
335 lines (282 loc) · 9.83 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package daemon
import (
"encoding/json"
"net/http"
"runtime"
"strings"
"time"
"github.com/dopejs/gozen/internal/config"
"github.com/dopejs/gozen/internal/proxy"
)
// getBotBridge returns the global bot bridge.
func getBotBridge() *proxy.BotBridge {
return proxy.GetBotBridge()
}
// --- Daemon Status API ---
type daemonStatusResponse struct {
Status string `json:"status"`
Version string `json:"version"`
Uptime string `json:"uptime"`
UptimeSeconds int64 `json:"uptime_seconds"`
ProxyPort int `json:"proxy_port"`
WebPort int `json:"web_port"`
ActiveSessions int `json:"active_sessions"`
FeatureGates *config.FeatureGates `json:"feature_gates,omitempty"`
}
type daemonMemoryStats struct {
AllocBytes uint64 `json:"alloc_bytes"`
SysBytes uint64 `json:"sys_bytes"`
HeapAllocBytes uint64 `json:"heap_alloc_bytes"`
HeapObjects uint64 `json:"heap_objects"`
NumGC uint32 `json:"num_gc"`
}
type daemonProviderHealth struct {
Name string `json:"name"`
Status proxy.HealthStatus `json:"status"`
LastCheck *time.Time `json:"last_check,omitempty"`
LatencyMs int `json:"latency_ms,omitempty"`
SuccessRate float64 `json:"success_rate"`
CheckCount int `json:"check_count"`
FailCount int `json:"fail_count"`
}
type daemonHealthResponse struct {
Status string `json:"status"`
Version string `json:"version"`
UptimeSeconds int64 `json:"uptime_seconds"`
Goroutines int `json:"goroutines"`
Memory daemonMemoryStats `json:"memory"`
ActiveSessions int `json:"active_sessions"`
HealthCheckEnabled bool `json:"health_check_enabled"`
HealthCheckRunning bool `json:"health_check_running"`
Providers []daemonProviderHealth `json:"providers"`
}
func (d *Daemon) handleDaemonStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
uptime := time.Since(d.startTime)
writeJSON(w, http.StatusOK, daemonStatusResponse{
Status: "running",
Version: d.version,
Uptime: uptime.Truncate(time.Second).String(),
UptimeSeconds: int64(uptime.Seconds()),
ProxyPort: d.proxyPort,
WebPort: d.webPort,
ActiveSessions: d.ActiveSessionCount(),
FeatureGates: config.GetFeatureGates(),
})
}
func (d *Daemon) handleDaemonHealth(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
cfg := config.GetHealthCheck()
checker := proxy.GetGlobalHealthChecker()
running := checker != nil && checker.IsRunning()
providers := make([]daemonProviderHealth, 0)
unhealthyCount := 0
degradedCount := 0
if checker != nil {
for _, status := range checker.GetAllStatus() {
providers = append(providers, daemonProviderHealth{
Name: status.Provider,
Status: status.Status,
LastCheck: status.LastCheck,
LatencyMs: status.LatencyMs,
SuccessRate: status.SuccessRate,
CheckCount: status.CheckCount,
FailCount: status.FailCount,
})
switch status.Status {
case proxy.HealthStatusUnhealthy:
unhealthyCount++
case proxy.HealthStatusDegraded:
degradedCount++
}
}
}
overallStatus := "healthy"
if runtime.NumGoroutine() > 1000 || mem.Alloc > 500*1024*1024 {
overallStatus = "degraded"
}
if degradedCount > 0 || unhealthyCount > 0 {
overallStatus = "degraded"
}
if len(providers) > 0 && unhealthyCount == len(providers) {
overallStatus = "unhealthy"
}
writeJSON(w, http.StatusOK, daemonHealthResponse{
Status: overallStatus,
Version: d.version,
UptimeSeconds: int64(time.Since(d.startTime).Seconds()),
Goroutines: runtime.NumGoroutine(),
Memory: daemonMemoryStats{
AllocBytes: mem.Alloc,
SysBytes: mem.Sys,
HeapAllocBytes: mem.HeapAlloc,
HeapObjects: mem.HeapObjects,
NumGC: mem.NumGC,
},
ActiveSessions: d.ActiveSessionCount(),
HealthCheckEnabled: cfg != nil && cfg.Enabled,
HealthCheckRunning: running,
Providers: providers,
})
}
// --- Daemon Metrics API ---
func (d *Daemon) handleDaemonMetrics(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
if d.metrics == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "metrics not initialized"})
return
}
// Update resource peaks before returning stats
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
memoryMB := int64(mem.Alloc / 1024 / 1024)
d.metrics.UpdateResourcePeaks(runtime.NumGoroutine(), memoryMB)
stats := d.metrics.GetStats()
writeJSON(w, http.StatusOK, stats)
}
// --- Daemon Shutdown API ---
func (d *Daemon) handleDaemonShutdown(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "shutting down"})
// Trigger shutdown in background so the response is sent first
go func() {
time.Sleep(100 * time.Millisecond)
d.shutdownOnce.Do(func() { close(d.shutdownCh) })
}()
}
// ShutdownCh returns a channel that is closed when shutdown is requested via API.
func (d *Daemon) ShutdownCh() <-chan struct{} {
return d.shutdownCh
}
// ProxyErrCh returns the proxy error channel for crash detection
func (d *Daemon) ProxyErrCh() <-chan error {
return d.proxyErrCh
}
// --- Daemon Reload API ---
func (d *Daemon) handleDaemonReload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
d.onConfigReload()
writeJSON(w, http.StatusOK, map[string]string{"status": "reloaded"})
}
// --- Daemon Sessions API ---
type registerSessionRequest struct {
SessionID string `json:"session_id"`
Profile string `json:"profile"`
ClientType string `json:"client_type"`
}
func (d *Daemon) handleDaemonSessions(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
d.handleGetSessions(w, r)
case http.MethodPost:
d.handleRegisterSession(w, r)
default:
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}
func (d *Daemon) handleGetSessions(w http.ResponseWriter, r *http.Request) {
d.mu.RLock()
sessions := make([]SessionInfo, 0, len(d.sessions))
for _, s := range d.sessions {
sessions = append(sessions, *s) // copy struct to avoid data race after unlock
}
d.mu.RUnlock()
writeJSON(w, http.StatusOK, map[string]interface{}{
"sessions": sessions,
"count": len(sessions),
})
}
func (d *Daemon) handleRegisterSession(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var req registerSessionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
if req.SessionID == "" || req.Profile == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "session_id and profile are required"})
return
}
d.RegisterSession(req.SessionID, req.Profile, req.ClientType)
// Register with bot bridge
if bridge := getBotBridge(); bridge != nil {
cacheKey := req.Profile + ":" + req.SessionID
bridge.UpdateSession(cacheKey, req.ClientType, nil, "", "", "input")
d.logger.Printf("[session] registered %s (client=%s)", cacheKey, req.ClientType)
}
writeJSON(w, http.StatusOK, map[string]string{"status": "registered"})
}
// --- Temporary Profile API ---
type tempProfileRequest struct {
Providers []string `json:"providers"`
}
type tempProfileResponse struct {
ID string `json:"id"`
}
func (d *Daemon) handleTempProfiles(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
var req tempProfileRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
_ = r.Body.Close()
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
return
}
_ = r.Body.Close()
if len(req.Providers) == 0 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "providers required"})
return
}
// Validate provider names exist
store := config.DefaultStore()
for _, name := range req.Providers {
if store.GetProvider(name) == nil {
writeJSON(w, http.StatusBadRequest, map[string]string{
"error": "provider not found: " + name,
})
return
}
}
id := d.RegisterTempProfile(req.Providers)
writeJSON(w, http.StatusCreated, tempProfileResponse{ID: id})
}
func (d *Daemon) handleTempProfile(w http.ResponseWriter, r *http.Request) {
// Extract ID from URL: /api/v1/profiles/temp/{id}
id := strings.TrimPrefix(r.URL.Path, "/api/v1/profiles/temp/")
if id == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "id required"})
return
}
switch r.Method {
case http.MethodDelete:
d.RemoveTempProfile(id)
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
default:
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}
// --- Helpers ---
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(v)
}