about summary refs log tree commit diff stats
path: root/main.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-11 04:08:05 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-11 04:08:05 -0400
commitc87acad59c4436554e60ab14870b77e49046c84a (patch)
tree0170c7e8d0767c53f3017ecbf49ed2ef35c3ae45 /main.go
parentd62c82f30506baa430fb7e3e117f007cfa66ac13 (diff)
downloadgetwtxt-c87acad59c4436554e60ab14870b77e49046c84a.tar.gz
more graceful routing of http requests
Diffstat (limited to 'main.go')
-rw-r--r--main.go31
1 files changed, 23 insertions, 8 deletions
diff --git a/main.go b/main.go
index d8a0614..9cc1337 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@ package main
 import (
 	"log"
 	"net/http"
+	"time"
 
 	"github.com/gorilla/handlers"
 	"github.com/gorilla/mux"
@@ -10,17 +11,31 @@ import (
 
 const getwtxt = "0.1"
 
+var closelog = make(chan bool)
+
 func main() {
 	log.Printf("getwtxt " + getwtxt + "\n")
 
-	serv := mux.NewRouter()
+	index := mux.NewRouter()
+	api := index.PathPrefix("/api").Subrouter()
+
+	index.HandleFunc("/", indexHandler)
+	api.HandleFunc("/", apiBaseHandler)
+	api.HandleFunc("/{format:(?:plain)}", apiFormatHandler)
+	api.HandleFunc("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}", apiEndpointHandler)
+	api.HandleFunc("/{format:(?:plain)}/tags", apiTagsBaseHandler)
+	api.HandleFunc("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9]+}", apiTagsHandler)
 
-	serv.HandleFunc("/", indexHandler)
-	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)
+	server := &http.Server{
+		Handler:      handlers.CompressHandler(index),
+		Addr:         ":9001",
+		WriteTimeout: 15 * time.Second,
+		ReadTimeout:  15 * time.Second,
+	}
 
-	log.Fatalln(http.ListenAndServe(":8080", handlers.CompressHandler(serv)))
+	err := server.ListenAndServe()
+	if err != nil {
+		log.Printf("%v\n", err)
+	}
+	closelog <- true
 }