conf.go 5.0 KB

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