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.

init.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "context"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/migrations"
  12. "code.gitea.io/gitea/modules/auth/sso"
  13. "code.gitea.io/gitea/modules/cache"
  14. "code.gitea.io/gitea/modules/cron"
  15. "code.gitea.io/gitea/modules/eventsource"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/highlight"
  18. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  19. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  20. stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/markup"
  23. "code.gitea.io/gitea/modules/markup/external"
  24. "code.gitea.io/gitea/modules/notification"
  25. "code.gitea.io/gitea/modules/options"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/ssh"
  28. "code.gitea.io/gitea/modules/task"
  29. "code.gitea.io/gitea/modules/webhook"
  30. "code.gitea.io/gitea/services/mailer"
  31. mirror_service "code.gitea.io/gitea/services/mirror"
  32. pull_service "code.gitea.io/gitea/services/pull"
  33. "gitea.com/macaron/i18n"
  34. "gitea.com/macaron/macaron"
  35. )
  36. func checkRunMode() {
  37. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  38. case "prod":
  39. macaron.Env = macaron.PROD
  40. macaron.ColorLog = false
  41. setting.ProdMode = true
  42. default:
  43. git.Debug = true
  44. }
  45. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  46. }
  47. // NewServices init new services
  48. func NewServices() {
  49. setting.NewServices()
  50. mailer.NewContext()
  51. _ = cache.NewContext()
  52. notification.NewContext()
  53. }
  54. // In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
  55. func initDBEngine(ctx context.Context) (err error) {
  56. log.Info("Beginning ORM engine initialization.")
  57. for i := 0; i < setting.Database.DBConnectRetries; i++ {
  58. select {
  59. case <-ctx.Done():
  60. return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization")
  61. default:
  62. }
  63. log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
  64. if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
  65. break
  66. } else if i == setting.Database.DBConnectRetries-1 {
  67. return err
  68. }
  69. log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
  70. log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
  71. time.Sleep(setting.Database.DBConnectBackoff)
  72. }
  73. models.HasEngine = true
  74. return nil
  75. }
  76. // InitLocales loads the locales
  77. func InitLocales() {
  78. localeNames, err := options.Dir("locale")
  79. if err != nil {
  80. log.Fatal("Failed to list locale files: %v", err)
  81. }
  82. localFiles := make(map[string][]byte)
  83. for _, name := range localeNames {
  84. localFiles[name], err = options.Locale(name)
  85. if err != nil {
  86. log.Fatal("Failed to load %s locale file. %v", name, err)
  87. }
  88. }
  89. i18n.I18n(i18n.Options{
  90. SubURL: setting.AppSubURL,
  91. Files: localFiles,
  92. Langs: setting.Langs,
  93. Names: setting.Names,
  94. DefaultLang: "en-US",
  95. Redirect: false,
  96. CookieDomain: setting.SessionConfig.Domain,
  97. })
  98. }
  99. // GlobalInit is for global configuration reload-able.
  100. func GlobalInit(ctx context.Context) {
  101. setting.NewContext()
  102. if err := git.Init(ctx); err != nil {
  103. log.Fatal("Git module init failed: %v", err)
  104. }
  105. setting.CheckLFSVersion()
  106. log.Trace("AppPath: %s", setting.AppPath)
  107. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  108. log.Trace("Custom path: %s", setting.CustomPath)
  109. log.Trace("Log path: %s", setting.LogRootPath)
  110. // Setup i18n
  111. InitLocales()
  112. NewServices()
  113. if setting.InstallLock {
  114. highlight.NewContext()
  115. external.RegisterParsers()
  116. markup.Init()
  117. if err := initDBEngine(ctx); err == nil {
  118. log.Info("ORM engine initialization successful!")
  119. } else {
  120. log.Fatal("ORM engine initialization failed: %v", err)
  121. }
  122. if err := models.InitOAuth2(); err != nil {
  123. log.Fatal("Failed to initialize OAuth2 support: %v", err)
  124. }
  125. models.NewRepoContext()
  126. // Booting long running goroutines.
  127. cron.NewContext()
  128. issue_indexer.InitIssueIndexer(false)
  129. code_indexer.Init()
  130. if err := stats_indexer.Init(); err != nil {
  131. log.Fatal("Failed to initialize repository stats indexer queue: %v", err)
  132. }
  133. mirror_service.InitSyncMirrors()
  134. webhook.InitDeliverHooks()
  135. if err := pull_service.Init(); err != nil {
  136. log.Fatal("Failed to initialize test pull requests queue: %v", err)
  137. }
  138. if err := task.Init(); err != nil {
  139. log.Fatal("Failed to initialize task scheduler: %v", err)
  140. }
  141. eventsource.GetManager().Init()
  142. }
  143. if setting.EnableSQLite3 {
  144. log.Info("SQLite3 Supported")
  145. }
  146. checkRunMode()
  147. // Now because Install will re-run GlobalInit once it has set InstallLock
  148. // we can't tell if the ssh port will remain unused until that's done.
  149. // However, see FIXME comment in install.go
  150. if setting.InstallLock {
  151. if setting.SSH.StartBuiltinServer {
  152. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  153. 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)
  154. } else {
  155. ssh.Unused()
  156. }
  157. }
  158. if setting.InstallLock {
  159. sso.Init()
  160. }
  161. }