From 84e21b163e8959395092692eac44c37ccdf1f524 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 6 Aug 2017 23:44:42 +0200 Subject: [PATCH] Added timeouts, fixed last route not sending data --- router.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index 1719c5d..f9f0816 100644 --- a/router.go +++ b/router.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "strings" + "time" "git.aiterp.net/gisle/notebook3/session" @@ -71,6 +72,7 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(404) + w.Write([]byte("Not Found: " + req.URL.Path)) } func (router *Router) Resource(mount string, list, create Func, get, update, delete IDFunc) { @@ -82,5 +84,13 @@ func (router *Router) Static(mount string, filePath string) { } func (router *Router) Listen(host string, port int) error { - return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), router) + srv := &http.Server{ + Addr: fmt.Sprintf("%s:%d", host, port), + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 120 * time.Second, + Handler: router, + } + + return srv.ListenAndServe() }