Improve error handling and fallback mechanisms in WebUI server

This commit is contained in:
Andy Oknen 2025-07-31 04:31:33 +00:00
parent 791214c18b
commit 1f75299312

View file

@ -63,7 +63,10 @@ func (w *WebUIServer) SetAdmin(admin *admin.AdminSocket) {
// generateSessionID creates a random session ID // generateSessionID creates a random session ID
func (w *WebUIServer) generateSessionID() string { func (w *WebUIServer) generateSessionID() string {
bytes := make([]byte, 32) bytes := make([]byte, 32)
rand.Read(bytes) if _, err := rand.Read(bytes); err != nil {
// Fallback to timestamp-based ID if random generation fails
return hex.EncodeToString([]byte(fmt.Sprintf("%d", time.Now().UnixNano())))
}
return hex.EncodeToString(bytes) return hex.EncodeToString(bytes)
} }
@ -327,10 +330,12 @@ func (w *WebUIServer) adminAPIHandler(rw http.ResponseWriter, r *http.Request) {
// Return list of available commands // Return list of available commands
commands := w.admin.GetAvailableCommands() commands := w.admin.GetAvailableCommands()
rw.Header().Set("Content-Type", "application/json") rw.Header().Set("Content-Type", "application/json")
json.NewEncoder(rw).Encode(map[string]interface{}{ if err := json.NewEncoder(rw).Encode(map[string]interface{}{
"status": "success", "status": "success",
"commands": commands, "commands": commands,
}) }); err != nil {
http.Error(rw, "Failed to encode response", http.StatusInternalServerError)
}
return return
} }
@ -348,18 +353,22 @@ func (w *WebUIServer) adminAPIHandler(rw http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
rw.Header().Set("Content-Type", "application/json") rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusBadRequest) rw.WriteHeader(http.StatusBadRequest)
json.NewEncoder(rw).Encode(map[string]interface{}{ if encErr := json.NewEncoder(rw).Encode(map[string]interface{}{
"status": "error", "status": "error",
"error": err.Error(), "error": err.Error(),
}) }); encErr != nil {
http.Error(rw, "Failed to encode error response", http.StatusInternalServerError)
}
return return
} }
rw.Header().Set("Content-Type", "application/json") rw.Header().Set("Content-Type", "application/json")
json.NewEncoder(rw).Encode(map[string]interface{}{ if err := json.NewEncoder(rw).Encode(map[string]interface{}{
"status": "success", "status": "success",
"response": result, "response": result,
}) }); err != nil {
http.Error(rw, "Failed to encode response", http.StatusInternalServerError)
}
} }
// callAdminHandler calls admin handlers directly without socket // callAdminHandler calls admin handlers directly without socket