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.

47 lines
1.7 KiB

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