summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-21 22:04:19 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 22:04:19 -0400
commitbded77fe18a8acb6397835a836ac68f7435bf3d4 (patch)
tree77e319f53b1d7cb79dc2e5f5a48f44556429ece6
parent4f0847bb24a5df77f88c883461491a2f6fd955d7 (diff)
downloadgetwtxt-bded77fe18a8acb6397835a836ac68f7435bf3d4.tar.gz
runtime bugs re: http400 and database reading on startup
-rw-r--r--cache.go92
-rw-r--r--http.go3
-rw-r--r--init.go8
-rw-r--r--post.go15
4 files changed, 61 insertions, 57 deletions
diff --git a/cache.go b/cache.go
index 6220b26..fedc29d 100644
--- a/cache.go
+++ b/cache.go
@@ -2,7 +2,7 @@ package main
 
 import (
 	"log"
-	"reflect"
+	"net"
 	"strings"
 	"time"
 
@@ -123,66 +123,60 @@ func pullDatabase() {
 
 	iter := db.NewIterator(nil, nil)
 
-	// Read the database key-by-key
+	// Read the database entry-by-entry
 	for iter.Next() {
-		key := iter.Key()
-		val := iter.Value()
+		key := string(iter.Key())
+		val := string(iter.Value())
 
-		split := strings.Split(string(key), "*")
+		split := strings.Split(key, "*")
 		urls := string(split[0])
 		field := string(split[1])
 
-		// Start with an empty Data struct. If
-		// there's already one in the cache, pull
-		// it and use it instead.
-		data := registry.NewUserData()
-		twtxtCache.Mu.RLock()
-		if _, ok := twtxtCache.Reg[urls]; ok {
-			data = twtxtCache.Reg[urls]
-		}
-		twtxtCache.Mu.RUnlock()
-		ref := reflect.ValueOf(data).Elem()
-
-		// Use reflection to find the right field
-		// in the Data struct. Once found, assign
-		// the value and break so the DB iteration
-		// can continue.
-		if field != "Status" && urls != "remote" {
-			for i := 0; i < ref.NumField(); i++ {
-
-				f := ref.Field(i)
-				if strings.Contains(f.String(), field) {
-					f.Set(reflect.ValueOf(val))
-					break
-				}
+		if urls != "remote" {
+			// Start with an empty Data struct. If
+			// there's already one in the cache, pull
+			// it and use it instead.
+			data := registry.NewUserData()
+			twtxtCache.Mu.RLock()
+			if _, ok := twtxtCache.Reg[urls]; ok {
+				data = twtxtCache.Reg[urls]
 			}
-		} else if field == "Status" && urls != "remote" {
-
-			// If we're looking at a Status entry in the DB,
-			// parse the time then add it to the TimeMap under
-			// data.Status
-			thetime, err := time.Parse(time.RFC3339, split[2])
-			if err != nil {
-				log.Printf("%v\n", err)
+			twtxtCache.Mu.RUnlock()
+
+			switch field {
+			case "IP":
+				data.IP = net.ParseIP(val)
+			case "Nick":
+				data.Nick = val
+			case "URL":
+				data.URL = val
+			case "Date":
+				data.Date = val
+			case "Status":
+				// If we're looking at a Status entry in the DB,
+				// parse the time then add it to the TimeMap under
+				// data.Status
+				thetime, err := time.Parse(time.RFC3339, split[2])
+				if err != nil {
+					log.Printf("%v\n", err)
+				}
+				data.Status[thetime] = val
 			}
-			data.Status[thetime] = string(val)
+
+			// Push the data struct (back) into
+			// the cache.
+			twtxtCache.Mu.Lock()
+			twtxtCache.Reg[urls] = data
+			twtxtCache.Mu.Unlock()
 
 		} else {
-			// The third and final possibility is
-			// if we've come across an entry for
-			// a remote twtxt registry to scrape.
-			// If so, add it to our list.
+			// If we've come across an entry for
+			// a remote twtxt registry to scrape,
+			// add it to our list.
 			remoteRegistries.Mu.Lock()
-			remoteRegistries.List = append(remoteRegistries.List, string(val))
+			remoteRegistries.List = append(remoteRegistries.List, val)
 			remoteRegistries.Mu.Unlock()
-			continue
 		}
-
-		// Push the data struct (back) into
-		// the cache.
-		twtxtCache.Mu.Lock()
-		twtxtCache.Reg[urls] = data
-		twtxtCache.Mu.Unlock()
 	}
 
 	iter.Release()
diff --git a/http.go b/http.go
index 1f6126c..9b5dd81 100644
--- a/http.go
+++ b/http.go
@@ -45,10 +45,9 @@ func log200(r *http.Request) {
 }
 
 // log output for 400s
-func log400(w http.ResponseWriter, r *http.Request, err error) {
+func log400(r *http.Request, err error) {
 	uip := getIPFromCtx(r.Context())
 	log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
-	http.Error(w, err.Error(), http.StatusBadRequest)
 }
 
 // log output for 404s
diff --git a/init.go b/init.go
index b898ae8..a6b2aa1 100644
--- a/init.go
+++ b/init.go
@@ -114,9 +114,17 @@ func initConfig() {
 	confObj.port = viper.GetInt("port")
 	confObj.logFile = viper.GetString("logFile")
 	confObj.dbPath = viper.GetString("databasePath")
+	log.Printf("Using database: %v\n", confObj.dbPath)
 	confObj.stdoutLogging = viper.GetBool("stdoutLogging")
+	if confObj.stdoutLogging {
+		log.Printf("Logging to stdout\n")
+	} else {
+		log.Printf("Logging to %v\n", confObj.logFile)
+	}
 	confObj.cacheInterval = dur
+	log.Printf("Cache refresh interval: %v\n", dur)
 	confObj.dbInterval = dbDur
+	log.Printf("Database push interval: %v\n", dbDur)
 	confObj.lastCache = thetime
 	confObj.lastPush = thetime
 	confObj.version = getwtxt
diff --git a/post.go b/post.go
index 95cc6d3..b9c830e 100644
--- a/post.go
+++ b/post.go
@@ -14,14 +14,15 @@ import (
 // registry before adding each user to the local cache.
 func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	if err := r.ParseForm(); err != nil {
-		log400(w, r, err)
+		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
+		log400(r, err)
 		return
 	}
 	nick := r.FormValue("nickname")
 	urls := r.FormValue("url")
 	if nick == "" || urls == "" {
 		_, _ = w.Write([]byte("400 Bad Request: Nickname or URL Missing\n"))
-		log400(w, r, fmt.Errorf("nickname or URL missing"))
+		log400(r, fmt.Errorf("nickname or URL missing"))
 		return
 	}
 
@@ -30,7 +31,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	out, remoteRegistry, err := registry.GetTwtxt(urls)
 	if err != nil {
 		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(w, r, err)
+		log400(r, err)
 		return
 	}
 
@@ -42,7 +43,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 		err := twtxtCache.ScrapeRemoteRegistry(urls)
 		if err != nil {
 			_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-			log400(w, r, err)
+			log400(r, err)
 			return
 		}
 		log200(r)
@@ -52,15 +53,17 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
 	if err != nil {
 		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(w, r, err)
+		log400(r, err)
 		return
 	}
 
 	err = twtxtCache.AddUser(nick, urls, uip, statuses)
 	if err != nil {
-		log400(w, r, err)
+		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
+		log400(r, err)
 		return
 	}
 
 	log200(r)
+	_, _ = w.Write([]byte(fmt.Sprintf("200 OK\n")))
 }