summary refs log tree commit diff stats
path: root/post.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-20 22:52:50 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 03:42:17 -0400
commitdf1d1efa19aed5bc6553c7c0a0b4b7dfe20e3bd0 (patch)
tree2a60f30829b8d1fca943aa3ca4a077cb4cb3517d /post.go
parenta3c67f1ff9ed2cff44930f6bfd17a3ec272fe2f5 (diff)
downloadgetwtxt-df1d1efa19aed5bc6553c7c0a0b4b7dfe20e3bd0.tar.gz
fleshed out POST handler, added remote registry list
Diffstat (limited to 'post.go')
-rw-r--r--post.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/post.go b/post.go
new file mode 100644
index 0000000..5091a0e
--- /dev/null
+++ b/post.go
@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+	"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, err)
+		return
+	}
+	nick := r.FormValue("nickname")
+	urls := r.FormValue("url")
+	if nick == "" || urls == "" {
+		log400(w, r, fmt.Errorf("nickname or URL missing"))
+		return
+	}
+
+	uip := getIPFromCtx(r.Context())
+
+	out, remoteRegistry, err := registry.GetTwtxt(urls)
+	if err != nil {
+		log400(w, r, err)
+		return
+	}
+
+	if remoteRegistry {
+		remote.Mu.Lock()
+		remote.List = append(remote.List, urls)
+		remote.Mu.Unlock()
+
+		err := twtxtCache.ScrapeRemoteRegistry(urls)
+		if err != nil {
+			log400(w, r, err)
+			return
+		}
+		log200(r)
+		return
+	}
+
+	statuses, err := registry.ParseUserTwtxt(out)
+	if err != nil {
+		log400(w, r, err)
+		return
+	}
+
+	err = twtxtCache.AddUser(nick, urls, uip, statuses)
+	if err != nil {
+		log400(w, r, err)
+		return
+	}
+
+	log200(r)
+}