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.5KB

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