summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-13 22:25:16 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-13 22:34:22 -0400
commitdf79157454391a17bb9276cf44d9141d1a29b8f6 (patch)
treecc88f1a07d579b3f1863036cba6d405e22636719
parentec2c4edb9d47dfee9d402bf63d3d006900fbc5a1 (diff)
downloadgetwtxt-df79157454391a17bb9276cf44d9141d1a29b8f6.tar.gz
commented indexHandler
-rw-r--r--handlers.go19
1 files changed, 14 insertions, 5 deletions
diff --git a/handlers.go b/handlers.go
index f00eec5..c455e99 100644
--- a/handlers.go
+++ b/handlers.go
@@ -14,15 +14,24 @@ import (
 
 // handles "/"
 func indexHandler(w http.ResponseWriter, _ *http.Request) {
-	indextmpl, err := os.Stat("assets/tmpl/index.html")
-	if err != nil {
-		log.Printf("Couldn't stat index template, sending empty ETag ... %v\n", err)
+
+	// Stat the index template to get the mod time
+	var etag string
+	if indextmpl, err := os.Stat("assets/tmpl/index.html"); err != nil {
+		log.Printf("Couldn't stat index template for ETag ... %v\n", err)
+	} else {
+		etag = fmt.Sprintf("%x", sha256.Sum256([]byte(indextmpl.ModTime().String())))
 	}
 
-	etag := fmt.Sprintf("%x", sha256.Sum256([]byte(indextmpl.ModTime().String())))
+	// Take the hex-encoded sha256 sum of the index template's mod time
+	// to send as an ETag. If an error occured when grabbing the file info,
+	// the ETag will be empty.
 	w.Header().Set("ETag", "\""+etag+"\"")
 	w.Header().Set("Content-Type", htmlutf8)
-	err = tmpls.ExecuteTemplate(w, "index.html", confObj.Instance)
+
+	// Pass the confObj.Instance data to the template,
+	// then send it to the client.
+	err := tmpls.ExecuteTemplate(w, "index.html", confObj.Instance)
 	if err != nil {
 		log.Printf("Error writing to HTTP stream: %v\n", err)
 		http.Error(w, err.Error(), http.StatusInternalServerError)