conf.go 5.1 KB

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