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

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