conf.go 5.3 KB

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