conf.go 6.4 KB

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