summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-12 23:36:04 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-12 23:36:04 -0400
commitd2680fb62bac6c779f371b2b6839592274a8d281 (patch)
treeae6adcc3609396a44eed63462e292c57066f1dcb
parent1f8b2ce166e104bca99e4a758bb658dd27d5e4d6 (diff)
downloadgetwtxt-d2680fb62bac6c779f371b2b6839592274a8d281.tar.gz
reorganized handlers. StrictSlash(true). handling POST /api/plain/users for new users
-rw-r--r--handlers.go14
-rw-r--r--main.go34
2 files changed, 39 insertions, 9 deletions
diff --git a/handlers.go b/handlers.go
index 3998228..a44c9d8 100644
--- a/handlers.go
+++ b/handlers.go
@@ -64,6 +64,20 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 
 }
 
+// handles POST for "/api/plain/users"
+func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+	format := vars["format"]
+	endpoint := vars["endpoint"]
+
+	w.Header().Set("Content-Type", htmlutf8)
+	n, err := w.Write([]byte(format + "/" + endpoint))
+	if err != nil || n == 0 {
+		log.Printf("Error writing to HTTP stream: %v\n", err)
+	}
+
+}
+
 // handles "/api/plain/tags"
 func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 	vars := mux.Vars(r)
diff --git a/main.go b/main.go
index ef083dc..267c4ea 100644
--- a/main.go
+++ b/main.go
@@ -14,20 +14,36 @@ const getwtxt = "0.1"
 
 func main() {
 
-	// more precise path-based routing
-	index := mux.NewRouter()
+	index := mux.NewRouter().StrictSlash(true)
 	api := index.PathPrefix("/api").Subrouter()
 
 	// gorilla/mux makes path validation painless
-	index.HandleFunc("/", indexHandler)
-	index.HandleFunc("/api", apiBaseHandler)
-	api.HandleFunc("/", apiBaseHandler)
-	api.HandleFunc("/{format:(?:plain)}", apiFormatHandler)
+	index.Path("/").
+		Methods("GET").
+		HandlerFunc(indexHandler)
+	index.Path("/api").
+		Methods("GET").
+		HandlerFunc(apiBaseHandler)
+	api.Path("/").
+		Methods("GET").
+		HandlerFunc(apiBaseHandler)
+	api.Path("/{format:(?:plain)}").
+		Methods("GET").
+		HandlerFunc(apiFormatHandler)
 	api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}").
-		Queries("url", "{url}", "q", "{query}", "nickname", "{nickname}").
+		Methods("GET").
+		Queries("url", "{url}", "q", "{query}").
 		HandlerFunc(apiEndpointHandler)
-	api.HandleFunc("/{format:(?:plain)}/tags", apiTagsBaseHandler)
-	api.HandleFunc("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9]+}", apiTagsHandler)
+	api.Path("/{format:(?:plain)}/{endpoint:users}").
+		Methods("POST").
+		Queries("url", "{url}", "nickname", "{nickname}").
+		HandlerFunc(apiEndpointPOSTHandler)
+	api.Path("/{format:(?:plain)}/tags").
+		Methods("GET").
+		HandlerFunc(apiTagsBaseHandler)
+	api.Path("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9]+}").
+		Methods("GET").
+		HandlerFunc(apiTagsHandler)
 
 	// format the port for the http.Server object
 	portnum := fmt.Sprintf(":%v", confObj.port)