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