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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "context"
  7. "net"
  8. "reflect"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. asymkey_model "code.gitea.io/gitea/models/asymkey"
  14. "code.gitea.io/gitea/modules/appstate"
  15. "code.gitea.io/gitea/modules/cache"
  16. "code.gitea.io/gitea/modules/eventsource"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/highlight"
  19. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  20. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  21. stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
  22. "code.gitea.io/gitea/modules/log"
  23. "code.gitea.io/gitea/modules/markup"
  24. "code.gitea.io/gitea/modules/markup/external"
  25. "code.gitea.io/gitea/modules/notification"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/ssh"
  28. "code.gitea.io/gitea/modules/storage"
  29. "code.gitea.io/gitea/modules/svg"
  30. "code.gitea.io/gitea/modules/translation"
  31. "code.gitea.io/gitea/modules/web"
  32. packages_router "code.gitea.io/gitea/routers/api/packages"
  33. apiv1 "code.gitea.io/gitea/routers/api/v1"
  34. "code.gitea.io/gitea/routers/common"
  35. "code.gitea.io/gitea/routers/private"
  36. web_routers "code.gitea.io/gitea/routers/web"
  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. 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/webhook"
  49. )
  50. func mustInit(fn func() error) {
  51. err := fn()
  52. if err != nil {
  53. ptr := reflect.ValueOf(fn).Pointer()
  54. fi := runtime.FuncForPC(ptr)
  55. log.Fatal("%s failed: %v", fi.Name(), err)
  56. }
  57. }
  58. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  59. err := fn(ctx)
  60. if err != nil {
  61. ptr := reflect.ValueOf(fn).Pointer()
  62. fi := runtime.FuncForPC(ptr)
  63. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  64. }
  65. }
  66. // InitGitServices init new services for git, this is also called in `contrib/pr/checkout.go`
  67. func InitGitServices() {
  68. setting.NewServices()
  69. mustInit(storage.Init)
  70. mustInit(repo_service.Init)
  71. }
  72. func syncAppPathForGit(ctx context.Context) error {
  73. runtimeState := new(appstate.RuntimeState)
  74. if err := appstate.AppState.Get(runtimeState); err != nil {
  75. return err
  76. }
  77. if runtimeState.LastAppPath != setting.AppPath {
  78. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  79. log.Info("re-sync repository hooks ...")
  80. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  81. log.Info("re-write ssh public keys ...")
  82. mustInit(asymkey_model.RewriteAllPublicKeys)
  83. runtimeState.LastAppPath = setting.AppPath
  84. return appstate.AppState.Set(runtimeState)
  85. }
  86. return nil
  87. }
  88. // GlobalInitInstalled is for global installed configuration.
  89. func GlobalInitInstalled(ctx context.Context) {
  90. if !setting.InstallLock {
  91. log.Fatal("Gitea is not installed")
  92. }
  93. mustInitCtx(ctx, git.Init)
  94. log.Info(git.VersionInfo())
  95. git.CheckLFSVersion()
  96. log.Info("AppPath: %s", setting.AppPath)
  97. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  98. log.Info("Custom path: %s", setting.CustomPath)
  99. log.Info("Log path: %s", setting.LogRootPath)
  100. log.Info("Configuration file: %s", setting.CustomConf)
  101. log.Info("Run Mode: %s", strings.Title(setting.RunMode))
  102. // Setup i18n
  103. translation.InitLocales()
  104. setting.NewServices()
  105. mustInit(storage.Init)
  106. mailer.NewContext()
  107. mustInit(cache.NewContext)
  108. notification.NewContext()
  109. mustInit(archiver.Init)
  110. highlight.NewContext()
  111. external.RegisterRenderers()
  112. markup.Init()
  113. if setting.EnableSQLite3 {
  114. log.Info("SQLite3 support is enabled")
  115. } else if setting.Database.UseSQLite3 {
  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(appstate.Init)
  121. mustInit(oauth2.Init)
  122. models.NewRepoContext()
  123. mustInit(repo_service.Init)
  124. // Booting long running goroutines.
  125. cron.NewContext(ctx)
  126. issue_indexer.InitIssueIndexer(false)
  127. code_indexer.Init()
  128. mustInit(stats_indexer.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, syncAppPathForGit)
  137. if setting.SSH.StartBuiltinServer {
  138. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  139. log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
  140. net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
  141. setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  142. } else {
  143. ssh.Unused()
  144. }
  145. auth.Init()
  146. svg.Init()
  147. }
  148. // NormalRoutes represents non install routes
  149. func NormalRoutes() *web.Route {
  150. r := web.NewRoute()
  151. for _, middle := range common.Middlewares() {
  152. r.Use(middle)
  153. }
  154. r.Mount("/", web_routers.Routes())
  155. r.Mount("/api/v1", apiv1.Routes())
  156. r.Mount("/api/internal", private.Routes())
  157. if setting.Packages.Enabled {
  158. r.Mount("/api/packages", packages_router.Routes())
  159. r.Mount("/v2", packages_router.ContainerRoutes())
  160. }
  161. return r
  162. }