diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-22 02:50:34 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-22 03:21:29 -0400 |
commit | 37bf8b04e76ea1cce678238af48ac8097348e655 (patch) | |
tree | 5b3e2e38435fc3f51f2ee55a5684d4a31ce4b56b | |
parent | 29c35a301b66f0bc7fe2fb102ac2548ca9b522c9 (diff) | |
download | getwtxt-37bf8b04e76ea1cce678238af48ac8097348e655.tar.gz |
tuning query handling
-rw-r--r-- | handlers.go | 13 | ||||
-rw-r--r-- | main.go | 17 | ||||
-rw-r--r-- | query.go | 33 |
3 files changed, 48 insertions, 15 deletions
diff --git a/handlers.go b/handlers.go index 947c96a..9dc7192 100644 --- a/handlers.go +++ b/handlers.go @@ -136,25 +136,20 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) tags := vars["tags"] - out, err := twtxtCache.QueryInStatus("#" + tags) - if err != nil { - log500(w, r, err) - return - } tags = strings.ToLower(tags) - out2, err := twtxtCache.QueryInStatus("#" + tags) + out, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return } tags = strings.Title(tags) - out3, err := twtxtCache.QueryInStatus("#" + tags) + out2, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return } tags = strings.ToUpper(tags) - out4, err := twtxtCache.QueryInStatus("#" + tags) + out3, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return @@ -162,7 +157,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { out = append(out, out2...) out = append(out, out3...) - out = append(out, out4...) + out = uniq(out) data := parseQueryOut(out) diff --git a/main.go b/main.go index 61acb42..43c204b 100644 --- a/main.go +++ b/main.go @@ -51,12 +51,27 @@ func main() { Methods("GET"). HandlerFunc(apiEndpointHandler) - // This is for submitting new users + // This is for submitting new users. Both query variables must exist + // in the request for this to match. api.Path("/{format:(?:plain)}/{endpoint:users}"). Queries("url", "{url}", "nickname", "{nickname:[a-zA-Z0-9_-]+}"). Methods("POST"). HandlerFunc(apiEndpointPOSTHandler) + // This is for submitting new users incorrectly + // and letting the requester know about their error. + api.Path("/{format:(?:plain)}/{endpoint:users}"). + Queries("url", "{url}"). + Methods("POST"). + HandlerFunc(apiEndpointPOSTHandler) + + // This is for submitting new users incorrectly + // and letting the requester know about their error. + api.Path("/{format:(?:plain)}/{endpoint:users}"). + Queries("nickname", "{nickname:[a-zA-Z0-9_-]+}"). + Methods("POST"). + HandlerFunc(apiEndpointPOSTHandler) + // Show all observed tags api.Path("/{format:(?:plain)}/tags"). Methods("GET"). diff --git a/query.go b/query.go index 156ccda..ac46223 100644 --- a/query.go +++ b/query.go @@ -34,6 +34,19 @@ func parseQueryOut(out []string) []byte { return data } +// Removes duplicate statuses from query output +func uniq(str []string) []string { + keys := make(map[string]bool) + out := []string{} + for _, e := range str { + if _, ok := keys[e]; !ok { + keys[e] = true + out = append(out, e) + } + } + return out +} + // apiUserQuery is called via apiEndpointHandler when // the endpoint is "users" and r.FormValue("q") is not empty. // It queries the registry cache for users or user URLs @@ -42,7 +55,6 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { query := r.FormValue("q") urls := r.FormValue("url") var out []string - var out2 []string var err error vars := mux.Vars(r) @@ -56,13 +68,11 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { switch endpoint { case "users": if query != "" { - out2, err = twtxtCache.QueryUser(query) - out = append(out, out2...) + out, err = twtxtCache.QueryUser(query) apiErrCheck(err, r) } if urls != "" { - out2, err = twtxtCache.QueryUser(urls) - out = append(out, out2...) + out, err = twtxtCache.QueryUser(urls) apiErrCheck(err, r) } @@ -75,9 +85,22 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { apiErrCheck(err, r) case "tweets": + query = strings.ToLower(query) out, err = twtxtCache.QueryInStatus(query) apiErrCheck(err, r) + query = strings.Title(query) + out2, err := twtxtCache.QueryInStatus(query) + apiErrCheck(err, r) + + query = strings.ToUpper(query) + out3, err := twtxtCache.QueryInStatus(query) + apiErrCheck(err, r) + + out = append(out, out2...) + out = append(out, out3...) + out = uniq(out) + default: return fmt.Errorf("endpoint query, no cases match") } |