about summary refs log tree commit diff stats
path: root/query.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-20 03:32:14 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-20 04:19:26 -0400
commitfc49c5701fde7d0c9dd2fa321ba0ebbff712b08a (patch)
tree96a6805a83a01aaf04d836af74ad6ba77df23a23 /query.go
parentb6e7bb3cee897688300cacf29291ee11751fe78c (diff)
downloadgetwtxt-fc49c5701fde7d0c9dd2fa321ba0ebbff712b08a.tar.gz
streamlined error handling and misc minor fixes
Diffstat (limited to 'query.go')
-rw-r--r--query.go43
1 files changed, 25 insertions, 18 deletions
diff --git a/query.go b/query.go
index 63deb55..c47cfb0 100644
--- a/query.go
+++ b/query.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"log"
 	"net/http"
 	"strings"
@@ -11,23 +12,33 @@ import (
 func apiErrCheck(err error, r *http.Request) {
 	if err != nil {
 		uip := getIPFromCtx(r.Context())
-		log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err)
+		log.Printf("*** %v :: %v %v :: %v\n", uip, r.Method, r.URL, err)
 	}
 }
 
-func apiErrCheck500(err error, w http.ResponseWriter, r *http.Request) {
-	if err != nil {
-		uip := getIPFromCtx(r.Context())
-		log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+// Takes the output of queries and formats it for
+// an HTTP response. Iterates over the string slice,
+// appending each entry to a byte slice, and adding
+// newlines where appropriate.
+func parseQueryOut(out []string) []byte {
+	var data []byte
+
+	for _, e := range out {
+		data = append(data, []byte(e)...)
+
+		if !strings.HasSuffix(e, "\n") {
+			data = append(data, byte('\n'))
+		}
 	}
+
+	return data
 }
 
 // apiUserQuery is called via apiEndpointHandler when
 // the endpoint is "users" and r.FormValue("q") is not empty.
 // It queries the registry cache for users or user URLs
 // matching the term supplied via r.FormValue("q")
-func apiEndpointQuery(w http.ResponseWriter, r *http.Request) {
+func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error {
 	query := r.FormValue("q")
 	urls := r.FormValue("url")
 	var out []string
@@ -64,20 +75,16 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) {
 		apiErrCheck(err, r)
 
 	default:
-		http.Error(w, "500", http.StatusInternalServerError)
+		return fmt.Errorf("endpoint query, no cases match")
 	}
 
-	// iterate over the output. if there aren't
-	// explicit newlines, add them.
-	var data []byte
-	for _, e := range out {
-		data = append(data, []byte(e)...)
-		if !strings.HasSuffix(e, "\n") {
-			data = append(data, byte('\n'))
-		}
-	}
+	data := parseQueryOut(out)
 
 	w.Header().Set("Content-Type", txtutf8)
 	_, err = w.Write(data)
-	apiErrCheck500(err, w, r)
+	if err != nil {
+		return err
+	}
+
+	return nil
 }