about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-09 14:36:55 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-09 14:36:55 -0400
commit707e45702f7aed6d0a0f12c68f235f0fe8bdd9a1 (patch)
tree6ab3e89e7d3b4c33a5922e0d54342b8817dcfaa6
parent1e93f53cbea237fa9fdcced97971198cd897dc9b (diff)
downloadapi-707e45702f7aed6d0a0f12c68f235f0fe8bdd9a1.tar.gz
cache.yoink(requestPath) now returns both the raw
response body from the cache and the expiration time formatted
as an rfc1123 datetime.

added a default clause to cache.bap(requestPath) that responds with
a failure state if an invalid query type has been requested.
-rw-r--r--cache.go17
-rw-r--r--http.go9
2 files changed, 9 insertions, 17 deletions
diff --git a/cache.go b/cache.go
index 19e00ec..9ec1109 100644
--- a/cache.go
+++ b/cache.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"errors"
 	"io/ioutil"
 	"log"
 	"strings"
@@ -80,6 +81,8 @@ func (cache *cacheWrapper) bap(requestPath string) {
 		bytes, err = uptimeQuery(format)
 	case "usercount":
 		bytes, err = userCountQuery(format)
+	default:
+		err = errors.New("Invalid Query Type")
 	}
 
 	if err != nil {
@@ -96,20 +99,12 @@ func (cache *cacheWrapper) bap(requestPath string) {
 	}
 }
 
-// yoinks the raw data to send to the requester
-func (cache *cacheWrapper) yoink(path string) []byte {
+// yoinks the raw data and expiry time to send to the requester
+func (cache *cacheWrapper) yoink(path string) ([]byte, string) {
 	cache.RLock()
 	defer cache.RUnlock()
 
-	return cache.pages[path].raw
-}
-
-// yoinks the expiration for the cache
-func (cache *cacheWrapper) expiresWhen(path string) string {
-	cache.RLock()
-	defer cache.RUnlock()
-
-	return cache.pages[path].expires.Format(time.RFC1123)
+	return cache.pages[path].raw, cache.pages[path].expires.Format(time.RFC1123)
 }
 
 // Checks if cache either has expired or has nil copy of
diff --git a/http.go b/http.go
index a2948f7..8ef76c5 100644
--- a/http.go
+++ b/http.go
@@ -9,10 +9,7 @@ import (
 const mimePlain = "text/plain; charset=utf-8"
 const mimeJSON = "application/json; charset=utf-8"
 
-// Validates the request and then sends it off to where it needs to go.
-// Eventually.
-// I chose this monolithic handler that calls validation functions to
-// determine what to do next because this will make it easier to test.
+// Validates the request and then queries the cache for the response.
 func mainHandler(w http.ResponseWriter, r *http.Request) {
 	if !methodHop(r) {
 		errHTTP(w, r, errors.New("405 Method Not Allowed"), http.StatusMethodNotAllowed)
@@ -26,7 +23,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
 	}
 
 	cache.bap(r.URL.Path)
-	out := cache.yoink(r.URL.Path)
+	out, expires := cache.yoink(r.URL.Path)
 
 	if format == "json" {
 		w.Header().Set("Content-Type", mimeJSON)
@@ -34,7 +31,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Content-Type", mimePlain)
 	}
 
-	w.Header().Set("Expires", cache.expiresWhen(r.URL.Path))
+	w.Header().Set("Expires", expires)
 
 	_, err := w.Write(out)
 	if err != nil {