summary refs log tree commit diff stats
path: root/svc/post.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-05 15:36:23 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-05 15:36:23 -0400
commitfd43c61bd128ad77b22db0537a9a4eb58490b0b5 (patch)
tree4c5fa7b33fadbf7c3e14e69b7d68ce280bc3810a /svc/post.go
parent4658fe82be3e9d95e93fa5c7c7ca64a15cf2f1a1 (diff)
downloadgetwtxt-fd43c61bd128ad77b22db0537a9a4eb58490b0b5.tar.gz
moved bulk of code to its own package to clean up source tree
Diffstat (limited to 'svc/post.go')
-rw-r--r--svc/post.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/svc/post.go b/svc/post.go
new file mode 100644
index 0000000..0355afa
--- /dev/null
+++ b/svc/post.go
@@ -0,0 +1,65 @@
+package svc // import "github.com/getwtxt/getwtxt/svc"
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+
+	"github.com/getwtxt/registry"
+)
+
+// Requests to apiEndpointPOSTHandler are passed off to this
+// function. apiPostUser then fetches the twtxt data, then if
+// it's an individual user's file, adds it. If it's registry
+// output, it scrapes the users/urls/statuses from the remote
+// 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, "Error Parsing Values: "+err.Error())
+		return
+	}
+
+	nick := r.FormValue("nickname")
+	urls := r.FormValue("url")
+	if nick == "" || urls == "" {
+		log400(w, r, "Nickname or URL missing")
+		return
+	}
+
+	uip := getIPFromCtx(r.Context())
+
+	out, remoteRegistry, err := registry.GetTwtxt(urls)
+	if err != nil {
+		log400(w, r, "Error Fetching twtxt Data: "+err.Error())
+		return
+	}
+
+	if remoteRegistry {
+		remoteRegistries.Mu.Lock()
+		remoteRegistries.List = append(remoteRegistries.List, urls)
+		remoteRegistries.Mu.Unlock()
+
+		if err := twtxtCache.CrawlRemoteRegistry(urls); err != nil {
+			log400(w, r, "Error Crawling Remote Registry: "+err.Error())
+			return
+		}
+		log200(r)
+		return
+	}
+
+	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
+	if err != nil {
+		log.Printf("Error Parsing User Data: %v\n", err.Error())
+	}
+
+	if err := twtxtCache.AddUser(nick, urls, "", uip, statuses); err != nil {
+		log400(w, r, "Error Adding User to Cache: "+err.Error())
+		return
+	}
+
+	log200(r)
+	_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
+	if err != nil {
+		log.Printf("%v\n", err.Error())
+	}
+}