diff --git a/bird/bird.go b/bird/bird.go index 8a69934..97639c4 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -53,7 +53,13 @@ func toCache(key string, val Parsed) { if ClientConf.CacheTtl > 0 { ttl = ClientConf.CacheTtl } - val["ttl"] = time.Now().Add(time.Duration(ttl) * time.Minute) + cachedAt := time.Now().UTC() + cacheTtl := cachedAt.Add(time.Duration(ttl) * time.Minute) + + // This is not a really ... clean way of doing this. + val["ttl"] = cacheTtl + val["cached_at"] = cachedAt + Cache.Lock() Cache.m[key] = val Cache.Unlock() @@ -186,7 +192,9 @@ func ProtocolsBgp() (Parsed, bool) { } } - return Parsed{"protocols": bgpProto, "ttl": p["ttl"]}, from_cache + return Parsed{"protocols": bgpProto, + "ttl": p["ttl"], + "cached_at": p["cached_at"]}, from_cache } func Symbols() (Parsed, bool) { diff --git a/endpoints/endpoint.go b/endpoints/endpoint.go index 5ca4a04..e967744 100644 --- a/endpoints/endpoint.go +++ b/endpoints/endpoint.go @@ -67,7 +67,7 @@ func Endpoint(wrapped endpoint) httprouter.Handle { w.Write(js) return } - res["api"] = GetApiInfo(from_cache) + res["api"] = GetApiInfo(&ret, from_cache) for k, v := range ret { res[k] = v diff --git a/endpoints/utils.go b/endpoints/utils.go index 904c4ed..688f95d 100644 --- a/endpoints/utils.go +++ b/endpoints/utils.go @@ -1,9 +1,15 @@ package endpoints +import ( + "time" + + "github.com/alice-lg/birdwatcher/bird" +) + type TimeInfo struct { - Date string `json:"date"` - TimezoneType string `json:"timezone_type"` - Timezone string `json:"timezone"` + Date time.Time `json:"date"` + TimezoneType string `json:"timezone_type"` + Timezone string `json:"timezone"` } type CacheStatus struct { @@ -20,11 +26,40 @@ type APIInfo struct { // go generate does not work in subdirectories. Beautious. var VERSION string -func GetApiInfo(from_cache bool) *APIInfo { +func GetApiInfo(res *bird.Parsed, from_cache bool) *APIInfo { ai := &APIInfo{} ai.Version = VERSION ai.ResultFromCache = from_cache + api := *res + + // Derive cache status from TTL + cachedAt, ok := api["cached_at"].(time.Time) + if !ok { + cachedAt = time.Time{} + } + + // tbh. I have no clue what the difference between + // timezone type and timezone actually is. + // I could trace back the timezonetype to the symphony framework + // Barry was using; the docs say it accepts timezones like + // "America/New_York", however nothing about UTC could be found. + // + // As we convert everything to UTC and let the client + // render it in local time, it is safe to set this to a fixed + // value. + + cacheInfo := CacheStatus{ + OrigTTL: -1, + CachedAt: TimeInfo{ + Date: cachedAt, + TimezoneType: "UTC", + Timezone: "UTC", + }, + } + + ai.CacheStatus = cacheInfo + return ai }