conf.go 5.2 KB

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