ls 5 years ago
parent
commit
16fe973967
3 changed files with 47 additions and 19 deletions
  1. 1 0
      go.mod
  2. 6 0
      go.sum
  3. 40 19
      serve.go

+ 1 - 0
go.mod

@@ -9,5 +9,6 @@ require (
 	github.com/jmoiron/sqlx v1.2.0
 	github.com/stretchr/testify v1.3.0
 	github.com/ugorji/go v1.1.4
+	golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6
 	gopkg.in/yaml.v2 v2.2.2
 )

+ 6 - 0
go.sum

@@ -21,6 +21,12 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
 github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6 h1:FP8hkuE6yUEaJnK7O2eTuejKWwW+Rhfj80dQ2JcKxCU=
+golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 40 - 19
serve.go

@@ -8,21 +8,36 @@ import (
 	"os/signal"
 	"syscall"
 	"time"
+
+	"golang.org/x/net/http2"
+)
+
+const (
+	megaByte = 1 << 20 // 1 mb , 2 mb 1 << 21, 4mb 1 << 22, 8mb 1 << 23
+	gigaByte = 1 << 30
+)
+
+var (
+	readTimeout  = 30 * time.Second
+	writeTimeout = 30 * time.Second
 )
 
 func newServe(addr string, router http.Handler) *http.Server {
 	return &http.Server{
 		Addr:           addr,
 		Handler:        router,
-		ReadTimeout:    30 * time.Second,
-		WriteTimeout:   30 * time.Second,
-		MaxHeaderBytes: 1 << 20,
+		ReadTimeout:    readTimeout,
+		WriteTimeout:   writeTimeout,
+		MaxHeaderBytes: megaByte,
 	}
 }
 
-// ListenAndServe new server and start
-func ListenAndServe(addr string, router http.Handler) {
+func run(useV2 bool, addr, certFile, keyFile string, router http.Handler) {
 	s := newServe(addr, router)
+	if useV2 {
+		http2.VerboseLogs = false
+		http2.ConfigureServer(s, nil)
+	}
 	errc := make(chan error)
 	go func() {
 		c := make(chan os.Signal)
@@ -32,27 +47,33 @@ func ListenAndServe(addr string, router http.Handler) {
 
 	go func() {
 		log.Println("HTTP Server listen on", addr)
-		errc <- s.ListenAndServe()
+		if len(certFile) > 0 && len(keyFile) > 0 {
+			errc <- s.ListenAndServeTLS(certFile, keyFile)
+		} else {
+			errc <- s.ListenAndServe()
+		}
+
 		log.Println("Exit HTTP server", "Quit")
 	}()
 	log.Println("Exit", <-errc)
 }
 
+// ListenAndServe new server and start
+func ListenAndServe(addr string, router http.Handler) {
+	run(false, addr, ``, ``, router)
+}
+
 // ListenAndServeTLS new server and start
 func ListenAndServeTLS(addr, certFile, keyFile string, router http.Handler) {
-	s := newServe(addr, router)
+	run(false, addr, certFile, keyFile, router)
+}
 
-	errc := make(chan error)
-	go func() {
-		c := make(chan os.Signal)
-		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
-		errc <- fmt.Errorf("%s", <-c)
-	}()
+// ListenAndServeV2 new http2 server and start
+func ListenAndServeV2(addr string, router http.Handler) {
+	run(true, addr, ``, ``, router)
+}
 
-	go func() {
-		log.Println("HTTP Server listen on", addr)
-		errc <- s.ListenAndServeTLS(certFile, keyFile)
-		log.Println("Exit HTTP server", "Quit")
-	}()
-	log.Println("Exit", <-errc)
+// ListenAndServeTLSV2 new http2 server and start
+func ListenAndServeTLSV2(addr, certFile, keyFile string, router http.Handler) {
+	run(true, addr, certFile, keyFile, router)
 }