From 86545fd4cb7f41f25e36e0d46fb5f16a5b552111 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 26 Apr 2026 00:12:08 +0800 Subject: [PATCH] =?UTF-8?q?Debug:=20=E6=B7=BB=E5=8A=A0=E8=AF=A6=E7=BB=86?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BB=A5=E8=AF=8A=E6=96=AD=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E5=92=8C=E6=95=B0=E6=8D=AE=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加设备时记录接口和邻居数量 - 获取设备列表时记录返回数量 - 数据库保存成功/失败都有明确日志 - 创建API测试脚本便于调试 --- cmd/main.go | 16 +++++++++++++--- test-api.bat | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test-api.bat diff --git a/cmd/main.go b/cmd/main.go index c82a8d2..09ba1b5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -262,12 +262,14 @@ func (app *App) handleGetDevices(w http.ResponseWriter, r *http.Request) { var err error devices, err = app.storage.GetAllDevices() if err != nil { - log.Printf("Warning: failed to get devices from database: %v", err) - // 降级到从builder获取 + log.Printf("Error: failed to get devices from database: %v", err) + // 降级到 builder获取 devices = app.builder.GetDevices() } + log.Printf("Returning %d devices from storage", len(devices)) } else { devices = app.builder.GetDevices() + log.Printf("Returning %d devices from builder", len(devices)) } w.Header().Set("Content-Type", "application/json") @@ -294,20 +296,28 @@ func (app *App) handleAddDevice(w http.ResponseWriter, r *http.Request) { } deviceType := models.DeviceType(req.Type) + log.Printf("Adding device: %s (type: %s)", req.IP, req.Type) + dev, err := device.DiscoverDevice(req.IP, deviceType, req.Username, req.Password) if err != nil { + log.Printf("Failed to discover device %s: %v", req.IP, err) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(map[string]string{"message": err.Error()}) return } + log.Printf("Device discovered: %s, interfaces: %d, neighbors: %d", + dev.IP, len(dev.Interfaces), len(dev.Neighbors)) + app.builder.AddDevice(*dev) // 保存到数据库 if app.storage != nil { if err := app.storage.SaveDevice(dev); err != nil { - log.Printf("Warning: failed to save device to database: %v", err) + log.Printf("Error: failed to save device %s to database: %v", req.IP, err) + } else { + log.Printf("Device %s saved to database successfully", req.IP) } } diff --git a/test-api.bat b/test-api.bat new file mode 100644 index 0000000..7b88ff1 --- /dev/null +++ b/test-api.bat @@ -0,0 +1,16 @@ +@echo off +chcp 65001 >nul 2>&1 +echo ======================================== +echo Testing API Endpoints +echo ======================================== +echo. + +echo [1] Testing /api/devices endpoint... +curl -s http://localhost:8080/api/devices | python -m json.tool +echo. + +echo [2] Testing /api/topology endpoint... +curl -s http://localhost:8080/api/topology | python -m json.tool +echo. + +pause