conf.go 5.7 KB

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