summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-11 20:10:58 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-11 20:10:58 -0400
commit69adb2159f0c050d18d075241945d3224dca5d30 (patch)
treea991343772ee112f34d7f27f87a60cab68480836
parentc9a35a4a6879f8070ba5d1cd0dda74e9cf99e702 (diff)
downloadgetwtxt-69adb2159f0c050d18d075241945d3224dca5d30.tar.gz
cleaned up staticHandler
-rw-r--r--svc/handlers.go18
1 files changed, 8 insertions, 10 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index 9ab9cdb..def01ad 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -19,36 +19,34 @@ func getEtag(modtime time.Time) string {
 }
 
 func servStatic(w http.ResponseWriter, isCSS bool) error {
+	pingAssets()
+
+	staticCache.mu.RLock()
 	if isCSS {
 		etag := getEtag(staticCache.cssMod)
 		w.Header().Set("ETag", "\""+etag+"\"")
 		w.Header().Set("Content-Type", cssutf8)
 		_, err := w.Write(staticCache.css)
+		staticCache.mu.RUnlock()
 		return err
 	}
+
 	etag := getEtag(staticCache.indexMod)
 	w.Header().Set("ETag", "\""+etag+"\"")
 	w.Header().Set("Content-Type", htmlutf8)
 	_, err := w.Write(staticCache.index)
+	staticCache.mu.RUnlock()
 	return err
 }
 
 // handles "/" and "/css"
 func staticHandler(w http.ResponseWriter, r *http.Request) {
-	pingAssets()
-
-	// Take the hex-encoded sha256 sum of the index template's mod time
-	// to send as an ETag. If an error occurred when grabbing the file info,
-	// the ETag will be empty.
-	staticCache.mu.RLock()
 	isCSS := strings.Contains(r.URL.Path, "css")
-	err := servStatic(w, isCSS)
-	if err != nil {
-		staticCache.mu.RUnlock()
+
+	if err := servStatic(w, isCSS); err != nil {
 		errHTTP(w, r, err, http.StatusInternalServerError)
 		return
 	}
-	staticCache.mu.RUnlock()
 
 	log200(r)
 }