gitea源码

init.go 7.1KB

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