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.

web.go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "os"
  10. "strings"
  11. _ "net/http/pprof" // Used for debugging if enabled and a web server is running
  12. "code.gitea.io/gitea/modules/graceful"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/process"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/routers"
  17. "code.gitea.io/gitea/routers/install"
  18. "github.com/felixge/fgprof"
  19. "github.com/urfave/cli"
  20. ini "gopkg.in/ini.v1"
  21. )
  22. // CmdWeb represents the available web sub-command.
  23. var CmdWeb = cli.Command{
  24. Name: "web",
  25. Usage: "Start Gitea web server",
  26. Description: `Gitea web server is the only thing you need to run,
  27. and it takes care of all the other things for you`,
  28. Action: runWeb,
  29. Flags: []cli.Flag{
  30. cli.StringFlag{
  31. Name: "port, p",
  32. Value: "3000",
  33. Usage: "Temporary port number to prevent conflict",
  34. },
  35. cli.StringFlag{
  36. Name: "install-port",
  37. Value: "3000",
  38. Usage: "Temporary port number to run the install page on to prevent conflict",
  39. },
  40. cli.StringFlag{
  41. Name: "pid, P",
  42. Value: setting.PIDFile,
  43. Usage: "Custom pid file path",
  44. },
  45. cli.BoolFlag{
  46. Name: "quiet, q",
  47. Usage: "Only display Fatal logging errors until logging is set-up",
  48. },
  49. cli.BoolFlag{
  50. Name: "verbose",
  51. Usage: "Set initial logging to TRACE level until logging is properly set-up",
  52. },
  53. },
  54. }
  55. func runHTTPRedirector() {
  56. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Web: HTTP Redirector", process.SystemProcessType, true)
  57. defer finished()
  58. source := fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.PortToRedirect)
  59. dest := strings.TrimSuffix(setting.AppURL, "/")
  60. log.Info("Redirecting: %s to %s", source, dest)
  61. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  62. target := dest + r.URL.Path
  63. if len(r.URL.RawQuery) > 0 {
  64. target += "?" + r.URL.RawQuery
  65. }
  66. http.Redirect(w, r, target, http.StatusTemporaryRedirect)
  67. })
  68. err := runHTTP("tcp", source, "HTTP Redirector", handler, setting.RedirectorUseProxyProtocol)
  69. if err != nil {
  70. log.Fatal("Failed to start port redirection: %v", err)
  71. }
  72. }
  73. func runWeb(ctx *cli.Context) error {
  74. if ctx.Bool("verbose") {
  75. _ = log.DelLogger("console")
  76. log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "trace", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
  77. } else if ctx.Bool("quiet") {
  78. _ = log.DelLogger("console")
  79. log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "fatal", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
  80. }
  81. defer func() {
  82. if panicked := recover(); panicked != nil {
  83. log.Fatal("PANIC: %v\n%s", panicked, log.Stack(2))
  84. }
  85. }()
  86. managerCtx, cancel := context.WithCancel(context.Background())
  87. graceful.InitManager(managerCtx)
  88. defer cancel()
  89. if os.Getppid() > 1 && len(os.Getenv("LISTEN_FDS")) > 0 {
  90. log.Info("Restarting Gitea on PID: %d from parent PID: %d", os.Getpid(), os.Getppid())
  91. } else {
  92. log.Info("Starting Gitea on PID: %d", os.Getpid())
  93. }
  94. // Set pid file setting
  95. if ctx.IsSet("pid") {
  96. setting.PIDFile = ctx.String("pid")
  97. setting.WritePIDFile = true
  98. }
  99. // Perform pre-initialization
  100. needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
  101. if needsInstall {
  102. // Flag for port number in case first time run conflict
  103. if ctx.IsSet("port") {
  104. if err := setPort(ctx.String("port")); err != nil {
  105. return err
  106. }
  107. }
  108. if ctx.IsSet("install-port") {
  109. if err := setPort(ctx.String("install-port")); err != nil {
  110. return err
  111. }
  112. }
  113. installCtx, cancel := context.WithCancel(graceful.GetManager().HammerContext())
  114. c := install.Routes(installCtx)
  115. err := listen(c, false)
  116. cancel()
  117. if err != nil {
  118. log.Critical("Unable to open listener for installer. Is Gitea already running?")
  119. graceful.GetManager().DoGracefulShutdown()
  120. }
  121. select {
  122. case <-graceful.GetManager().IsShutdown():
  123. <-graceful.GetManager().Done()
  124. log.Info("PID: %d Gitea Web Finished", os.Getpid())
  125. log.Close()
  126. return err
  127. default:
  128. }
  129. } else {
  130. NoInstallListener()
  131. }
  132. if setting.EnablePprof {
  133. go func() {
  134. http.DefaultServeMux.Handle("/debug/fgprof", fgprof.Handler())
  135. _, _, finished := process.GetManager().AddTypedContext(context.Background(), "Web: PProf Server", process.SystemProcessType, true)
  136. // The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment it's not worth to introduce a configurable option for it.
  137. log.Info("Starting pprof server on localhost:6060")
  138. log.Info("Stopped pprof server: %v", http.ListenAndServe("localhost:6060", nil))
  139. finished()
  140. }()
  141. }
  142. log.Info("Global init")
  143. // Perform global initialization
  144. setting.LoadFromExisting()
  145. routers.GlobalInitInstalled(graceful.GetManager().HammerContext())
  146. // We check that AppDataPath exists here (it should have been created during installation)
  147. // We can't check it in `GlobalInitInstalled`, because some integration tests
  148. // use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during those tests.
  149. if _, err := os.Stat(setting.AppDataPath); err != nil {
  150. log.Fatal("Can not find APP_DATA_PATH '%s'", setting.AppDataPath)
  151. }
  152. // Override the provided port number within the configuration
  153. if ctx.IsSet("port") {
  154. if err := setPort(ctx.String("port")); err != nil {
  155. return err
  156. }
  157. }
  158. // Set up Chi routes
  159. c := routers.NormalRoutes(graceful.GetManager().HammerContext())
  160. err := listen(c, true)
  161. <-graceful.GetManager().Done()
  162. log.Info("PID: %d Gitea Web Finished", os.Getpid())
  163. log.Close()
  164. return err
  165. }
  166. func setPort(port string) error {
  167. setting.AppURL = strings.Replace(setting.AppURL, setting.HTTPPort, port, 1)
  168. setting.HTTPPort = port
  169. switch setting.Protocol {
  170. case setting.HTTPUnix:
  171. case setting.FCGI:
  172. case setting.FCGIUnix:
  173. default:
  174. defaultLocalURL := string(setting.Protocol) + "://"
  175. if setting.HTTPAddr == "0.0.0.0" {
  176. defaultLocalURL += "localhost"
  177. } else {
  178. defaultLocalURL += setting.HTTPAddr
  179. }
  180. defaultLocalURL += ":" + setting.HTTPPort + "/"
  181. // Save LOCAL_ROOT_URL if port changed
  182. setting.CreateOrAppendToCustomConf("server.LOCAL_ROOT_URL", func(cfg *ini.File) {
  183. cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
  184. })
  185. }
  186. return nil
  187. }
  188. func listen(m http.Handler, handleRedirector bool) error {
  189. listenAddr := setting.HTTPAddr
  190. if setting.Protocol != setting.HTTPUnix && setting.Protocol != setting.FCGIUnix {
  191. listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort)
  192. }
  193. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Web: Gitea Server", process.SystemProcessType, true)
  194. defer finished()
  195. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
  196. // This can be useful for users, many users do wrong to their config and get strange behaviors behind a reverse-proxy.
  197. // A user may fix the configuration mistake when he sees this log.
  198. // And this is also very helpful to maintainers to provide help to users to resolve their configuration problems.
  199. log.Info("AppURL(ROOT_URL): %s", setting.AppURL)
  200. if setting.LFS.StartServer {
  201. log.Info("LFS server enabled")
  202. }
  203. var err error
  204. switch setting.Protocol {
  205. case setting.HTTP:
  206. if handleRedirector {
  207. NoHTTPRedirector()
  208. }
  209. err = runHTTP("tcp", listenAddr, "Web", m, setting.UseProxyProtocol)
  210. case setting.HTTPS:
  211. if setting.EnableAcme {
  212. err = runACME(listenAddr, m)
  213. break
  214. }
  215. if handleRedirector {
  216. if setting.RedirectOtherPort {
  217. go runHTTPRedirector()
  218. } else {
  219. NoHTTPRedirector()
  220. }
  221. }
  222. err = runHTTPS("tcp", listenAddr, "Web", setting.CertFile, setting.KeyFile, m, setting.UseProxyProtocol, setting.ProxyProtocolTLSBridging)
  223. case setting.FCGI:
  224. if handleRedirector {
  225. NoHTTPRedirector()
  226. }
  227. err = runFCGI("tcp", listenAddr, "FCGI Web", m, setting.UseProxyProtocol)
  228. case setting.HTTPUnix:
  229. if handleRedirector {
  230. NoHTTPRedirector()
  231. }
  232. err = runHTTP("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
  233. case setting.FCGIUnix:
  234. if handleRedirector {
  235. NoHTTPRedirector()
  236. }
  237. err = runFCGI("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
  238. default:
  239. log.Fatal("Invalid protocol: %s", setting.Protocol)
  240. }
  241. if err != nil {
  242. log.Critical("Failed to start server: %v", err)
  243. }
  244. log.Info("HTTP Listener: %s Closed", listenAddr)
  245. return err
  246. }