package myth import ( "fmt" "log" "net/http" "os" "os/signal" "syscall" "time" "golang.org/x/net/http2" ) 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: readTimeout, WriteTimeout: writeTimeout, MaxHeaderBytes: MB, } } 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) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) errc <- fmt.Errorf("%s", <-c) }() go func() { log.Println("HTTP Server listen on", addr) 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) { run(false, addr, certFile, keyFile, router) } // ListenAndServeV2 new http2 server and start func ListenAndServeV2(addr string, router http.Handler) { run(true, addr, ``, ``, router) } // ListenAndServeTLSV2 new http2 server and start func ListenAndServeTLSV2(addr, certFile, keyFile string, router http.Handler) { run(true, addr, certFile, keyFile, router) }