about summary refs log tree commit diff stats
path: root/cache.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-09 02:06:06 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-09 02:06:06 -0400
commitd928c73de4c75f3113a4ddb3418789d950097fcb (patch)
tree3a6bb5ec2e2736a36790cdd3bd7cbc267b95df83 /cache.go
parentdd81555e6d53c36525358d620d25a9ab042cc9ae (diff)
downloadapi-d928c73de4c75f3113a4ddb3418789d950097fcb.tar.gz
eliminated query-specific cache checking functions
stub for uptime, will have to adjust output later.
Diffstat (limited to 'cache.go')
-rw-r--r--cache.go40
1 files changed, 29 insertions, 11 deletions
diff --git a/cache.go b/cache.go
index c7a68b7..d7839b8 100644
--- a/cache.go
+++ b/cache.go
@@ -59,19 +59,38 @@ func (cache *cacheWrapper) bap(requestPath string) {
 		bapIndex()
 		return
 	}
+
 	split := strings.Split(requestPath[1:], "/")
-	switch split[1] {
+	format := split[0]
+	query := split[1]
+
+	unNullPage(requestPath)
+
+	if cache.isFresh(requestPath) {
+		return
+	}
+
+	var bytes []byte
+	var err error
+
+	switch query {
 	case "osversion":
-		bapOSVersion(split[0])
-	case "pkgs":
-		bapPkgs(split[0])
+		bytes, err = osVersionQuery(format)
 	case "uptime":
-		bapUptime(split[0])
-	case "usercount":
-		bapUserCount(split[0])
-	case "users":
-		bapUsers(split[0])
-	default:
+		bytes, err = uptimeQuery(format)
+	}
+
+	if err != nil {
+		log.Printf("Could not query %s: %s", requestPath, err.Error())
+		bytes = []byte("Internal Error")
+	}
+
+	cache.Lock()
+	defer cache.Unlock()
+
+	cache.pages[requestPath] = &page{
+		raw:     bytes,
+		expires: time.Now().Add(cacheDuration),
 	}
 }
 
@@ -118,6 +137,5 @@ func bapIndex() {
 
 func bapPkgs(format string)      {}
 func bapQuery(format string)     {}
-func bapUptime(format string)    {}
 func bapUserCount(format string) {}
 func bapUsers(format string)     {}