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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // GlobalInit is for global configuration reload-able.
  87. func GlobalInit(ctx context.Context) {
  88. setting.NewContext()
  89. if !setting.InstallLock {
  90. log.Fatal("Gitea is not installed")
  91. }
  92. mustInitCtx(ctx, git.Init)
  93. log.Info(git.VersionInfo())
  94. git.CheckLFSVersion()
  95. log.Info("AppPath: %s", setting.AppPath)
  96. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  97. log.Info("Custom path: %s", setting.CustomPath)
  98. log.Info("Log path: %s", setting.LogRootPath)
  99. log.Info("Configuration file: %s", setting.CustomConf)
  100. log.Info("Run Mode: %s", strings.Title(setting.RunMode))
  101. // Setup i18n
  102. translation.InitLocales()
  103. InitGitServices()
  104. mailer.NewContext()
  105. mustInit(cache.NewContext)
  106. notification.NewContext()
  107. mustInit(archiver.Init)
  108. highlight.NewContext()
  109. external.RegisterRenderers()
  110. markup.Init()
  111. if setting.EnableSQLite3 {
  112. log.Info("SQLite3 support is enabled")
  113. } else if setting.Database.UseSQLite3 {
  114. log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.")
  115. }
  116. mustInitCtx(ctx, common.InitDBEngine)
  117. log.Info("ORM engine initialization successful!")
  118. mustInit(appstate.Init)
  119. mustInit(oauth2.Init)
  120. models.NewRepoContext()
  121. // Booting long running goroutines.
  122. cron.NewContext()
  123. issue_indexer.InitIssueIndexer(false)
  124. code_indexer.Init()
  125. mustInit(stats_indexer.Init)
  126. mirror_service.InitSyncMirrors()
  127. webhook.InitDeliverHooks()
  128. mustInit(pull_service.Init)
  129. mustInit(task.Init)
  130. mustInit(repo_migrations.Init)
  131. eventsource.GetManager().Init()
  132. mustInitCtx(ctx, syncAppPathForGit)
  133. if setting.SSH.StartBuiltinServer {
  134. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  135. log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
  136. net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
  137. setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  138. } else {
  139. ssh.Unused()
  140. }
  141. auth.Init()
  142. svg.Init()
  143. }
  144. // NormalRoutes represents non install routes
  145. func NormalRoutes() *web.Route {
  146. r := web.NewRoute()
  147. for _, middle := range common.Middlewares() {
  148. r.Use(middle)
  149. }
  150. sessioner := session.Sessioner(session.Options{
  151. Provider: setting.SessionConfig.Provider,
  152. ProviderConfig: setting.SessionConfig.ProviderConfig,
  153. CookieName: setting.SessionConfig.CookieName,
  154. CookiePath: setting.SessionConfig.CookiePath,
  155. Gclifetime: setting.SessionConfig.Gclifetime,
  156. Maxlifetime: setting.SessionConfig.Maxlifetime,
  157. Secure: setting.SessionConfig.Secure,
  158. SameSite: setting.SessionConfig.SameSite,
  159. Domain: setting.SessionConfig.Domain,
  160. })
  161. r.Mount("/", web_routers.Routes(sessioner))
  162. r.Mount("/api/v1", apiv1.Routes(sessioner))
  163. r.Mount("/api/internal", private.Routes())
  164. return r
  165. }