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

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