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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. ReadTimeout: DefaultReadTimeOut,
  14. WriteTimeout: DefaultWriteTimeOut,
  15. MaxHeaderBytes: DefaultMaxHeaderBytes,
  16. Handler: handler,
  17. BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
  18. }
  19. server.OnShutdown = func() {
  20. httpServer.SetKeepAlivesEnabled(false)
  21. }
  22. return server, httpServer.Serve
  23. }
  24. // HTTPListenAndServe listens on the provided network address and then calls Serve
  25. // to handle requests on incoming connections.
  26. func HTTPListenAndServe(network, address, name string, handler http.Handler, useProxyProtocol bool) error {
  27. server, lHandler := newHTTPServer(network, address, name, handler)
  28. return server.ListenAndServe(lHandler, useProxyProtocol)
  29. }
  30. // HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve
  31. // to handle requests on incoming connections.
  32. func HTTPListenAndServeTLSConfig(network, address, name string, tlsConfig *tls.Config, handler http.Handler, useProxyProtocol, proxyProtocolTLSBridging bool) error {
  33. server, lHandler := newHTTPServer(network, address, name, handler)
  34. return server.ListenAndServeTLSConfig(tlsConfig, lHandler, useProxyProtocol, proxyProtocolTLSBridging)
  35. }