Add timeouts to http server

This commit is contained in:
Marcos Lilljedahl
2017-06-02 16:16:45 -03:00
parent db1c06a99f
commit af197ce7c5
2 changed files with 10 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
FROM golang:1.7 FROM golang:1.8
# Copy the runtime dockerfile into the context as Dockerfile # Copy the runtime dockerfile into the context as Dockerfile
COPY Dockerfile.run /go/bin/Dockerfile COPY Dockerfile.run /go/bin/Dockerfile

10
api.go
View File

@@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
gh "github.com/gorilla/handlers" gh "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@@ -104,9 +105,16 @@ func main() {
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter)))) r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
n.UseHandler(r) n.UseHandler(r)
httpServer := http.Server{
Addr: "0.0.0.0:" + config.PortNumber,
Handler: n,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
}
go func() { go func() {
log.Println("Listening on port " + config.PortNumber) log.Println("Listening on port " + config.PortNumber)
log.Fatal(http.ListenAndServe("0.0.0.0:"+config.PortNumber, n)) log.Fatal(httpServer.ListenAndServe())
}() }()
// Now listen for TLS connections that need to be proxied // Now listen for TLS connections that need to be proxied