diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-20 21:17:39 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-21 02:37:28 -0400 |
commit | 505c5d87d32a20d02efc5ed297a24f04e15765d6 (patch) | |
tree | e932ef392c0ab1dbb200551e348b0d24b3871f5e | |
parent | dec003b9f8bf7ed5b77c867e74dba061a9ec0c92 (diff) | |
download | getwtxt-505c5d87d32a20d02efc5ed297a24f04e15765d6.tar.gz |
apiEndpointHandler returning all users/statuses/mentions if no query present
-rw-r--r-- | handlers.go | 57 | ||||
-rw-r--r-- | http.go | 6 |
2 files changed, 27 insertions, 36 deletions
diff --git a/handlers.go b/handlers.go index 345e6b9..4085f73 100644 --- a/handlers.go +++ b/handlers.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "time" "github.com/gorilla/mux" ) @@ -42,45 +41,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { // handles "/api" func apiBaseHandler(w http.ResponseWriter, r *http.Request) { - - timerfc3339, err := time.Now().MarshalText() - if err != nil { - log.Printf("Couldn't format time as RFC3339: %v\n", err) - } - - etag := fmt.Sprintf("%x", sha256.Sum256(timerfc3339)) - - w.Header().Set("ETag", etag) - w.Header().Set("Content-Type", txtutf8) - - pathdata := []byte("\n\n" + r.URL.Path) - timerfc3339 = append(timerfc3339, pathdata...) - - _, err = w.Write(timerfc3339) - if err != nil { - log500(w, r, err) - return - } - - log200(r) + indexHandler(w, r) } // handles "/api/plain" // maybe add json/xml support later func apiFormatHandler(w http.ResponseWriter, r *http.Request) { - - vars := mux.Vars(r) - format := vars["format"] - - w.Header().Set("Content-Type", txtutf8) - - _, err := w.Write([]byte(format + "\n")) - if err != nil { - log500(w, r, err) - return - } - - log200(r) + indexHandler(w, r) } // handles "/api/plain/(users|mentions|tweets)" @@ -92,6 +59,7 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { return } + // if there's a query, execute it if r.FormValue("q") != "" || r.FormValue("url") != "" { err := apiEndpointQuery(w, r) if err != nil { @@ -102,9 +70,26 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { return } + // if there's no query, return everything in + // registry for a given endpoint + var out []string + if r.URL.Path == "/api/plain/users" { + out, err = twtxtCache.QueryUser("") + } else if r.URL.Path == "/api/plain/mentions" { + out, err = twtxtCache.QueryInStatus(".txt>") + } else { + out, err = twtxtCache.QueryAllStatuses() + } + if err != nil { + log500(w, r, err) + return + } + + data := parseQueryOut(out) + w.Header().Set("Content-Type", txtutf8) - _, err = w.Write([]byte(r.URL.String())) + _, err = w.Write(data) if err != nil { log500(w, r, err) return diff --git a/http.go b/http.go index 2a733aa..4b6d460 100644 --- a/http.go +++ b/http.go @@ -43,6 +43,12 @@ func log200(r *http.Request) { log.Printf("*** %v :: 200 :: %v %v\n", uip, r.Method, r.URL) } +// log output for 400s +func log400(w http.ResponseWriter, r *http.Request, err error) { + uip := getIPFromCtx(r.Context()) + log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err) +} + // log output for 404s func log404(w http.ResponseWriter, r *http.Request, err error) { |