summary refs log tree commit diff stats
path: root/handlers.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-13 17:20:40 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-13 17:22:28 -0400
commitd6fbc25111a9a76b40f19b2481428a9170c706b0 (patch)
treed071366939de7a6994c56153f81b16e836883043 /handlers.go
parent9d31e04f8ec8c53c6f6e2323d7422b86b7a4ad90 (diff)
downloadgetwtxt-d6fbc25111a9a76b40f19b2481428a9170c706b0.tar.gz
serving css virtually instead of directly
Diffstat (limited to 'handlers.go')
-rw-r--r--handlers.go49
1 files changed, 42 insertions, 7 deletions
diff --git a/handlers.go b/handlers.go
index 01049b0..3b23761 100644
--- a/handlers.go
+++ b/handlers.go
@@ -3,8 +3,10 @@ package main
 import (
 	"crypto/sha256"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
+	"os"
 	"time"
 
 	"github.com/gorilla/mux"
@@ -15,7 +17,7 @@ func indexHandler(w http.ResponseWriter, _ *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("getwtxt v" + getwtxt))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -33,7 +35,7 @@ func apiBaseHandler(w http.ResponseWriter, r *http.Request) {
 	timerfc3339 = append(timerfc3339, pathdata...)
 	n, err := w.Write(timerfc3339)
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 }
 
@@ -46,7 +48,7 @@ func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", txtutf8)
 	n, err := w.Write([]byte(format + "\n"))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 }
 
@@ -59,7 +61,7 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte(format + "/" + endpoint))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -73,7 +75,7 @@ func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte(format + "/" + endpoint))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -86,7 +88,7 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("api/" + format + "/tags"))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -100,7 +102,40 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("api/" + format + "/tags/" + tags))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
+
+// Serving the stylesheet virtually because
+// files aren't served directly.
+func cssHandler(w http.ResponseWriter, _ *http.Request) {
+	// read the raw bytes of the stylesheet
+	css, err := ioutil.ReadFile("assets/style.css")
+	if err != nil {
+		if os.IsNotExist(err) {
+			log.Printf("CSS file does not exist: /css request 404\n")
+			http.Error(w, err.Error(), http.StatusNotFound)
+			return
+		}
+		log.Printf("%v\n", err)
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	// Get the mod time for the etag header
+	stat, err := os.Stat("assets/style.css")
+	if err != nil {
+		log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err)
+	}
+
+	// Sending the sha256 sum of the modtime in hexadecimal for the ETag header
+	etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
+
+	w.Header().Set("ETag", "\""+etag+"\"")
+	w.Header().Set("Content-Type", cssutf8)
+	n, err := w.Write(css)
+	if err != nil || n == 0 {
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
+	}
+}