Fix: 修复存储文件名和拓扑构建逻辑
- 修正存储文件名: network-topology.db -> devices.json - 移除handleGetDevices中的重复builder重建逻辑 - 移除冗余的调试日志输出 - 统一日志术语: database -> storage - 确保添加设备时正确更新builder和存储
This commit is contained in:
+8
-15
@@ -31,10 +31,10 @@ type App struct {
|
||||
|
||||
// NewApp 创建应用
|
||||
func NewApp(cfg *config.Config) *App {
|
||||
// 初始化数据库
|
||||
store, err := storage.NewStorage("network-topology.db")
|
||||
// 初始化存储(使用JSON文件)
|
||||
store, err := storage.NewStorage("devices.json")
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to initialize database: %v", err)
|
||||
log.Printf("Warning: failed to initialize storage: %v", err)
|
||||
}
|
||||
|
||||
app := &App{
|
||||
@@ -257,21 +257,14 @@ func (app *App) handleTopology(w http.ResponseWriter, r *http.Request) {
|
||||
func (app *App) handleGetDevices(w http.ResponseWriter, r *http.Request) {
|
||||
var devices []models.Device
|
||||
|
||||
// 优先从数据库获取
|
||||
// 优先从存储获取
|
||||
if app.storage != nil {
|
||||
var err error
|
||||
devices, err = app.storage.GetAllDevices()
|
||||
if err != nil {
|
||||
log.Printf("Error: failed to get devices from database: %v", err)
|
||||
log.Printf("Error: failed to get devices from storage: %v", err)
|
||||
// 降级到 builder获取
|
||||
devices = app.builder.GetDevices()
|
||||
} else {
|
||||
// 重新构建设备到builder中,确保拓扑正确
|
||||
app.builder = topology.NewBuilder()
|
||||
for _, dev := range devices {
|
||||
app.builder.AddDevice(dev)
|
||||
}
|
||||
log.Printf("Rebuilt topology with %d devices from database", len(devices))
|
||||
}
|
||||
log.Printf("Returning %d devices from storage", len(devices))
|
||||
} else {
|
||||
@@ -319,12 +312,12 @@ func (app *App) handleAddDevice(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
app.builder.AddDevice(*dev)
|
||||
|
||||
// 保存到数据库
|
||||
// 保存到存储
|
||||
if app.storage != nil {
|
||||
if err := app.storage.SaveDevice(dev); err != nil {
|
||||
log.Printf("Error: failed to save device %s to database: %v", req.IP, err)
|
||||
log.Printf("Error: failed to save device %s to storage: %v", req.IP, err)
|
||||
} else {
|
||||
log.Printf("Device %s saved to database successfully", req.IP)
|
||||
log.Printf("Device %s saved to storage successfully", req.IP)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user