package database import ( "net" "time" log "github.com/sirupsen/logrus" "dev.sum7.eu/genofire/golang-lib/file" "dev.sum7.eu/genofire/golang-lib/worker" ) type DB struct { Clients map[string]*Client `json:"client"` APs map[string]*AP `json:"ap"` worker *worker.Worker } func NewDB(path string) *DB { db := &DB{ Clients: make(map[string]*Client), APs: make(map[string]*AP), } file.ReadJSON(path, db) for addr, client := range db.Clients { client.Addr, _ = net.ParseMAC(addr) if ap, ok := db.APs[client.APAddr]; ok { client.AP = ap } } db.worker = worker.NewWorker(time.Minute, func() { file.SaveJSON(path, db) log.Debug("save db state") }) db.worker.Start() return db } func (db *DB) Close() { if db.worker != nil { db.worker.Close() } }