conf.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log")
  82. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  83. config = fmt.Sprintf(
  84. `{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
  85. logPath,
  86. Cfg.MustBool(modeSec, "LOG_ROTATE", true),
  87. Cfg.MustInt(modeSec, "MAX_LINES", 1000000),
  88. 1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)),
  89. Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
  90. Cfg.MustInt(modeSec, "MAX_DAYS", 7))
  91. case "conn":
  92. config = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
  93. Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
  94. Cfg.MustBool(modeSec, "RECONNECT", false),
  95. Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
  96. Cfg.MustValue(modeSec, "ADDR", ":7020"))
  97. case "smtp":
  98. config = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
  99. Cfg.MustValue(modeSec, "USER", "[email protected]"),
  100. Cfg.MustValue(modeSec, "PASSWD", "******"),
  101. Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
  102. Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
  103. Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
  104. }
  105. log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, config)
  106. log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
  107. }
  108. func newMailService() {
  109. // Check mailer setting.
  110. if Cfg.MustBool("mailer", "ENABLED") {
  111. MailService = &Mailer{
  112. Name: Cfg.MustValue("mailer", "NAME", AppName),
  113. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  114. User: Cfg.MustValue("mailer", "USER", "[email protected]"),
  115. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  116. }
  117. log.Info("Mail Service Enabled")
  118. }
  119. }
  120. func newRegisterMailService() {
  121. if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") {
  122. return
  123. } else if MailService == nil {
  124. log.Warn("Register Mail Service: Mail Service is not enabled")
  125. return
  126. }
  127. Service.RegisterEmailConfirm = true
  128. log.Info("Register Mail Service Enabled")
  129. }
  130. func init() {
  131. var err error
  132. workDir, err := exeDir()
  133. if err != nil {
  134. fmt.Printf("Fail to get work directory: %s\n", err)
  135. os.Exit(2)
  136. }
  137. cfgPath := filepath.Join(workDir, "conf/app.ini")
  138. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  139. if err != nil {
  140. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  141. os.Exit(2)
  142. }
  143. Cfg.BlockMode = false
  144. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  145. if com.IsFile(cfgPath) {
  146. if err = Cfg.AppendFiles(cfgPath); err != nil {
  147. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  148. os.Exit(2)
  149. }
  150. }
  151. Cfg.BlockMode = false
  152. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  153. AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png")
  154. AppUrl = Cfg.MustValue("server", "ROOT_URL")
  155. Domain = Cfg.MustValue("server", "DOMAIN")
  156. SecretKey = Cfg.MustValue("security", "SECRET_KEY")
  157. }
  158. func NewServices() {
  159. newService()
  160. newLogService()
  161. newMailService()
  162. newRegisterMailService()
  163. }