summary refs log tree commit diff stats
path: root/README
Commit message (Expand)AuthorAgeFilesLines
* README: typohut2010-03-121-2/+2
* more documentationhut2010-03-121-3/+10
* README: slightly changed introductionhut2010-03-121-3/+3
* README: changed introductionhut2010-03-121-8/+10
* README: updatedhut2010-03-121-16/+19
* incremented version number and updated pydoc html files v1.0.3hut2010-02-161-2/+2
* readme: normalized letter casing in headingshut2010-02-151-2/+2
* updated READMEhut2010-02-151-29/+40
* made configuration more simplehut2010-01-251-6/+2
* README: corrected bad instructionshut2010-01-241-2/+2
* readme: improvedhut2010-01-201-6/+13
* 1.0.2! v1.0.2hut2010-01-141-2/+2
* readme: added part "customizing ranger"hut2010-01-141-0/+16
* readme: removed ugly sentencehut2010-01-141-3/+1
* new minor version v1.0.1hut2010-01-021-2/+2
* readme: updatedhut2010-01-011-4/+77
* decided to use python from now onhut2009-11-221-1/+1
* Initial commithut2009-11-161-0/+11
ben@gbmor.dev> 2019-05-21 03:42:17 -0400 fleshed out POST handler, added remote registry list' href='/gbmor/getwtxt/commit/post.go?h=v0.4.1&id=df1d1efa19aed5bc6553c7c0a0b4b7dfe20e3bd0'>df1d1ef ^
55c8ffa ^
d8e4c8a ^
df1d1ef ^





4f0847b ^
df1d1ef ^
b961a70 ^
df1d1ef ^

db1dc5d ^
d8e4c8a ^
df1d1ef ^



bce5265 ^

b961a70 ^
bce5265 ^
df1d1ef ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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())
	}
}