Browse Source

Ability to enable/disable server keep alive

Vova Tkach 6 years ago
parent
commit
f6ed30f68e
3 changed files with 11 additions and 2 deletions
  1. 1 1
      Dockerfile
  2. 1 0
      consts/consts.go
  3. 9 1
      main.go

+ 1 - 1
Dockerfile

@@ -1,6 +1,6 @@
 FROM debian:latest
 
-ENV FAVE_HOST=0.0.0.0 FAVE_PORT=8080 FAVE_DIR=/app/hosts FAVE_DEBUG=false
+ENV FAVE_HOST=0.0.0.0 FAVE_PORT=8080 FAVE_DIR=/app/hosts FAVE_DEBUG=false FAVE_KEEPALIVE=false
 
 ADD https://github.com/vladimirok5959/golang-fave/releases/download/v1.0.0/fave.linux-amd64.tar.gz /app/fave.linux-amd64.tar.gz
 ADD https://github.com/vladimirok5959/golang-fave/releases/download/v1.0.0/localhost.tar.gz /app/hosts/localhost.tar.gz

+ 1 - 0
consts/consts.go

@@ -29,6 +29,7 @@ var ParamHost string
 var ParamPort int
 var ParamWwwDir string
 var ParamDebug bool
+var ParamKeepAlive bool
 
 // For admin panel
 type BreadCrumb struct {

+ 9 - 1
main.go

@@ -25,6 +25,7 @@ func init() {
 	flag.IntVar(&consts.ParamPort, "port", 8080, "server port")
 	flag.StringVar(&consts.ParamWwwDir, "dir", "", "virtual hosts directory")
 	flag.BoolVar(&consts.ParamDebug, "debug", false, "debug mode with ignoring log files")
+	flag.BoolVar(&consts.ParamKeepAlive, "keepalive", false, "enable/disable server keep alive")
 	flag.Parse()
 }
 
@@ -50,6 +51,11 @@ func main() {
 			consts.ParamDebug = true
 		}
 	}
+	if consts.ParamKeepAlive == false {
+		if os.Getenv("FAVE_KEEPALIVE") == "true" {
+			consts.ParamKeepAlive = true
+		}
+	}
 
 	// Check www dir
 	consts.ParamWwwDir = utils.FixPath(consts.ParamWwwDir)
@@ -146,5 +152,7 @@ func main() {
 
 		// Error 404
 		utils.SystemErrorPage404(w)
-	}, nil)
+	}, func(s *http.Server) {
+		s.SetKeepAlivesEnabled(consts.ParamKeepAlive)
+	})
 }