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

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