You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conf.go 4.8 kB

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