diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-11 03:32:00 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-11 03:32:00 -0400 |
commit | d4c05d21a4de2fe8f492957b66f40aa5e6757dca (patch) | |
tree | 620378cd1968cf31718c7ba1a03939f5411ac463 | |
parent | 474ffad421eee04a50cbdd5cfbc2b396c24dc521 (diff) | |
download | getwtxt-d4c05d21a4de2fe8f492957b66f40aa5e6757dca.tar.gz |
skeleton handlers responding
-rw-r--r-- | handlers.go | 42 | ||||
-rw-r--r-- | main.go | 14 | ||||
-rw-r--r-- | types.go | 3 |
3 files changed, 51 insertions, 8 deletions
diff --git a/handlers.go b/handlers.go index 4b2ceef..ac52559 100644 --- a/handlers.go +++ b/handlers.go @@ -6,6 +6,8 @@ import ( "log" "net/http" "time" + + "github.com/gorilla/mux" ) func apiBaseHandler(w http.ResponseWriter, r *http.Request) { @@ -15,7 +17,7 @@ func apiBaseHandler(w http.ResponseWriter, r *http.Request) { } etag := fmt.Sprintf("%x", sha256.Sum256(timerfc3339)) w.Header().Set("ETag", etag) - w.Header().Set("Content-Type", textutf8) + w.Header().Set("Content-Type", txtutf8) pathdata := []byte("\n\n" + r.URL.Path) timerfc3339 = append(timerfc3339, pathdata...) n, err := w.Write(timerfc3339) @@ -25,17 +27,55 @@ func apiBaseHandler(w http.ResponseWriter, r *http.Request) { } func indexHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", htmlutf8) + n, err := w.Write([]byte(getwtxt)) + if err != nil || n == 0 { + log.Printf("Error writing to HTTP stream: %v\n", err) + } } func apiFormatHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + format := vars["format"] + w.Header().Set("Content-Type", txtutf8) + n, err := w.Write([]byte(format + "\n")) + if err != nil || n == 0 { + log.Printf("Error writing to HTTP stream: %v\n", err) + } } func apiEndpointHandler(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) + } } func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + format := vars["format"] + + w.Header().Set("Content-Type", htmlutf8) + n, err := w.Write([]byte("api/" + format + "/tags")) + if err != nil || n == 0 { + log.Printf("Error writing to HTTP stream: %v\n", err) + } } func apiTagsHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + format := vars["format"] + tags := vars["tags"] + + w.Header().Set("Content-Type", htmlutf8) + n, err := w.Write([]byte("api/" + format + "/" + tags)) + if err != nil || n == 0 { + log.Printf("Error writing to HTTP stream: %v\n", err) + } } diff --git a/main.go b/main.go index 06d124a..d8a0614 100644 --- a/main.go +++ b/main.go @@ -8,17 +8,19 @@ import ( "github.com/gorilla/mux" ) +const getwtxt = "0.1" + func main() { - log.Printf("getwtxt v0.1\n") + log.Printf("getwtxt " + getwtxt + "\n") serv := mux.NewRouter() serv.HandleFunc("/", indexHandler) - serv.HandleFunc("/api/", apiBaseHandler) - serv.HandleFunc("/api/{format:plain}", apiFormatHandler) - serv.HandleFunc("/api/{format:plain}/{endpoint:mentions|users|tweets}", apiEndpointHandler) - serv.HandleFunc("/api/{format:plain}/tags/{tags:[a-zA-Z0-9]+}", apiTagsHandler) - serv.HandleFunc("/api/{format:plain}/tags", apiTagsBaseHandler) + serv.HandleFunc("/{api:(?:api|api/)}", apiBaseHandler) + serv.HandleFunc("/api/{format:(?:plain|plain/)}", apiFormatHandler) + serv.HandleFunc("/api/{format:(?:plain)}/{endpoint:(?:mentions|mentions/|users|users/|tweets|tweets/)}", apiEndpointHandler) + serv.HandleFunc("/api/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9]+}", apiTagsHandler) + serv.HandleFunc("/api/{format:(?:plain)}/{tagpathfixer:(?:tags|tags/)}", apiTagsBaseHandler) log.Fatalln(http.ListenAndServe(":8080", handlers.CompressHandler(serv))) } diff --git a/types.go b/types.go index fa9b8aa..7cfa7d6 100644 --- a/types.go +++ b/types.go @@ -2,7 +2,8 @@ package main import "regexp" -const textutf8 = "text/plain; charset=utf8" +const txtutf8 = "text/plain; charset=utf8" +const htmlutf8 = "text/html; charset=utf8" type configuration struct { port string |