Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

init.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package routers
  4. import (
  5. "context"
  6. "net/http"
  7. "reflect"
  8. "runtime"
  9. "code.gitea.io/gitea/models"
  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/util"
  26. "code.gitea.io/gitea/modules/web"
  27. "code.gitea.io/gitea/modules/web/routing"
  28. actions_router "code.gitea.io/gitea/routers/api/actions"
  29. packages_router "code.gitea.io/gitea/routers/api/packages"
  30. apiv1 "code.gitea.io/gitea/routers/api/v1"
  31. "code.gitea.io/gitea/routers/common"
  32. "code.gitea.io/gitea/routers/private"
  33. web_routers "code.gitea.io/gitea/routers/web"
  34. actions_service "code.gitea.io/gitea/services/actions"
  35. asymkey_service "code.gitea.io/gitea/services/asymkey"
  36. "code.gitea.io/gitea/services/auth"
  37. "code.gitea.io/gitea/services/auth/source/oauth2"
  38. "code.gitea.io/gitea/services/automerge"
  39. "code.gitea.io/gitea/services/cron"
  40. feed_service "code.gitea.io/gitea/services/feed"
  41. indexer_service "code.gitea.io/gitea/services/indexer"
  42. "code.gitea.io/gitea/services/mailer"
  43. mailer_incoming "code.gitea.io/gitea/services/mailer/incoming"
  44. markup_service "code.gitea.io/gitea/services/markup"
  45. repo_migrations "code.gitea.io/gitea/services/migrations"
  46. mirror_service "code.gitea.io/gitea/services/mirror"
  47. pull_service "code.gitea.io/gitea/services/pull"
  48. release_service "code.gitea.io/gitea/services/release"
  49. repo_service "code.gitea.io/gitea/services/repository"
  50. "code.gitea.io/gitea/services/repository/archiver"
  51. "code.gitea.io/gitea/services/task"
  52. "code.gitea.io/gitea/services/uinotification"
  53. "code.gitea.io/gitea/services/webhook"
  54. )
  55. func mustInit(fn func() error) {
  56. err := fn()
  57. if err != nil {
  58. ptr := reflect.ValueOf(fn).Pointer()
  59. fi := runtime.FuncForPC(ptr)
  60. log.Fatal("%s failed: %v", fi.Name(), err)
  61. }
  62. }
  63. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  64. err := fn(ctx)
  65. if err != nil {
  66. ptr := reflect.ValueOf(fn).Pointer()
  67. fi := runtime.FuncForPC(ptr)
  68. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  69. }
  70. }
  71. func syncAppConfForGit(ctx context.Context) error {
  72. runtimeState := new(system.RuntimeState)
  73. if err := system.AppState.Get(ctx, runtimeState); err != nil {
  74. return err
  75. }
  76. updated := false
  77. if runtimeState.LastAppPath != setting.AppPath {
  78. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  79. runtimeState.LastAppPath = setting.AppPath
  80. updated = true
  81. }
  82. if runtimeState.LastCustomConf != setting.CustomConf {
  83. log.Info("CustomConf changed from '%s' to '%s'", runtimeState.LastCustomConf, setting.CustomConf)
  84. runtimeState.LastCustomConf = setting.CustomConf
  85. updated = true
  86. }
  87. if updated {
  88. log.Info("re-sync repository hooks ...")
  89. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  90. log.Info("re-write ssh public keys ...")
  91. mustInitCtx(ctx, asymkey_service.RewriteAllPublicKeys)
  92. return system.AppState.Set(ctx, runtimeState)
  93. }
  94. return nil
  95. }
  96. func InitWebInstallPage(ctx context.Context) {
  97. translation.InitLocales(ctx)
  98. setting.LoadSettingsForInstall()
  99. mustInit(svg.Init)
  100. }
  101. // InitWebInstalled is for global installed configuration.
  102. func InitWebInstalled(ctx context.Context) {
  103. mustInitCtx(ctx, git.InitFull)
  104. log.Info("Git version: %s (home: %s)", git.DefaultFeatures().VersionInfo(), git.HomeDir())
  105. if !git.DefaultFeatures().SupportHashSha256 {
  106. log.Warn("sha256 hash support is disabled - requires Git >= 2.42." + util.Iif(git.DefaultFeatures().UsingGogit, " Gogit is currently unsupported.", ""))
  107. }
  108. // Setup i18n
  109. translation.InitLocales(ctx)
  110. setting.LoadSettings()
  111. mustInit(storage.Init)
  112. mailer.NewContext(ctx)
  113. mustInit(cache.Init)
  114. mustInit(feed_service.Init)
  115. mustInit(uinotification.Init)
  116. mustInitCtx(ctx, archiver.Init)
  117. highlight.NewContext()
  118. external.RegisterRenderers()
  119. markup.Init(markup_service.ProcessorHelper())
  120. if setting.EnableSQLite3 {
  121. log.Info("SQLite3 support is enabled")
  122. } else if setting.Database.Type.IsSQLite3() {
  123. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  124. }
  125. mustInitCtx(ctx, common.InitDBEngine)
  126. log.Info("ORM engine initialization successful!")
  127. mustInit(system.Init)
  128. mustInitCtx(ctx, oauth2.Init)
  129. mustInit(release_service.Init)
  130. mustInitCtx(ctx, models.Init)
  131. mustInitCtx(ctx, authmodel.Init)
  132. mustInitCtx(ctx, repo_service.Init)
  133. // Booting long running goroutines.
  134. mustInit(indexer_service.Init)
  135. mirror_service.InitSyncMirrors()
  136. mustInit(webhook.Init)
  137. mustInit(pull_service.Init)
  138. mustInit(automerge.Init)
  139. mustInit(task.Init)
  140. mustInit(repo_migrations.Init)
  141. eventsource.GetManager().Init()
  142. mustInitCtx(ctx, mailer_incoming.Init)
  143. mustInitCtx(ctx, syncAppConfForGit)
  144. mustInit(ssh.Init)
  145. auth.Init()
  146. mustInit(svg.Init)
  147. actions_service.Init()
  148. // Finally start up the cron
  149. cron.NewContext(ctx)
  150. }
  151. // NormalRoutes represents non install routes
  152. func NormalRoutes() *web.Route {
  153. _ = templates.HTMLRenderer()
  154. r := web.NewRoute()
  155. r.Use(common.ProtocolMiddlewares()...)
  156. r.Mount("/", web_routers.Routes())
  157. r.Mount("/api/v1", apiv1.Routes())
  158. r.Mount("/api/internal", private.Routes())
  159. r.Post("/-/fetch-redirect", common.FetchRedirectDelegate)
  160. if setting.Packages.Enabled {
  161. // This implements package support for most package managers
  162. r.Mount("/api/packages", packages_router.CommonRoutes())
  163. // This implements the OCI API (Note this is not preceded by /api but is instead /v2)
  164. r.Mount("/v2", packages_router.ContainerRoutes())
  165. }
  166. if setting.Actions.Enabled {
  167. prefix := "/api/actions"
  168. r.Mount(prefix, actions_router.Routes(prefix))
  169. // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now.
  170. // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/
  171. // TODO: this prefix should be generated with a token string with runner ?
  172. prefix = "/api/actions_pipeline"
  173. r.Mount(prefix, actions_router.ArtifactsRoutes(prefix))
  174. prefix = actions_router.ArtifactV4RouteBase
  175. r.Mount(prefix, actions_router.ArtifactsV4Routes(prefix))
  176. }
  177. r.NotFound(func(w http.ResponseWriter, req *http.Request) {
  178. routing.UpdateFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound"))
  179. http.NotFound(w, req)
  180. })
  181. return r
  182. }