選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

init.go 4.0KB

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