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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "reflect"
  8. "runtime"
  9. "code.gitea.io/gitea/models"
  10. asymkey_model "code.gitea.io/gitea/models/asymkey"
  11. "code.gitea.io/gitea/modules/appstate"
  12. "code.gitea.io/gitea/modules/cache"
  13. "code.gitea.io/gitea/modules/eventsource"
  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. stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/markup"
  21. "code.gitea.io/gitea/modules/markup/external"
  22. "code.gitea.io/gitea/modules/notification"
  23. "code.gitea.io/gitea/modules/setting"
  24. "code.gitea.io/gitea/modules/ssh"
  25. "code.gitea.io/gitea/modules/storage"
  26. "code.gitea.io/gitea/modules/svg"
  27. "code.gitea.io/gitea/modules/translation"
  28. "code.gitea.io/gitea/modules/util"
  29. "code.gitea.io/gitea/modules/web"
  30. packages_router "code.gitea.io/gitea/routers/api/packages"
  31. apiv1 "code.gitea.io/gitea/routers/api/v1"
  32. "code.gitea.io/gitea/routers/common"
  33. "code.gitea.io/gitea/routers/private"
  34. web_routers "code.gitea.io/gitea/routers/web"
  35. "code.gitea.io/gitea/services/auth"
  36. "code.gitea.io/gitea/services/auth/source/oauth2"
  37. "code.gitea.io/gitea/services/automerge"
  38. "code.gitea.io/gitea/services/cron"
  39. "code.gitea.io/gitea/services/mailer"
  40. repo_migrations "code.gitea.io/gitea/services/migrations"
  41. mirror_service "code.gitea.io/gitea/services/mirror"
  42. pull_service "code.gitea.io/gitea/services/pull"
  43. repo_service "code.gitea.io/gitea/services/repository"
  44. "code.gitea.io/gitea/services/repository/archiver"
  45. "code.gitea.io/gitea/services/task"
  46. "code.gitea.io/gitea/services/webhook"
  47. )
  48. func mustInit(fn func() error) {
  49. err := fn()
  50. if err != nil {
  51. ptr := reflect.ValueOf(fn).Pointer()
  52. fi := runtime.FuncForPC(ptr)
  53. log.Fatal("%s failed: %v", fi.Name(), err)
  54. }
  55. }
  56. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  57. err := fn(ctx)
  58. if err != nil {
  59. ptr := reflect.ValueOf(fn).Pointer()
  60. fi := runtime.FuncForPC(ptr)
  61. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  62. }
  63. }
  64. // InitGitServices init new services for git, this is also called in `contrib/pr/checkout.go`
  65. func InitGitServices() {
  66. setting.NewServices()
  67. mustInit(storage.Init)
  68. mustInit(repo_service.Init)
  69. }
  70. func syncAppPathForGit(ctx context.Context) error {
  71. runtimeState := new(appstate.RuntimeState)
  72. if err := appstate.AppState.Get(runtimeState); err != nil {
  73. return err
  74. }
  75. if runtimeState.LastAppPath != setting.AppPath {
  76. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  77. log.Info("re-sync repository hooks ...")
  78. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  79. log.Info("re-write ssh public keys ...")
  80. mustInit(asymkey_model.RewriteAllPublicKeys)
  81. runtimeState.LastAppPath = setting.AppPath
  82. return appstate.AppState.Set(runtimeState)
  83. }
  84. return nil
  85. }
  86. // GlobalInitInstalled is for global installed configuration.
  87. func GlobalInitInstalled(ctx context.Context) {
  88. if !setting.InstallLock {
  89. log.Fatal("Gitea is not installed")
  90. }
  91. mustInitCtx(ctx, git.InitOnceWithSync)
  92. log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
  93. git.CheckLFSVersion()
  94. log.Info("AppPath: %s", setting.AppPath)
  95. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  96. log.Info("Custom path: %s", setting.CustomPath)
  97. log.Info("Log path: %s", setting.LogRootPath)
  98. log.Info("Configuration file: %s", setting.CustomConf)
  99. log.Info("Run Mode: %s", util.ToTitleCase(setting.RunMode))
  100. // Setup i18n
  101. translation.InitLocales()
  102. setting.NewServices()
  103. mustInit(storage.Init)
  104. mailer.NewContext()
  105. mustInit(cache.NewContext)
  106. notification.NewContext()
  107. mustInit(archiver.Init)
  108. highlight.NewContext()
  109. external.RegisterRenderers()
  110. markup.Init()
  111. if setting.EnableSQLite3 {
  112. log.Info("SQLite3 support is enabled")
  113. } else if setting.Database.UseSQLite3 {
  114. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  115. }
  116. mustInitCtx(ctx, common.InitDBEngine)
  117. log.Info("ORM engine initialization successful!")
  118. mustInit(appstate.Init)
  119. mustInit(oauth2.Init)
  120. models.NewRepoContext()
  121. mustInit(repo_service.Init)
  122. // Booting long running goroutines.
  123. cron.NewContext(ctx)
  124. issue_indexer.InitIssueIndexer(false)
  125. code_indexer.Init()
  126. mustInit(stats_indexer.Init)
  127. mirror_service.InitSyncMirrors()
  128. mustInit(webhook.Init)
  129. mustInit(pull_service.Init)
  130. mustInit(automerge.Init)
  131. mustInit(task.Init)
  132. mustInit(repo_migrations.Init)
  133. eventsource.GetManager().Init()
  134. mustInitCtx(ctx, syncAppPathForGit)
  135. mustInit(ssh.Init)
  136. auth.Init()
  137. svg.Init()
  138. }
  139. // NormalRoutes represents non install routes
  140. func NormalRoutes() *web.Route {
  141. r := web.NewRoute()
  142. for _, middle := range common.Middlewares() {
  143. r.Use(middle)
  144. }
  145. r.Mount("/", web_routers.Routes())
  146. r.Mount("/api/v1", apiv1.Routes())
  147. r.Mount("/api/internal", private.Routes())
  148. if setting.Packages.Enabled {
  149. r.Mount("/api/packages", packages_router.Routes())
  150. r.Mount("/v2", packages_router.ContainerRoutes())
  151. }
  152. return r
  153. }