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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package routers
  4. import (
  5. "context"
  6. "reflect"
  7. "runtime"
  8. "code.gitea.io/gitea/models"
  9. asymkey_model "code.gitea.io/gitea/models/asymkey"
  10. "code.gitea.io/gitea/modules/cache"
  11. "code.gitea.io/gitea/modules/eventsource"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/highlight"
  14. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  15. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  16. stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/markup"
  19. "code.gitea.io/gitea/modules/markup/external"
  20. "code.gitea.io/gitea/modules/notification"
  21. "code.gitea.io/gitea/modules/setting"
  22. "code.gitea.io/gitea/modules/ssh"
  23. "code.gitea.io/gitea/modules/storage"
  24. "code.gitea.io/gitea/modules/svg"
  25. "code.gitea.io/gitea/modules/system"
  26. "code.gitea.io/gitea/modules/templates"
  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. mailer_incoming "code.gitea.io/gitea/services/mailer/incoming"
  41. markup_service "code.gitea.io/gitea/services/markup"
  42. repo_migrations "code.gitea.io/gitea/services/migrations"
  43. mirror_service "code.gitea.io/gitea/services/mirror"
  44. pull_service "code.gitea.io/gitea/services/pull"
  45. repo_service "code.gitea.io/gitea/services/repository"
  46. "code.gitea.io/gitea/services/repository/archiver"
  47. "code.gitea.io/gitea/services/task"
  48. "code.gitea.io/gitea/services/webhook"
  49. )
  50. func mustInit(fn func() error) {
  51. err := fn()
  52. if err != nil {
  53. ptr := reflect.ValueOf(fn).Pointer()
  54. fi := runtime.FuncForPC(ptr)
  55. log.Fatal("%s failed: %v", fi.Name(), err)
  56. }
  57. }
  58. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  59. err := fn(ctx)
  60. if err != nil {
  61. ptr := reflect.ValueOf(fn).Pointer()
  62. fi := runtime.FuncForPC(ptr)
  63. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  64. }
  65. }
  66. // InitGitServices init new services for git, this is also called in `contrib/pr/checkout.go`
  67. func InitGitServices() {
  68. setting.NewServices()
  69. mustInit(storage.Init)
  70. mustInit(repo_service.Init)
  71. }
  72. func syncAppConfForGit(ctx context.Context) error {
  73. runtimeState := new(system.RuntimeState)
  74. if err := system.AppState.Get(runtimeState); err != nil {
  75. return err
  76. }
  77. updated := false
  78. if runtimeState.LastAppPath != setting.AppPath {
  79. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  80. runtimeState.LastAppPath = setting.AppPath
  81. updated = true
  82. }
  83. if runtimeState.LastCustomConf != setting.CustomConf {
  84. log.Info("CustomConf changed from '%s' to '%s'", runtimeState.LastCustomConf, setting.CustomConf)
  85. runtimeState.LastCustomConf = setting.CustomConf
  86. updated = true
  87. }
  88. if updated {
  89. log.Info("re-sync repository hooks ...")
  90. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  91. log.Info("re-write ssh public keys ...")
  92. mustInit(asymkey_model.RewriteAllPublicKeys)
  93. return system.AppState.Set(runtimeState)
  94. }
  95. return nil
  96. }
  97. // GlobalInitInstalled is for global installed configuration.
  98. func GlobalInitInstalled(ctx context.Context) {
  99. if !setting.InstallLock {
  100. log.Fatal("Gitea is not installed")
  101. }
  102. mustInitCtx(ctx, git.InitFull)
  103. log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
  104. log.Info("AppPath: %s", setting.AppPath)
  105. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  106. log.Info("Custom path: %s", setting.CustomPath)
  107. log.Info("Log path: %s", setting.LogRootPath)
  108. log.Info("Configuration file: %s", setting.CustomConf)
  109. log.Info("Run Mode: %s", util.ToTitleCase(setting.RunMode))
  110. log.Info("Gitea v%s%s", setting.AppVer, setting.AppBuiltWith)
  111. // Setup i18n
  112. translation.InitLocales(ctx)
  113. setting.NewServices()
  114. mustInit(storage.Init)
  115. mailer.NewContext(ctx)
  116. mustInit(cache.NewContext)
  117. notification.NewContext()
  118. mustInit(archiver.Init)
  119. highlight.NewContext()
  120. external.RegisterRenderers()
  121. markup.Init(markup_service.ProcessorHelper())
  122. if setting.EnableSQLite3 {
  123. log.Info("SQLite3 support is enabled")
  124. } else if setting.Database.UseSQLite3 {
  125. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  126. }
  127. mustInitCtx(ctx, common.InitDBEngine)
  128. log.Info("ORM engine initialization successful!")
  129. mustInit(system.Init)
  130. mustInit(oauth2.Init)
  131. mustInit(models.Init)
  132. mustInit(repo_service.Init)
  133. // Booting long running goroutines.
  134. issue_indexer.InitIssueIndexer(false)
  135. code_indexer.Init()
  136. mustInit(stats_indexer.Init)
  137. mirror_service.InitSyncMirrors()
  138. mustInit(webhook.Init)
  139. mustInit(pull_service.Init)
  140. mustInit(automerge.Init)
  141. mustInit(task.Init)
  142. mustInit(repo_migrations.Init)
  143. eventsource.GetManager().Init()
  144. mustInitCtx(ctx, mailer_incoming.Init)
  145. mustInitCtx(ctx, syncAppConfForGit)
  146. mustInit(ssh.Init)
  147. auth.Init()
  148. svg.Init()
  149. // Finally start up the cron
  150. cron.NewContext(ctx)
  151. }
  152. // NormalRoutes represents non install routes
  153. func NormalRoutes(ctx context.Context) *web.Route {
  154. ctx, _ = templates.HTMLRenderer(ctx)
  155. r := web.NewRoute()
  156. for _, middle := range common.Middlewares() {
  157. r.Use(middle)
  158. }
  159. r.Mount("/", web_routers.Routes(ctx))
  160. r.Mount("/api/v1", apiv1.Routes(ctx))
  161. r.Mount("/api/internal", private.Routes())
  162. if setting.Packages.Enabled {
  163. // Add endpoints to match common package manager APIs
  164. // This implements package support for most package managers
  165. r.Mount("/api/packages", packages_router.CommonRoutes(ctx))
  166. // This implements the OCI API (Note this is not preceded by /api but is instead /v2)
  167. r.Mount("/v2", packages_router.ContainerRoutes(ctx))
  168. }
  169. return r
  170. }