You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.6 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package graceful
  5. import (
  6. "crypto/tls"
  7. "net/http"
  8. )
  9. func newHTTPServer(network, address string, handler http.Handler) (*Server, ServeFunction) {
  10. server := NewServer(network, address)
  11. httpServer := http.Server{
  12. ReadTimeout: DefaultReadTimeOut,
  13. WriteTimeout: DefaultWriteTimeOut,
  14. MaxHeaderBytes: DefaultMaxHeaderBytes,
  15. Handler: handler,
  16. }
  17. server.OnShutdown = func() {
  18. httpServer.SetKeepAlivesEnabled(false)
  19. }
  20. return server, httpServer.Serve
  21. }
  22. // HTTPListenAndServe listens on the provided network address and then calls Serve
  23. // to handle requests on incoming connections.
  24. func HTTPListenAndServe(network, address string, handler http.Handler) error {
  25. server, lHandler := newHTTPServer(network, address, handler)
  26. return server.ListenAndServe(lHandler)
  27. }
  28. // HTTPListenAndServeTLS listens on the provided network address and then calls Serve
  29. // to handle requests on incoming connections.
  30. func HTTPListenAndServeTLS(network, address, certFile, keyFile string, handler http.Handler) error {
  31. server, lHandler := newHTTPServer(network, address, handler)
  32. return server.ListenAndServeTLS(certFile, keyFile, lHandler)
  33. }
  34. // HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve
  35. // to handle requests on incoming connections.
  36. func HTTPListenAndServeTLSConfig(network, address string, tlsConfig *tls.Config, handler http.Handler) error {
  37. server, lHandler := newHTTPServer(network, address, handler)
  38. return server.ListenAndServeTLSConfig(tlsConfig, lHandler)
  39. }