conf.go 6.1 KB

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