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

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