本站源代码
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.

127 lines
3.9KB

  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/models/migrations"
  10. "code.gitea.io/gitea/modules/cache"
  11. "code.gitea.io/gitea/modules/cron"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/highlight"
  14. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/markup"
  17. "code.gitea.io/gitea/modules/markup/external"
  18. "code.gitea.io/gitea/modules/notification"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/ssh"
  21. "code.gitea.io/gitea/modules/task"
  22. "code.gitea.io/gitea/services/mailer"
  23. mirror_service "code.gitea.io/gitea/services/mirror"
  24. "gitea.com/macaron/macaron"
  25. )
  26. func checkRunMode() {
  27. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  28. case "prod":
  29. macaron.Env = macaron.PROD
  30. macaron.ColorLog = false
  31. setting.ProdMode = true
  32. default:
  33. git.Debug = true
  34. }
  35. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  36. }
  37. // NewServices init new services
  38. func NewServices() {
  39. setting.NewServices()
  40. mailer.NewContext()
  41. _ = cache.NewContext()
  42. notification.NewContext()
  43. }
  44. // In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
  45. func initDBEngine() (err error) {
  46. log.Info("Beginning ORM engine initialization.")
  47. for i := 0; i < setting.Database.DBConnectRetries; i++ {
  48. log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
  49. if err = models.NewEngine(migrations.Migrate); err == nil {
  50. break
  51. } else if i == setting.Database.DBConnectRetries-1 {
  52. return err
  53. }
  54. log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
  55. log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
  56. time.Sleep(setting.Database.DBConnectBackoff)
  57. }
  58. models.HasEngine = true
  59. return nil
  60. }
  61. // GlobalInit is for global configuration reload-able.
  62. func GlobalInit() {
  63. setting.NewContext()
  64. if err := git.Init(); err != nil {
  65. log.Fatal("Git module init failed: %v", err)
  66. }
  67. setting.CheckLFSVersion()
  68. log.Trace("AppPath: %s", setting.AppPath)
  69. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  70. log.Trace("Custom path: %s", setting.CustomPath)
  71. log.Trace("Log path: %s", setting.LogRootPath)
  72. NewServices()
  73. if setting.InstallLock {
  74. highlight.NewContext()
  75. external.RegisterParsers()
  76. markup.Init()
  77. if err := initDBEngine(); err == nil {
  78. log.Info("ORM engine initialization successful!")
  79. } else {
  80. log.Fatal("ORM engine initialization failed: %v", err)
  81. }
  82. if err := models.InitOAuth2(); err != nil {
  83. log.Fatal("Failed to initialize OAuth2 support: %v", err)
  84. }
  85. models.NewRepoContext()
  86. // Booting long running goroutines.
  87. cron.NewContext()
  88. issue_indexer.InitIssueIndexer(false)
  89. models.InitRepoIndexer()
  90. mirror_service.InitSyncMirrors()
  91. models.InitDeliverHooks()
  92. models.InitTestPullRequests()
  93. if err := task.Init(); err != nil {
  94. log.Fatal("Failed to initialize task scheduler: %v", err)
  95. }
  96. }
  97. if setting.EnableSQLite3 {
  98. log.Info("SQLite3 Supported")
  99. }
  100. checkRunMode()
  101. // Now because Install will re-run GlobalInit once it has set InstallLock
  102. // we can't tell if the ssh port will remain unused until that's done.
  103. // However, see FIXME comment in install.go
  104. if setting.InstallLock {
  105. if setting.SSH.StartBuiltinServer {
  106. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  107. log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  108. } else {
  109. ssh.Unused()
  110. }
  111. }
  112. }
上海开阖软件有限公司 沪ICP备12045867号-1