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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/cron"
  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. repo_migrations "code.gitea.io/gitea/modules/migrations"
  26. "code.gitea.io/gitea/modules/notification"
  27. repo_module "code.gitea.io/gitea/modules/repository"
  28. "code.gitea.io/gitea/modules/setting"
  29. "code.gitea.io/gitea/modules/ssh"
  30. "code.gitea.io/gitea/modules/storage"
  31. "code.gitea.io/gitea/modules/svg"
  32. "code.gitea.io/gitea/modules/task"
  33. "code.gitea.io/gitea/modules/translation"
  34. "code.gitea.io/gitea/modules/web"
  35. apiv1 "code.gitea.io/gitea/routers/api/v1"
  36. "code.gitea.io/gitea/routers/common"
  37. "code.gitea.io/gitea/routers/private"
  38. web_routers "code.gitea.io/gitea/routers/web"
  39. "code.gitea.io/gitea/services/archiver"
  40. "code.gitea.io/gitea/services/auth"
  41. "code.gitea.io/gitea/services/auth/source/oauth2"
  42. "code.gitea.io/gitea/services/mailer"
  43. mirror_service "code.gitea.io/gitea/services/mirror"
  44. pull_service "code.gitea.io/gitea/services/pull"
  45. "code.gitea.io/gitea/services/repository"
  46. "code.gitea.io/gitea/services/webhook"
  47. "gitea.com/go-chi/session"
  48. )
  49. func mustInit(fn func() error) {
  50. err := fn()
  51. if err != nil {
  52. ptr := reflect.ValueOf(fn).Pointer()
  53. fi := runtime.FuncForPC(ptr)
  54. log.Fatal("%s failed: %v", fi.Name(), err)
  55. }
  56. }
  57. func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
  58. err := fn(ctx)
  59. if err != nil {
  60. ptr := reflect.ValueOf(fn).Pointer()
  61. fi := runtime.FuncForPC(ptr)
  62. log.Fatal("%s(ctx) failed: %v", fi.Name(), err)
  63. }
  64. }
  65. // InitGitServices init new services for git, this is also called in `contrib/pr/checkout.go`
  66. func InitGitServices() {
  67. setting.NewServices()
  68. mustInit(storage.Init)
  69. mustInit(repository.NewContext)
  70. }
  71. func syncAppPathForGit(ctx context.Context) error {
  72. runtimeState := new(appstate.RuntimeState)
  73. if err := appstate.AppState.Get(runtimeState); err != nil {
  74. return err
  75. }
  76. if runtimeState.LastAppPath != setting.AppPath {
  77. log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
  78. log.Info("re-sync repository hooks ...")
  79. mustInitCtx(ctx, repo_module.SyncRepositoryHooks)
  80. log.Info("re-write ssh public keys ...")
  81. mustInit(models.RewriteAllPublicKeys)
  82. runtimeState.LastAppPath = setting.AppPath
  83. return appstate.AppState.Set(runtimeState)
  84. }
  85. return nil
  86. }
  87. // GlobalInit is for global configuration reload-able.
  88. func GlobalInit(ctx context.Context) {
  89. setting.NewContext()
  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. InitGitServices()
  105. mailer.NewContext()
  106. mustInit(cache.NewContext)
  107. notification.NewContext()
  108. mustInit(archiver.Init)
  109. highlight.NewContext()
  110. external.RegisterRenderers()
  111. markup.Init()
  112. if setting.EnableSQLite3 {
  113. log.Info("SQLite3 Supported")
  114. } else if setting.Database.UseSQLite3 {
  115. log.Fatal("SQLite3 is set in settings but NOT Supported")
  116. }
  117. mustInitCtx(ctx, common.InitDBEngine)
  118. log.Info("ORM engine initialization successful!")
  119. mustInit(appstate.Init)
  120. mustInit(oauth2.Init)
  121. models.NewRepoContext()
  122. // Booting long running goroutines.
  123. cron.NewContext()
  124. issue_indexer.InitIssueIndexer(false)
  125. code_indexer.Init()
  126. mustInit(stats_indexer.Init)
  127. mirror_service.InitSyncMirrors()
  128. webhook.InitDeliverHooks()
  129. mustInit(pull_service.Init)
  130. mustInit(task.Init)
  131. mustInit(repo_migrations.Init)
  132. eventsource.GetManager().Init()
  133. mustInitCtx(ctx, syncAppPathForGit)
  134. if setting.SSH.StartBuiltinServer {
  135. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  136. log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
  137. net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
  138. setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  139. } else {
  140. ssh.Unused()
  141. }
  142. auth.Init()
  143. svg.Init()
  144. }
  145. // NormalRoutes represents non install routes
  146. func NormalRoutes() *web.Route {
  147. r := web.NewRoute()
  148. for _, middle := range common.Middlewares() {
  149. r.Use(middle)
  150. }
  151. sessioner := session.Sessioner(session.Options{
  152. Provider: setting.SessionConfig.Provider,
  153. ProviderConfig: setting.SessionConfig.ProviderConfig,
  154. CookieName: setting.SessionConfig.CookieName,
  155. CookiePath: setting.SessionConfig.CookiePath,
  156. Gclifetime: setting.SessionConfig.Gclifetime,
  157. Maxlifetime: setting.SessionConfig.Maxlifetime,
  158. Secure: setting.SessionConfig.Secure,
  159. SameSite: setting.SessionConfig.SameSite,
  160. Domain: setting.SessionConfig.Domain,
  161. })
  162. r.Mount("/", web_routers.Routes(sessioner))
  163. r.Mount("/api/v1", apiv1.Routes(sessioner))
  164. r.Mount("/api/internal", private.Routes())
  165. return r
  166. }