code.gitea.io/gitea@v1.21.7/cmd/web_graceful.go (about)

     1  // Copyright 2016 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"net"
     8  	"net/http"
     9  	"net/http/fcgi"
    10  	"strings"
    11  
    12  	"code.gitea.io/gitea/modules/graceful"
    13  	"code.gitea.io/gitea/modules/log"
    14  	"code.gitea.io/gitea/modules/setting"
    15  )
    16  
    17  func runHTTP(network, listenAddr, name string, m http.Handler, useProxyProtocol bool) error {
    18  	return graceful.HTTPListenAndServe(network, listenAddr, name, m, useProxyProtocol)
    19  }
    20  
    21  // NoHTTPRedirector tells our cleanup routine that we will not be using a fallback http redirector
    22  func NoHTTPRedirector() {
    23  	graceful.GetManager().InformCleanup()
    24  }
    25  
    26  // NoMainListener tells our cleanup routine that we will not be using a possibly provided listener
    27  // for our main HTTP/HTTPS service
    28  func NoMainListener() {
    29  	graceful.GetManager().InformCleanup()
    30  }
    31  
    32  // NoInstallListener tells our cleanup routine that we will not be using a possibly provided listener
    33  // for our install HTTP/HTTPS service
    34  func NoInstallListener() {
    35  	graceful.GetManager().InformCleanup()
    36  }
    37  
    38  func runFCGI(network, listenAddr, name string, m http.Handler, useProxyProtocol bool) error {
    39  	// This needs to handle stdin as fcgi point
    40  	fcgiServer := graceful.NewServer(network, listenAddr, name)
    41  
    42  	err := fcgiServer.ListenAndServe(func(listener net.Listener) error {
    43  		return fcgi.Serve(listener, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
    44  			if setting.AppSubURL != "" {
    45  				req.URL.Path = strings.TrimPrefix(req.URL.Path, setting.AppSubURL)
    46  			}
    47  			m.ServeHTTP(resp, req)
    48  		}))
    49  	}, useProxyProtocol)
    50  	if err != nil {
    51  		log.Fatal("Failed to start FCGI main server: %v", err)
    52  	}
    53  	log.Info("FCGI Listener: %s Closed", listenAddr)
    54  	return err
    55  }