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.

214 lines
5.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs 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 base
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/goconfig"
  14. "github.com/gogits/cache"
  15. "github.com/gogits/gogs/modules/log"
  16. )
  17. // Mailer represents a mail service.
  18. type Mailer struct {
  19. Name string
  20. Host string
  21. User, Passwd string
  22. }
  23. var (
  24. AppVer string
  25. AppName string
  26. AppLogo string
  27. AppUrl string
  28. Domain string
  29. SecretKey string
  30. RunUser string
  31. RepoRootPath string
  32. Cfg *goconfig.ConfigFile
  33. MailService *Mailer
  34. Cache cache.Cache
  35. CacheAdapter string
  36. CacheConfig string
  37. )
  38. var Service struct {
  39. RegisterEmailConfirm bool
  40. DisenableRegisteration bool
  41. RequireSignInView bool
  42. ActiveCodeLives int
  43. ResetPwdCodeLives int
  44. }
  45. func exeDir() (string, error) {
  46. file, err := exec.LookPath(os.Args[0])
  47. if err != nil {
  48. return "", err
  49. }
  50. p, err := filepath.Abs(file)
  51. if err != nil {
  52. return "", err
  53. }
  54. return path.Dir(p), nil
  55. }
  56. var logLevels = map[string]string{
  57. "Trace": "0",
  58. "Debug": "1",
  59. "Info": "2",
  60. "Warn": "3",
  61. "Error": "4",
  62. "Critical": "5",
  63. }
  64. func newService() {
  65. Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180)
  66. Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180)
  67. Service.DisenableRegisteration = Cfg.MustBool("service", "DISENABLE_REGISTERATION", false)
  68. Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW", false)
  69. }
  70. func newLogService() {
  71. // Get and check log mode.
  72. mode := Cfg.MustValue("log", "MODE", "console")
  73. modeSec := "log." + mode
  74. if _, err := Cfg.GetSection(modeSec); err != nil {
  75. fmt.Printf("Unknown log mode: %s\n", mode)
  76. os.Exit(2)
  77. }
  78. // Log level.
  79. levelName := Cfg.MustValue("log."+mode, "LEVEL", "Trace")
  80. level, ok := logLevels[levelName]
  81. if !ok {
  82. fmt.Printf("Unknown log level: %s\n", levelName)
  83. os.Exit(2)
  84. }
  85. // Generate log configuration.
  86. var config string
  87. switch mode {
  88. case "console":
  89. config = fmt.Sprintf(`{"level":%s}`, level)
  90. case "file":
  91. logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log")
  92. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  93. config = fmt.Sprintf(
  94. `{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
  95. logPath,
  96. Cfg.MustBool(modeSec, "LOG_ROTATE", true),
  97. Cfg.MustInt(modeSec, "MAX_LINES", 1000000),
  98. 1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)),
  99. Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
  100. Cfg.MustInt(modeSec, "MAX_DAYS", 7))
  101. case "conn":
  102. config = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
  103. Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
  104. Cfg.MustBool(modeSec, "RECONNECT", false),
  105. Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
  106. Cfg.MustValue(modeSec, "ADDR", ":7020"))
  107. case "smtp":
  108. config = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
  109. Cfg.MustValue(modeSec, "USER", "example@example.com"),
  110. Cfg.MustValue(modeSec, "PASSWD", "******"),
  111. Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
  112. Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
  113. Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
  114. }
  115. log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, config)
  116. log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
  117. }
  118. func newMailService() {
  119. // Check mailer setting.
  120. if Cfg.MustBool("mailer", "ENABLED") {
  121. MailService = &Mailer{
  122. Name: Cfg.MustValue("mailer", "NAME", AppName),
  123. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  124. User: Cfg.MustValue("mailer", "USER", "example@example.com"),
  125. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  126. }
  127. log.Info("Mail Service Enabled")
  128. }
  129. }
  130. func newRegisterMailService() {
  131. if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") {
  132. return
  133. } else if MailService == nil {
  134. log.Warn("Register Mail Service: Mail Service is not enabled")
  135. return
  136. }
  137. Service.RegisterEmailConfirm = true
  138. log.Info("Register Mail Service Enabled")
  139. }
  140. func NewConfigContext() {
  141. var err error
  142. workDir, err := exeDir()
  143. if err != nil {
  144. fmt.Printf("Fail to get work directory: %s\n", err)
  145. os.Exit(2)
  146. }
  147. cfgPath := filepath.Join(workDir, "conf/app.ini")
  148. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  149. if err != nil {
  150. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  151. os.Exit(2)
  152. }
  153. Cfg.BlockMode = false
  154. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  155. if com.IsFile(cfgPath) {
  156. if err = Cfg.AppendFiles(cfgPath); err != nil {
  157. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  158. os.Exit(2)
  159. }
  160. }
  161. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  162. AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png")
  163. AppUrl = Cfg.MustValue("server", "ROOT_URL")
  164. Domain = Cfg.MustValue("server", "DOMAIN")
  165. SecretKey = Cfg.MustValue("security", "SECRET_KEY")
  166. RunUser = Cfg.MustValue("", "RUN_USER")
  167. CacheAdapter = Cfg.MustValue("cache", "ADAPTER")
  168. CacheConfig = Cfg.MustValue("cache", "CONFIG")
  169. Cache, err = cache.NewCache(CacheAdapter, CacheConfig)
  170. if err != nil {
  171. fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n",
  172. CacheAdapter, CacheConfig, err)
  173. os.Exit(2)
  174. }
  175. // Determine and create root git reposiroty path.
  176. RepoRootPath = Cfg.MustValue("repository", "ROOT")
  177. if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
  178. fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
  179. os.Exit(2)
  180. }
  181. }
  182. func NewServices() {
  183. newService()
  184. newLogService()
  185. newMailService()
  186. newRegisterMailService()
  187. }