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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. func syncAppConfForGit(ctx context.Context) error {
  69. runtimeState := new(system.RuntimeState)
  70. if err := system.AppState.Get(runtimeState); err != nil {
  71. return err
  72. }
  73. updated := false
  74. if runtimeState.LastAppPath != setting.AppPath {
  75. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  76. runtimeState.LastAppPath = setting.AppPath
  77. updated = true
  78. }
  79. if runtimeState.LastCustomConf != setting.CustomConf {
  80. log.Info("CustomConf changed from '%s' to '%s'", runtimeState.LastCustomConf, setting.CustomConf)
  81. runtimeState.LastCustomConf = setting.CustomConf
  82. updated = true
  83. }
  84. if updated {
  85. log.Info("re-sync repository hooks ...")
  86. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  87. log.Info("re-write ssh public keys ...")
  88. mustInit(asymkey_model.RewriteAllPublicKeys)
  89. return system.AppState.Set(runtimeState)
  90. }
  91. return nil
  92. }
  93. // GlobalInitInstalled is for global installed configuration.
  94. func GlobalInitInstalled(ctx context.Context) {
  95. if !setting.InstallLock {
  96. log.Fatal("Gitea is not installed")
  97. }
  98. mustInitCtx(ctx, git.InitFull)
  99. log.Info("Gitea Version: %s%s", setting.AppVer, setting.AppBuiltWith)
  100. log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
  101. log.Info("AppPath: %s", setting.AppPath)
  102. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  103. log.Info("Custom path: %s", setting.CustomPath)
  104. log.Info("Log path: %s", setting.Log.RootPath)
  105. log.Info("Configuration file: %s", setting.CustomConf)
  106. log.Info("Run Mode: %s", util.ToTitleCase(setting.RunMode))
  107. // Setup i18n
  108. translation.InitLocales(ctx)
  109. setting.LoadSettings()
  110. mustInit(storage.Init)
  111. mailer.NewContext(ctx)
  112. mustInit(cache.NewContext)
  113. notification.NewContext()
  114. mustInit(archiver.Init)
  115. highlight.NewContext()
  116. external.RegisterRenderers()
  117. markup.Init(markup_service.ProcessorHelper())
  118. if setting.EnableSQLite3 {
  119. log.Info("SQLite3 support is enabled")
  120. } else if setting.Database.Type.IsSQLite3() {
  121. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  122. }
  123. mustInitCtx(ctx, common.InitDBEngine)
  124. log.Info("ORM engine initialization successful!")
  125. mustInit(system.Init)
  126. mustInit(oauth2.Init)
  127. mustInitCtx(ctx, models.Init)
  128. mustInit(repo_service.Init)
  129. // Booting long running goroutines.
  130. issue_indexer.InitIssueIndexer(false)
  131. code_indexer.Init()
  132. mustInit(stats_indexer.Init)
  133. mirror_service.InitSyncMirrors()
  134. mustInit(webhook.Init)
  135. mustInit(pull_service.Init)
  136. mustInit(automerge.Init)
  137. mustInit(task.Init)
  138. mustInit(repo_migrations.Init)
  139. eventsource.GetManager().Init()
  140. mustInitCtx(ctx, mailer_incoming.Init)
  141. mustInitCtx(ctx, syncAppConfForGit)
  142. mustInit(ssh.Init)
  143. auth.Init()
  144. mustInit(svg.Init)
  145. actions_service.Init()
  146. // Finally start up the cron
  147. cron.NewContext(ctx)
  148. }
  149. // NormalRoutes represents non install routes
  150. func NormalRoutes() *web.Route {
  151. _ = templates.HTMLRenderer()
  152. r := web.NewRoute()
  153. r.Use(common.ProtocolMiddlewares()...)
  154. r.Mount("/", web_routers.Routes())
  155. r.Mount("/api/v1", apiv1.Routes())
  156. r.Mount("/api/internal", private.Routes())
  157. r.Post("/-/fetch-redirect", common.FetchRedirectDelegate)
  158. if setting.Packages.Enabled {
  159. // This implements package support for most package managers
  160. r.Mount("/api/packages", packages_router.CommonRoutes())
  161. // This implements the OCI API (Note this is not preceded by /api but is instead /v2)
  162. r.Mount("/v2", packages_router.ContainerRoutes())
  163. }
  164. if setting.Actions.Enabled {
  165. prefix := "/api/actions"
  166. r.Mount(prefix, actions_router.Routes(prefix))
  167. // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now.
  168. // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/
  169. // TODO: this prefix should be generated with a token string with runner ?
  170. prefix = "/api/actions_pipeline"
  171. r.Mount(prefix, actions_router.ArtifactsRoutes(prefix))
  172. }
  173. return r
  174. }