diff --git a/bird/bird.go b/bird/bird.go index 3d2bc83..b31ab39 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -3,32 +3,31 @@ package bird import ( "os/exec" "strings" - "time" - "sync" + "sync" + "time" ) var BirdCmd string -var Cache = struct{ - sync.RWMutex - m map[string]Parsed +var Cache = struct { + sync.RWMutex + m map[string]Parsed }{m: make(map[string]Parsed)} func fromCache(key string) (Parsed, bool) { - Cache.RLock() - val, ok := Cache.m[key] - Cache.RUnlock() - return val, ok + Cache.RLock() + val, ok := Cache.m[key] + Cache.RUnlock() + return val, ok } func toCache(key string, val Parsed) { - val["ttl"] = time.Now().Add(5 * time.Minute) - Cache.Lock() - Cache.m[key] = val - Cache.Unlock() + val["ttl"] = time.Now().Add(5 * time.Minute) + Cache.Lock() + Cache.m[key] = val + Cache.Unlock() } - func Run(args string) ([]byte, error) { args = "show " + args argsList := strings.Split(args, " ") @@ -36,9 +35,9 @@ func Run(args string) ([]byte, error) { } func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) { - if val, ok := fromCache(cmd); ok { - return val, true - } + if val, ok := fromCache(cmd); ok { + return val, true + } out, err := Run(cmd) @@ -48,8 +47,8 @@ func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) { } parsed := parser(out) - toCache(cmd, parsed) - return parsed, false + toCache(cmd, parsed) + return parsed, false } func Status() (Parsed, bool) { @@ -62,7 +61,7 @@ func Protocols() (Parsed, bool) { func ProtocolsBgp() (Parsed, bool) { p, from_cache := Protocols() - protocols := p["protocols"].([]string) + protocols := p["protocols"].([]string) bgpProto := Parsed{} diff --git a/endpoints/symbols.go b/endpoints/symbols.go index 42367aa..46e16ab 100644 --- a/endpoints/symbols.go +++ b/endpoints/symbols.go @@ -10,11 +10,11 @@ func Symbols(ps httprouter.Params) (bird.Parsed, bool) { } func SymbolTables(ps httprouter.Params) (bird.Parsed, bool) { - val, from_cache := bird.Symbols() + val, from_cache := bird.Symbols() return bird.Parsed{"symbols": val["routing table"]}, from_cache } func SymbolProtocols(ps httprouter.Params) (bird.Parsed, bool) { - val, from_cache := bird.Symbols() + val, from_cache := bird.Symbols() return bird.Parsed{"symbols": val["protocols"]}, from_cache } diff --git a/endpoints/utils.go b/endpoints/utils.go index 7fab85b..1bf3b41 100644 --- a/endpoints/utils.go +++ b/endpoints/utils.go @@ -21,7 +21,7 @@ func GetApiInfo(from_cache bool) *APIInfo { ai := &APIInfo{} ai.Version = "1.0" - ai.ResultFromCache = from_cache + ai.ResultFromCache = from_cache return ai }