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

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. authmodel "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/modules/cache"
  12. "code.gitea.io/gitea/modules/eventsource"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/highlight"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/markup"
  17. "code.gitea.io/gitea/modules/markup/external"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/ssh"
  20. "code.gitea.io/gitea/modules/storage"
  21. "code.gitea.io/gitea/modules/svg"
  22. "code.gitea.io/gitea/modules/system"
  23. "code.gitea.io/gitea/modules/templates"
  24. "code.gitea.io/gitea/modules/translation"
  25. "code.gitea.io/gitea/modules/web"
  26. actions_router "code.gitea.io/gitea/routers/api/actions"
  27. packages_router "code.gitea.io/gitea/routers/api/packages"
  28. apiv1 "code.gitea.io/gitea/routers/api/v1"
  29. "code.gitea.io/gitea/routers/common"
  30. "code.gitea.io/gitea/routers/private"
  31. web_routers "code.gitea.io/gitea/routers/web"
  32. actions_service "code.gitea.io/gitea/services/actions"
  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. 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/uinotification"
  49. "code.gitea.io/gitea/services/webhook"
  50. )
  51. func mustInit(fn func() error) {
  52. err := fn()
  53. if err != nil {
  54. ptr := reflect.ValueOf(fn).Pointer()
  55. fi := runtime.FuncForPC(ptr)
  56. log.Fatal("%s failed: %v", fi.Name(), err)
  57. }
  58. }
  59. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  60. err := fn(ctx)
  61. if err != nil {
  62. ptr := reflect.ValueOf(fn).Pointer()
  63. fi := runtime.FuncForPC(ptr)
  64. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  65. }
  66. }
  67. func syncAppConfForGit(ctx context.Context) error {
  68. runtimeState := new(system.RuntimeState)
  69. if err := system.AppState.Get(runtimeState); err != nil {
  70. return err
  71. }
  72. updated := false
  73. if runtimeState.LastAppPath != setting.AppPath {
  74. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  75. runtimeState.LastAppPath = setting.AppPath
  76. updated = true
  77. }
  78. if runtimeState.LastCustomConf != setting.CustomConf {
  79. log.Info("CustomConf changed from '%s' to '%s'", runtimeState.LastCustomConf, setting.CustomConf)
  80. runtimeState.LastCustomConf = setting.CustomConf
  81. updated = true
  82. }
  83. if updated {
  84. log.Info("re-sync repository hooks ...")
  85. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  86. log.Info("re-write ssh public keys ...")
  87. mustInitCtx(ctx, asymkey_model.RewriteAllPublicKeys)
  88. return system.AppState.Set(runtimeState)
  89. }
  90. return nil
  91. }
  92. func InitWebInstallPage(ctx context.Context) {
  93. translation.InitLocales(ctx)
  94. setting.LoadSettingsForInstall()
  95. mustInit(svg.Init)
  96. }
  97. // InitWebInstalled is for global installed configuration.
  98. func InitWebInstalled(ctx context.Context) {
  99. mustInitCtx(ctx, git.InitFull)
  100. log.Info("Git version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
  101. // Setup i18n
  102. translation.InitLocales(ctx)
  103. setting.LoadSettings()
  104. mustInit(storage.Init)
  105. mailer.NewContext(ctx)
  106. mustInit(cache.NewContext)
  107. mustInit(feed_service.Init)
  108. mustInit(uinotification.Init)
  109. mustInit(archiver.Init)
  110. highlight.NewContext()
  111. external.RegisterRenderers()
  112. markup.Init(markup_service.ProcessorHelper())
  113. if setting.EnableSQLite3 {
  114. log.Info("SQLite3 support is enabled")
  115. } else if setting.Database.Type.IsSQLite3() {
  116. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  117. }
  118. mustInitCtx(ctx, common.InitDBEngine)
  119. log.Info("ORM engine initialization successful!")
  120. mustInit(system.Init)
  121. mustInit(oauth2.Init)
  122. mustInitCtx(ctx, models.Init)
  123. mustInitCtx(ctx, authmodel.Init)
  124. mustInitCtx(ctx, repo_service.Init)
  125. // Booting long running goroutines.
  126. mustInit(indexer_service.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, mailer_incoming.Init)
  135. mustInitCtx(ctx, syncAppConfForGit)
  136. mustInit(ssh.Init)
  137. auth.Init()
  138. mustInit(svg.Init)
  139. actions_service.Init()
  140. // Finally start up the cron
  141. cron.NewContext(ctx)
  142. }
  143. // NormalRoutes represents non install routes
  144. func NormalRoutes() *web.Route {
  145. _ = templates.HTMLRenderer()
  146. r := web.NewRoute()
  147. r.Use(common.ProtocolMiddlewares()...)
  148. r.Mount("/", web_routers.Routes())
  149. r.Mount("/api/v1", apiv1.Routes())
  150. r.Mount("/api/internal", private.Routes())
  151. r.Post("/-/fetch-redirect", common.FetchRedirectDelegate)
  152. if setting.Packages.Enabled {
  153. // This implements package support for most package managers
  154. r.Mount("/api/packages", packages_router.CommonRoutes())
  155. // This implements the OCI API (Note this is not preceded by /api but is instead /v2)
  156. r.Mount("/v2", packages_router.ContainerRoutes())
  157. }
  158. if setting.Actions.Enabled {
  159. prefix := "/api/actions"
  160. r.Mount(prefix, actions_router.Routes(prefix))
  161. // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now.
  162. // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/
  163. // TODO: this prefix should be generated with a token string with runner ?
  164. prefix = "/api/actions_pipeline"
  165. r.Mount(prefix, actions_router.ArtifactsRoutes(prefix))
  166. }
  167. return r
  168. }