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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. authmodel "code.gitea.io/gitea/models/auth"
  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.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/markup"
  16. "code.gitea.io/gitea/modules/markup/external"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/ssh"
  19. "code.gitea.io/gitea/modules/storage"
  20. "code.gitea.io/gitea/modules/svg"
  21. "code.gitea.io/gitea/modules/system"
  22. "code.gitea.io/gitea/modules/templates"
  23. "code.gitea.io/gitea/modules/translation"
  24. "code.gitea.io/gitea/modules/web"
  25. actions_router "code.gitea.io/gitea/routers/api/actions"
  26. packages_router "code.gitea.io/gitea/routers/api/packages"
  27. apiv1 "code.gitea.io/gitea/routers/api/v1"
  28. "code.gitea.io/gitea/routers/common"
  29. "code.gitea.io/gitea/routers/private"
  30. web_routers "code.gitea.io/gitea/routers/web"
  31. actions_service "code.gitea.io/gitea/services/actions"
  32. asymkey_service "code.gitea.io/gitea/services/asymkey"
  33. "code.gitea.io/gitea/services/auth"
  34. "code.gitea.io/gitea/services/auth/source/oauth2"
  35. "code.gitea.io/gitea/services/automerge"
  36. "code.gitea.io/gitea/services/cron"
  37. feed_service "code.gitea.io/gitea/services/feed"
  38. indexer_service "code.gitea.io/gitea/services/indexer"
  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. release_service "code.gitea.io/gitea/services/release"
  46. repo_service "code.gitea.io/gitea/services/repository"
  47. "code.gitea.io/gitea/services/repository/archiver"
  48. "code.gitea.io/gitea/services/task"
  49. "code.gitea.io/gitea/services/uinotification"
  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(ctx, 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. mustInitCtx(ctx, asymkey_service.RewriteAllPublicKeys)
  89. return system.AppState.Set(ctx, runtimeState)
  90. }
  91. return nil
  92. }
  93. func InitWebInstallPage(ctx context.Context) {
  94. translation.InitLocales(ctx)
  95. setting.LoadSettingsForInstall()
  96. mustInit(svg.Init)
  97. }
  98. // InitWebInstalled is for global installed configuration.
  99. func InitWebInstalled(ctx context.Context) {
  100. mustInitCtx(ctx, git.InitFull)
  101. log.Info("Git version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
  102. // Setup i18n
  103. translation.InitLocales(ctx)
  104. setting.LoadSettings()
  105. mustInit(storage.Init)
  106. mailer.NewContext(ctx)
  107. mustInit(cache.Init)
  108. mustInit(feed_service.Init)
  109. mustInit(uinotification.Init)
  110. mustInitCtx(ctx, archiver.Init)
  111. highlight.NewContext()
  112. external.RegisterRenderers()
  113. markup.Init(markup_service.ProcessorHelper())
  114. if setting.EnableSQLite3 {
  115. log.Info("SQLite3 support is enabled")
  116. } else if setting.Database.Type.IsSQLite3() {
  117. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  118. }
  119. mustInitCtx(ctx, common.InitDBEngine)
  120. log.Info("ORM engine initialization successful!")
  121. mustInit(system.Init)
  122. mustInitCtx(ctx, oauth2.Init)
  123. mustInit(release_service.Init)
  124. mustInitCtx(ctx, models.Init)
  125. mustInitCtx(ctx, authmodel.Init)
  126. mustInitCtx(ctx, repo_service.Init)
  127. // Booting long running goroutines.
  128. mustInit(indexer_service.Init)
  129. mirror_service.InitSyncMirrors()
  130. mustInit(webhook.Init)
  131. mustInit(pull_service.Init)
  132. mustInit(automerge.Init)
  133. mustInit(task.Init)
  134. mustInit(repo_migrations.Init)
  135. eventsource.GetManager().Init()
  136. mustInitCtx(ctx, mailer_incoming.Init)
  137. mustInitCtx(ctx, syncAppConfForGit)
  138. mustInit(ssh.Init)
  139. auth.Init()
  140. mustInit(svg.Init)
  141. actions_service.Init()
  142. // Finally start up the cron
  143. cron.NewContext(ctx)
  144. }
  145. // NormalRoutes represents non install routes
  146. func NormalRoutes() *web.Route {
  147. _ = templates.HTMLRenderer()
  148. r := web.NewRoute()
  149. r.Use(common.ProtocolMiddlewares()...)
  150. r.Mount("/", web_routers.Routes())
  151. r.Mount("/api/v1", apiv1.Routes())
  152. r.Mount("/api/internal", private.Routes())
  153. r.Post("/-/fetch-redirect", common.FetchRedirectDelegate)
  154. if setting.Packages.Enabled {
  155. // This implements package support for most package managers
  156. r.Mount("/api/packages", packages_router.CommonRoutes())
  157. // This implements the OCI API (Note this is not preceded by /api but is instead /v2)
  158. r.Mount("/v2", packages_router.ContainerRoutes())
  159. }
  160. if setting.Actions.Enabled {
  161. prefix := "/api/actions"
  162. r.Mount(prefix, actions_router.Routes(prefix))
  163. // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now.
  164. // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/
  165. // TODO: this prefix should be generated with a token string with runner ?
  166. prefix = "/api/actions_pipeline"
  167. r.Mount(prefix, actions_router.ArtifactsRoutes(prefix))
  168. prefix = actions_router.ArtifactV4RouteBase
  169. r.Mount(prefix, actions_router.ArtifactsV4Routes(prefix))
  170. }
  171. return r
  172. }