Initial commit
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type debugCheck struct {
|
||||
OK bool `json:"ok"`
|
||||
Status string `json:"status"`
|
||||
DurationMS int64 `json:"duration_ms,omitempty"`
|
||||
HTTPStatus int `json:"http_status,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Details map[string]any `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) debugStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"request_id": requestID(r),
|
||||
"debug": map[string]any{
|
||||
"enabled": true,
|
||||
"verbose": s.cfg.DebugVerbose,
|
||||
"log_level": s.cfg.LogLevel.String(),
|
||||
},
|
||||
"config": s.debugConfig(),
|
||||
"checks": map[string]any{
|
||||
"db": s.store.DebugStatus(r.Context()),
|
||||
"azure": s.azureStatus(),
|
||||
"flowise": s.flowiseStatus(r.Context()),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) debugDB(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"request_id": requestID(r),
|
||||
"db": s.store.DebugStatus(r.Context()),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) debugFlowise(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"request_id": requestID(r),
|
||||
"flowise": s.flowiseStatus(r.Context()),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) debugAzure(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"request_id": requestID(r),
|
||||
"azure": s.azureStatus(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) debugRuntime(w http.ResponseWriter, r *http.Request) {
|
||||
hostname, _ := os.Hostname()
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"request_id": requestID(r),
|
||||
"runtime": map[string]any{
|
||||
"hostname": hostname,
|
||||
"go_version": runtime.Version(),
|
||||
"go_os": runtime.GOOS,
|
||||
"go_arch": runtime.GOARCH,
|
||||
"goroutines": runtime.NumGoroutine(),
|
||||
"uptime_seconds": int64(time.Since(s.started).Seconds()),
|
||||
"started_at": s.started.UTC().Format(time.RFC3339),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) debugConfig() map[string]any {
|
||||
return map[string]any{
|
||||
"api_port": s.cfg.Port,
|
||||
"cors_origins": s.cfg.CORSOrigins,
|
||||
"azure_mock": s.cfg.AzureMock,
|
||||
"azure_region": valueOr(s.cfg.AzureRegion, "not-set"),
|
||||
"azure_speech_key_set": s.cfg.AzureSpeechKey != "",
|
||||
"flowise_base_url": valueOr(s.cfg.FlowiseBaseURL, "not-set"),
|
||||
"flowise_chatflow_set": s.cfg.FlowiseChatflow != "",
|
||||
"flowise_api_key_set": s.cfg.FlowiseAPIKey != "",
|
||||
"jwt_secret_configured": s.cfg.JWTSecret != "" && s.cfg.JWTSecret != "dev-secret-change-me",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) azureStatus() debugCheck {
|
||||
details := map[string]any{
|
||||
"mock_enabled": s.cfg.AzureMock,
|
||||
"region": valueOr(s.cfg.AzureRegion, "not-set"),
|
||||
"speech_key_set": s.cfg.AzureSpeechKey != "",
|
||||
}
|
||||
if s.cfg.AzureMock {
|
||||
return debugCheck{OK: true, Status: "mock", Details: details}
|
||||
}
|
||||
if s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
||||
return debugCheck{OK: false, Status: "not_configured", Details: details}
|
||||
}
|
||||
details["stt_endpoint"] = "https://" + s.cfg.AzureRegion + ".stt.speech.microsoft.com"
|
||||
details["tts_endpoint"] = "https://" + s.cfg.AzureRegion + ".tts.speech.microsoft.com"
|
||||
return debugCheck{OK: true, Status: "configured", Details: details}
|
||||
}
|
||||
|
||||
func (s *Server) flowiseStatus(ctx context.Context) debugCheck {
|
||||
baseURL := strings.TrimRight(s.cfg.FlowiseBaseURL, "/")
|
||||
details := map[string]any{
|
||||
"base_url": valueOr(baseURL, "not-set"),
|
||||
"chatflow_set": s.cfg.FlowiseChatflow != "",
|
||||
"api_key_set": s.cfg.FlowiseAPIKey != "",
|
||||
}
|
||||
if baseURL == "" {
|
||||
return debugCheck{OK: false, Status: "not_configured", Details: details}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
checkCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(checkCtx, http.MethodGet, baseURL, nil)
|
||||
if err != nil {
|
||||
return debugCheck{OK: false, Status: "invalid_url", Error: err.Error(), Details: details}
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return debugCheck{OK: false, Status: "unreachable", Error: err.Error(), DurationMS: time.Since(start).Milliseconds(), Details: details}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
ok := resp.StatusCode < 500
|
||||
status := "reachable"
|
||||
if !ok {
|
||||
status = "server_error"
|
||||
}
|
||||
return debugCheck{
|
||||
OK: ok,
|
||||
Status: status,
|
||||
HTTPStatus: resp.StatusCode,
|
||||
DurationMS: time.Since(start).Milliseconds(),
|
||||
Details: details,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user