code.gitea.io/gitea@v1.22.3/modules/graceful/server_http.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package graceful
     5  
     6  import (
     7  	"context"
     8  	"crypto/tls"
     9  	"net"
    10  	"net/http"
    11  )
    12  
    13  func newHTTPServer(network, address, name string, handler http.Handler) (*Server, ServeFunction) {
    14  	server := NewServer(network, address, name)
    15  	httpServer := http.Server{
    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  
    25  // HTTPListenAndServe listens on the provided network address and then calls Serve
    26  // to handle requests on incoming connections.
    27  func HTTPListenAndServe(network, address, name string, handler http.Handler, useProxyProtocol bool) error {
    28  	server, lHandler := newHTTPServer(network, address, name, handler)
    29  	return server.ListenAndServe(lHandler, useProxyProtocol)
    30  }
    31  
    32  // HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve
    33  // to handle requests on incoming connections.
    34  func HTTPListenAndServeTLSConfig(network, address, name string, tlsConfig *tls.Config, handler http.Handler, useProxyProtocol, proxyProtocolTLSBridging bool) error {
    35  	server, lHandler := newHTTPServer(network, address, name, handler)
    36  	return server.ListenAndServeTLSConfig(tlsConfig, lHandler, useProxyProtocol, proxyProtocolTLSBridging)
    37  }