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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "code.gitea.io/gitea/modules/appstate"
  14. "code.gitea.io/gitea/modules/cache"
  15. "code.gitea.io/gitea/modules/eventsource"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/highlight"
  18. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  19. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  20. stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/markup"
  23. "code.gitea.io/gitea/modules/markup/external"
  24. "code.gitea.io/gitea/modules/notification"
  25. "code.gitea.io/gitea/modules/setting"
  26. "code.gitea.io/gitea/modules/ssh"
  27. "code.gitea.io/gitea/modules/storage"
  28. "code.gitea.io/gitea/modules/svg"
  29. "code.gitea.io/gitea/modules/translation"
  30. "code.gitea.io/gitea/modules/web"
  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. "code.gitea.io/gitea/services/archiver"
  36. "code.gitea.io/gitea/services/auth"
  37. "code.gitea.io/gitea/services/auth/source/oauth2"
  38. "code.gitea.io/gitea/services/cron"
  39. "code.gitea.io/gitea/services/mailer"
  40. repo_migrations "code.gitea.io/gitea/services/migrations"
  41. mirror_service "code.gitea.io/gitea/services/mirror"
  42. pull_service "code.gitea.io/gitea/services/pull"
  43. repo_service "code.gitea.io/gitea/services/repository"
  44. "code.gitea.io/gitea/services/task"
  45. "code.gitea.io/gitea/services/webhook"
  46. "gitea.com/go-chi/session"
  47. )
  48. func mustInit(fn func() error) {
  49. err := fn()
  50. if err != nil {
  51. ptr := reflect.ValueOf(fn).Pointer()
  52. fi := runtime.FuncForPC(ptr)
  53. log.Fatal("%s failed: %v", fi.Name(), err)
  54. }
  55. }
  56. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  57. err := fn(ctx)
  58. if err != nil {
  59. ptr := reflect.ValueOf(fn).Pointer()
  60. fi := runtime.FuncForPC(ptr)
  61. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  62. }
  63. }
  64. // InitGitServices init new services for git, this is also called in `contrib/pr/checkout.go`
  65. func InitGitServices() {
  66. setting.NewServices()
  67. mustInit(storage.Init)
  68. mustInit(repo_service.NewContext)
  69. }
  70. func syncAppPathForGit(ctx context.Context) error {
  71. runtimeState := new(appstate.RuntimeState)
  72. if err := appstate.AppState.Get(runtimeState); err != nil {
  73. return err
  74. }
  75. if runtimeState.LastAppPath != setting.AppPath {
  76. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  77. log.Info("re-sync repository hooks ...")
  78. mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
  79. log.Info("re-write ssh public keys ...")
  80. mustInit(models.RewriteAllPublicKeys)
  81. runtimeState.LastAppPath = setting.AppPath
  82. return appstate.AppState.Set(runtimeState)
  83. }
  84. return nil
  85. }
  86. // GlobalInitInstalled is for global installed configuration.
  87. func GlobalInitInstalled(ctx context.Context) {
  88. if !setting.InstallLock {
  89. log.Fatal("Gitea is not installed")
  90. }
  91. mustInitCtx(ctx, git.Init)
  92. log.Info(git.VersionInfo())
  93. git.CheckLFSVersion()
  94. log.Info("AppPath: %s", setting.AppPath)
  95. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  96. log.Info("Custom path: %s", setting.CustomPath)
  97. log.Info("Log path: %s", setting.LogRootPath)
  98. log.Info("Configuration file: %s", setting.CustomConf)
  99. log.Info("Run Mode: %s", strings.Title(setting.RunMode))
  100. // Setup i18n
  101. translation.InitLocales()
  102. InitGitServices()
  103. mailer.NewContext()
  104. mustInit(cache.NewContext)
  105. notification.NewContext()
  106. mustInit(archiver.Init)
  107. highlight.NewContext()
  108. external.RegisterRenderers()
  109. markup.Init()
  110. if setting.EnableSQLite3 {
  111. log.Info("SQLite3 support is enabled")
  112. } else if setting.Database.UseSQLite3 {
  113. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  114. }
  115. mustInitCtx(ctx, common.InitDBEngine)
  116. log.Info("ORM engine initialization successful!")
  117. mustInit(appstate.Init)
  118. mustInit(oauth2.Init)
  119. models.NewRepoContext()
  120. // Booting long running goroutines.
  121. cron.NewContext()
  122. issue_indexer.InitIssueIndexer(false)
  123. code_indexer.Init()
  124. mustInit(stats_indexer.Init)
  125. mirror_service.InitSyncMirrors()
  126. webhook.InitDeliverHooks()
  127. mustInit(pull_service.Init)
  128. mustInit(task.Init)
  129. mustInit(repo_migrations.Init)
  130. eventsource.GetManager().Init()
  131. mustInitCtx(ctx, syncAppPathForGit)
  132. if setting.SSH.StartBuiltinServer {
  133. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  134. log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
  135. net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
  136. setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  137. } else {
  138. ssh.Unused()
  139. }
  140. auth.Init()
  141. svg.Init()
  142. }
  143. // NormalRoutes represents non install routes
  144. func NormalRoutes() *web.Route {
  145. r := web.NewRoute()
  146. for _, middle := range common.Middlewares() {
  147. r.Use(middle)
  148. }
  149. sessioner := session.Sessioner(session.Options{
  150. Provider: setting.SessionConfig.Provider,
  151. ProviderConfig: setting.SessionConfig.ProviderConfig,
  152. CookieName: setting.SessionConfig.CookieName,
  153. CookiePath: setting.SessionConfig.CookiePath,
  154. Gclifetime: setting.SessionConfig.Gclifetime,
  155. Maxlifetime: setting.SessionConfig.Maxlifetime,
  156. Secure: setting.SessionConfig.Secure,
  157. SameSite: setting.SessionConfig.SameSite,
  158. Domain: setting.SessionConfig.Domain,
  159. })
  160. r.Mount("/", web_routers.Routes(sessioner))
  161. r.Mount("/api/v1", apiv1.Routes(sessioner))
  162. r.Mount("/api/internal", private.Routes())
  163. return r
  164. }