From f86e9b161aff35749e1213cd485d4de7c0da20e6 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Mon, 10 Jun 2019 01:01:37 -0400 Subject: Start() broken into multiple functions --- svc/svc.go | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/svc/svc.go b/svc/svc.go index 4d781eb..102e1ca 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -20,6 +20,34 @@ func Start() { index := mux.NewRouter().StrictSlash(true) api := index.PathPrefix("/api").Subrouter() + setIndexRouting(index) + setEndpointRouting(api) + + confObj.Mu.RLock() + portnum := fmt.Sprintf(":%v", confObj.Port) + confObj.Mu.RUnlock() + + server := newServer(portnum, index) + + log.Printf("\nListening on %v\n", portnum) + log.Printf("\n:: getwtxt %v Started :: %v ::\n\n", Vers, time.Now().Format(time.RFC3339)) + errLog("", server.ListenAndServe()) + + closeLog <- true +} + +func newServer(port string, index *mux.Router) *http.Server { + // handlers.CompressHandler gzips all responses. + // Write/Read timeouts are self explanatory. + return &http.Server{ + Handler: handlers.CompressHandler(ipMiddleware(index)), + Addr: port, + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } +} + +func setIndexRouting(index *mux.Router) { index.Path("/"). Methods("GET", "HEAD"). HandlerFunc(indexHandler) @@ -31,7 +59,9 @@ func Start() { index.Path("/api"). Methods("GET", "HEAD"). HandlerFunc(apiBaseHandler) +} +func setEndpointRouting(api *mux.Router) { // twtxt will add support for other formats later. // Maybe json? Making this future-proof. api.Path("/{format:(?:plain)}"). @@ -97,23 +127,4 @@ func Start() { Queries("page", "{[0-9]+}"). Methods("GET", "HEAD"). HandlerFunc(apiTagsHandler) - - confObj.Mu.RLock() - portnum := fmt.Sprintf(":%v", confObj.Port) - confObj.Mu.RUnlock() - - // handlers.CompressHandler gzips all responses. - // Write/Read timeouts are self explanatory. - server := &http.Server{ - Handler: handlers.CompressHandler(ipMiddleware(index)), - Addr: portnum, - WriteTimeout: 15 * time.Second, - ReadTimeout: 15 * time.Second, - } - - log.Printf("\nListening on %v\n", portnum) - log.Printf("\n:: getwtxt %v Started :: %v ::\n\n", Vers, time.Now().Format(time.RFC3339)) - errLog("", server.ListenAndServe()) - - closeLog <- true } -- cgit 1.4.1-2-gfad0