about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-09 15:00:18 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-09 15:00:18 -0400
commit3565a2f54ac9892330a938c0c873e607783208d9 (patch)
tree6cd52eca24584cd4d16f15e0ca9794bc9919f746
parentd55a6f801394ec9dccf9b51b662d35b4f53e1f5a (diff)
downloadapi-3565a2f54ac9892330a938c0c873e607783208d9.tar.gz
removed index-specific cache checking
added index cache checking to cache.bap()
-rw-r--r--cache.go47
1 files changed, 13 insertions, 34 deletions
diff --git a/cache.go b/cache.go
index bc3ab3e..be00083 100644
--- a/cache.go
+++ b/cache.go
@@ -53,14 +53,14 @@ func (cache *cacheWrapper) isFresh(path string) bool {
 // Wraps the two cache-checking functions.
 // One for /, the other for various requests.
 func (cache *cacheWrapper) bap(requestPath string) {
-	if requestPath == "/" {
-		bapIndex()
-		return
-	}
+	var split []string
+	var format, query string
 
-	split := strings.Split(requestPath[1:], "/")
-	format := split[0]
-	query := split[1]
+	if requestPath != "/" {
+		split = strings.Split(requestPath[1:], "/")
+		format = split[0]
+		query = split[1]
+	}
 
 	cache.checkedInit(requestPath)
 
@@ -79,11 +79,15 @@ func (cache *cacheWrapper) bap(requestPath string) {
 	case "usercount":
 		bytes, err = userCountQuery(format)
 	default:
-		err = errors.New("Invalid Query Type")
+		if requestPath == "/" {
+			bytes, err = ioutil.ReadFile("web/index.txt")
+		} else {
+			err = errors.New("Invalid Query Type")
+		}
 	}
 
 	if err != nil {
-		log.Printf("Could not query %s: %s", requestPath, err.Error())
+		log.Printf("Request error %s :: %s", requestPath, err.Error())
 		bytes = []byte("Internal Error")
 	}
 
@@ -103,28 +107,3 @@ func (cache *cacheWrapper) yoink(path string) ([]byte, string) {
 
 	return cache.pages[path].raw, cache.pages[path].expires.Format(time.RFC1123)
 }
-
-// Checks if cache either has expired or has nil copy of
-// the index. If so, it yoinks the page from disk and
-// sets the expiration time.
-func bapIndex() {
-	cache.checkedInit("/")
-
-	if cache.isFresh("/") {
-		return
-	}
-
-	bytes, err := ioutil.ReadFile("web/index.txt")
-	if err != nil {
-		log.Printf("Could not read index page: %s", err.Error())
-		bytes = []byte("tilde.institute informational API")
-	}
-
-	cache.Lock()
-	defer cache.Unlock()
-
-	cache.pages["/"] = &page{
-		raw:     bytes,
-		expires: time.Now().Add(cacheDuration),
-	}
-}