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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "path"
  7. "strings"
  8. "time"
  9. "code.gitea.io/git"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/migrations"
  12. "code.gitea.io/gitea/modules/cache"
  13. "code.gitea.io/gitea/modules/cron"
  14. "code.gitea.io/gitea/modules/highlight"
  15. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/mailer"
  18. "code.gitea.io/gitea/modules/markup"
  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. setting.CheckLFSVersion()
  61. log.Trace("AppPath: %s", setting.AppPath)
  62. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  63. log.Trace("Custom path: %s", setting.CustomPath)
  64. log.Trace("Log path: %s", setting.LogRootPath)
  65. models.LoadConfigs()
  66. NewServices()
  67. if setting.InstallLock {
  68. highlight.NewContext()
  69. markup.Init()
  70. if err := initDBEngine(); err == nil {
  71. log.Info("ORM engine initialization successful!")
  72. } else {
  73. log.Fatal(4, "ORM engine initialization failed: %v", err)
  74. }
  75. if err := models.InitOAuth2(); err != nil {
  76. log.Fatal(4, "Failed to initialize OAuth2 support: %v", err)
  77. }
  78. models.LoadRepoConfig()
  79. models.NewRepoContext()
  80. // Booting long running goroutines.
  81. cron.NewContext()
  82. if err := issue_indexer.InitIssueIndexer(false); err != nil {
  83. log.Fatal(4, "Failed to initialize issue indexer: %v", err)
  84. }
  85. models.InitRepoIndexer()
  86. models.InitSyncMirrors()
  87. models.InitDeliverHooks()
  88. models.InitTestPullRequests()
  89. log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
  90. }
  91. if models.EnableSQLite3 {
  92. log.Info("SQLite3 Supported")
  93. }
  94. if models.EnableTiDB {
  95. log.Info("TiDB Supported")
  96. }
  97. checkRunMode()
  98. if setting.InstallLock && setting.SSH.StartBuiltinServer {
  99. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  100. 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)
  101. }
  102. }