summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-11 17:41:02 -0400
committerGitHub <noreply@github.com>2019-06-11 17:41:02 -0400
commitd4af885c40ba55ea0ed9adade98afe0658099c47 (patch)
tree1c1f14428e39c8322c03d77912914647ce826d06
parent1349bbc6e6627f357065602e085be762aab06383 (diff)
parent3d2994b74c1105b4c6c8fe0c10d47d6d3541907a (diff)
downloadgetwtxt-d4af885c40ba55ea0ed9adade98afe0658099c47.tar.gz
Merge pull request #3 from getwtxt/merge-indexHandler-cssHandler
merged indexHandler with cssHandler, updated tests
-rw-r--r--svc/handlers.go69
-rw-r--r--svc/handlers_test.go8
-rw-r--r--svc/svc.go4
3 files changed, 38 insertions, 43 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index f83d4b2..d3b8d8a 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -17,59 +17,58 @@ func getEtag(modtime time.Time) string {
 	return fmt.Sprintf("%x", sha256.Sum256(shabytes))
 }
 
-// handles "/"
-func indexHandler(w http.ResponseWriter, r *http.Request) {
+func sendStaticEtag(w http.ResponseWriter, isCSS bool) {
+	if isCSS {
+		etag := getEtag(staticCache.cssMod)
+		w.Header().Set("ETag", "\""+etag+"\"")
+		w.Header().Set("Content-Time", txtutf8)
+		return
+	}
+	etag := getEtag(staticCache.indexMod)
+	w.Header().Set("ETag", "\""+etag+"\"")
+	w.Header().Set("Content-Time", htmlutf8)
+}
 
+// 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()
-	etag := getEtag(staticCache.indexMod)
-	w.Header().Set("ETag", "\""+etag+"\"")
-	w.Header().Set("Content-Type", htmlutf8)
-
-	_, err := w.Write(staticCache.index)
-	staticCache.mu.RUnlock()
-	if err != nil {
-		errHTTP(w, r, err, http.StatusInternalServerError)
-		return
+	switch r.URL.Path {
+	case "/css":
+		sendStaticEtag(w, true)
+		_, err := w.Write(staticCache.css)
+		if err != nil {
+			staticCache.mu.RUnlock()
+			errHTTP(w, r, err, http.StatusInternalServerError)
+			return
+		}
+	default:
+		sendStaticEtag(w, false)
+		_, err := w.Write(staticCache.index)
+		if err != nil {
+			staticCache.mu.RUnlock()
+			errHTTP(w, r, err, http.StatusInternalServerError)
+			return
+		}
 	}
-
-	log200(r)
-}
-
-// Serving the stylesheet virtually because
-// files aren't served directly in getwtxt.
-func cssHandler(w http.ResponseWriter, r *http.Request) {
-
-	pingAssets()
-
-	staticCache.mu.RLock()
-	etag := getEtag(staticCache.cssMod)
-	w.Header().Set("ETag", "\""+etag+"\"")
-	w.Header().Set("Content-Type", cssutf8)
-
-	_, err := w.Write(staticCache.css)
 	staticCache.mu.RUnlock()
-	if err != nil {
-		errHTTP(w, r, err, http.StatusInternalServerError)
-		return
-	}
 
 	log200(r)
 }
 
 // handles "/api"
 func apiBaseHandler(w http.ResponseWriter, r *http.Request) {
-	indexHandler(w, r)
+	staticHandler(w, r)
 }
 
 // handles "/api/plain"
 // maybe add json/xml support later
 func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
-	indexHandler(w, r)
+	staticHandler(w, r)
 }
 
 func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
@@ -95,7 +94,6 @@ func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/(users|mentions|tweets)"
 func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
-
 	errLog("Error when parsing query values: ", r.ParseForm())
 
 	if r.FormValue("q") != "" || r.FormValue("url") != "" {
@@ -165,7 +163,6 @@ func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/tags"
 func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
-
 	out, err := twtxtCache.QueryInStatus("#")
 	if err != nil {
 		errHTTP(w, r, err, http.StatusInternalServerError)
@@ -190,12 +187,10 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/tags/[a-zA-Z0-9]+"
 func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
-
 	vars := mux.Vars(r)
 	tags := vars["tags"]
 
 	out := compositeStatusQuery("#"+tags, r)
-
 	out = registry.ReduceToPage(1, out)
 	data := parseQueryOut(out)
 
diff --git a/svc/handlers_test.go b/svc/handlers_test.go
index c393957..455950f 100644
--- a/svc/handlers_test.go
+++ b/svc/handlers_test.go
@@ -10,7 +10,7 @@ import (
 	"testing"
 )
 
-// The first three are testing whether the landing page is
+// The first few are testing whether the landing page is
 // being sent correctly. If i change the base behavior of
 //    /api
 //    /api/plain
@@ -23,7 +23,7 @@ func basicHandlerTest(path string, name string, t *testing.T) {
 		w := httptest.NewRecorder()
 		req := httptest.NewRequest("GET", path, nil)
 
-		indexHandler(w, req)
+		staticHandler(w, req)
 		resp := w.Result()
 		defer resp.Body.Close()
 
@@ -51,7 +51,7 @@ func Benchmark_indexHandler(b *testing.B) {
 	b.ResetTimer()
 
 	for i := 0; i < b.N; i++ {
-		indexHandler(w, req)
+		staticHandler(w, req)
 	}
 }
 func Test_apiBaseHandler(t *testing.T) {
@@ -224,7 +224,7 @@ func Test_cssHandler(t *testing.T) {
 	req := httptest.NewRequest("GET", "http://localhost"+testport+"/css", nil)
 
 	t.Run(name, func(t *testing.T) {
-		cssHandler(w, req)
+		staticHandler(w, req)
 
 		resp := w.Result()
 		defer resp.Body.Close()
diff --git a/svc/svc.go b/svc/svc.go
index 6c9ee58..6284239 100644
--- a/svc/svc.go
+++ b/svc/svc.go
@@ -50,11 +50,11 @@ func newServer(port string, index *mux.Router) *http.Server {
 func setIndexRouting(index *mux.Router) {
 	index.Path("/").
 		Methods("GET", "HEAD").
-		HandlerFunc(indexHandler)
+		HandlerFunc(staticHandler)
 
 	index.Path("/css").
 		Methods("GET", "HEAD").
-		HandlerFunc(cssHandler)
+		HandlerFunc(staticHandler)
 
 	index.Path("/api").
 		Methods("GET", "HEAD").