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.

server_http.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package graceful
  4. import (
  5. "context"
  6. "crypto/tls"
  7. "net"
  8. "net/http"
  9. )
  10. func newHTTPServer(network, address, name string, handler http.Handler) (*Server, ServeFunction) {
  11. server := NewServer(network, address, name)
  12. httpServer := http.Server{
  13. Handler: handler,
  14. BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
  15. }
  16. server.OnShutdown = func() {
  17. httpServer.SetKeepAlivesEnabled(false)
  18. }
  19. return server, httpServer.Serve
  20. }
  21. // HTTPListenAndServe listens on the provided network address and then calls Serve
  22. // to handle requests on incoming connections.
  23. func HTTPListenAndServe(network, address, name string, handler http.Handler, useProxyProtocol bool) error {
  24. server, lHandler := newHTTPServer(network, address, name, handler)
  25. return server.ListenAndServe(lHandler, useProxyProtocol)
  26. }
  27. // HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve
  28. // to handle requests on incoming connections.
  29. func HTTPListenAndServeTLSConfig(network, address, name string, tlsConfig *tls.Config, handler http.Handler, useProxyProtocol, proxyProtocolTLSBridging bool) error {
  30. server, lHandler := newHTTPServer(network, address, name, handler)
  31. return server.ListenAndServeTLSConfig(tlsConfig, lHandler, useProxyProtocol, proxyProtocolTLSBridging)
  32. }