diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-05-09 01:37:10 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-05-09 01:37:10 -0400 |
commit | dd81555e6d53c36525358d620d25a9ab042cc9ae (patch) | |
tree | ee833ced000a2dac579d838dc002dc2bf1d81c4c | |
parent | 21633380cd3010423821a78d34db2c0fb4a740a7 (diff) | |
download | api-dd81555e6d53c36525358d620d25a9ab042cc9ae.tar.gz |
consolidated osversion functions to osversion.go
-rw-r--r-- | cache.go | 24 | ||||
-rw-r--r-- | osversion.go | 30 |
2 files changed, 28 insertions, 26 deletions
diff --git a/cache.go b/cache.go index 24744f6..c7a68b7 100644 --- a/cache.go +++ b/cache.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io/ioutil" "log" "strings" @@ -117,29 +116,6 @@ func bapIndex() { } } -func bapOSVersion(format string) { - path := fmt.Sprintf("/%s/osversion", format) - unNullPage(path) - - if cache.isFresh(path) { - return - } - - bytes, err := osVersionQuery(format) - if err != nil { - log.Printf("Could not query OS version: %s", err.Error()) - bytes = []byte("Internal Error") - } - - cache.Lock() - defer cache.Unlock() - - cache.pages[path] = &page{ - raw: bytes, - expires: time.Now().Add(cacheDuration), - } -} - func bapPkgs(format string) {} func bapQuery(format string) {} func bapUptime(format string) {} diff --git a/osversion.go b/osversion.go index 6ecac9a..eadc960 100644 --- a/osversion.go +++ b/osversion.go @@ -3,12 +3,38 @@ package main import ( "errors" "fmt" + "log" "os/exec" "strings" + "time" ) -// osVersionQuery handles the /<format>/osversion endpoint. -// Responds with the OpenBSD version. +// Checks the cache for the freshness of the osversion query. +// If stale, store the query again. +func bapOSVersion(format string) { + path := fmt.Sprintf("/%s/osversion", format) + unNullPage(path) + + if cache.isFresh(path) { + return + } + + bytes, err := osVersionQuery(format) + if err != nil { + log.Printf("Could not query OS version: %s", err.Error()) + bytes = []byte("Internal Error") + } + + cache.Lock() + defer cache.Unlock() + + cache.pages[path] = &page{ + raw: bytes, + expires: time.Now().Add(cacheDuration), + } +} + +// executes uname and responds with "$OS $VERSION" in []byte{} func osVersionQuery(format string) ([]byte, error) { out, err := exec.Command("/usr/bin/uname", "-a").Output() if err != nil { |