package xhttp import ( "context" "fmt" "net/http" "os" "os/signal" "syscall" //"golang.org/x/net/http2" ) func run(addr string, router http.Handler, option ServerOption) (errs, err error) { s := &http.Server{ Addr: addr, Handler: router, ReadTimeout: option.ReadTimeout, ReadHeaderTimeout: option.ReadHeaderTimeout, WriteTimeout: option.WriteTimeout, IdleTimeout: option.IdleTimeout, MaxHeaderBytes: option.MaxHeaderBytes, } errc := make(chan error) go func() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) errc <- fmt.Errorf("%s", <-c) }() go func() { if len(option.CertFile) > 0 && len(option.KeyFile) > 0 { /* http2.VerboseLogs = false http2.ConfigureServer(s, nil) // */ errs = s.ListenAndServeTLS(option.CertFile, option.KeyFile) } else { errs = s.ListenAndServe() } errc <- errs }() err = <-errc ctx, cancel := context.WithTimeout(context.Background(), option.ShutdownTimeout) defer cancel() s.Shutdown(ctx) return } // ListenAndServe new server and start func ListenAndServe(addr string, router http.Handler, option ServerOption) (es, e error) { return run(addr, router, option) } // ListenAndServeV2 new http2 server and start func ListenAndServeV2(addr string, router http.Handler, option ServerOption) (es, e error) { return run(addr, router, option) }