conf.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Domain string
  26. Cfg *goconfig.ConfigFile
  27. MailService *Mailer
  28. )
  29. func exeDir() (string, error) {
  30. file, err := exec.LookPath(os.Args[0])
  31. if err != nil {
  32. return "", err
  33. }
  34. p, err := filepath.Abs(file)
  35. if err != nil {
  36. return "", err
  37. }
  38. return path.Dir(p), nil
  39. }
  40. var logLevels = map[string]string{
  41. "Trace": "0",
  42. "Debug": "1",
  43. "Info": "2",
  44. "Warn": "3",
  45. "Error": "4",
  46. "Critical": "5",
  47. }
  48. func newLogService() {
  49. // Get and check log mode.
  50. mode := Cfg.MustValue("log", "MODE", "console")
  51. modeSec := "log." + mode
  52. if _, err := Cfg.GetSection(modeSec); err != nil {
  53. fmt.Printf("Unknown log mode: %s\n", mode)
  54. os.Exit(2)
  55. }
  56. // Log level.
  57. levelName := Cfg.MustValue("log."+mode, "LEVEL", "Trace")
  58. level, ok := logLevels[levelName]
  59. if !ok {
  60. fmt.Printf("Unknown log level: %s\n", levelName)
  61. os.Exit(2)
  62. }
  63. // Generate log configuration.
  64. var config string
  65. switch mode {
  66. case "console":
  67. config = fmt.Sprintf(`{"level":%s}`, level)
  68. case "file":
  69. config = fmt.Sprintf(
  70. `{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
  71. Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log"),
  72. Cfg.MustBool(modeSec, "LOG_ROTATE", true),
  73. Cfg.MustInt(modeSec, "MAX_LINES", 1000000),
  74. 1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)),
  75. Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
  76. Cfg.MustInt(modeSec, "MAX_DAYS", 7))
  77. case "conn":
  78. config = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
  79. Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
  80. Cfg.MustBool(modeSec, "RECONNECT", false),
  81. Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
  82. Cfg.MustValue(modeSec, "ADDR", ":7020"))
  83. case "smtp":
  84. config = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
  85. Cfg.MustValue(modeSec, "USER", "[email protected]"),
  86. Cfg.MustValue(modeSec, "PASSWD", "******"),
  87. Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
  88. Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
  89. Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
  90. }
  91. log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, config)
  92. log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
  93. }
  94. func newMailService() {
  95. // Check mailer setting.
  96. if Cfg.MustBool("mailer", "ENABLED") {
  97. MailService = &Mailer{
  98. Name: Cfg.MustValue("mailer", "NAME", AppName),
  99. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  100. User: Cfg.MustValue("mailer", "USER", "[email protected]"),
  101. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  102. }
  103. log.Info("Mail Service Enabled")
  104. }
  105. }
  106. func init() {
  107. var err error
  108. workDir, err := exeDir()
  109. if err != nil {
  110. fmt.Printf("Fail to get work directory: %s\n", err)
  111. os.Exit(2)
  112. }
  113. cfgPath := filepath.Join(workDir, "conf/app.ini")
  114. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  115. if err != nil {
  116. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  117. os.Exit(2)
  118. }
  119. Cfg.BlockMode = false
  120. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  121. if com.IsFile(cfgPath) {
  122. if err = Cfg.AppendFiles(cfgPath); err != nil {
  123. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  124. os.Exit(2)
  125. }
  126. }
  127. Cfg.BlockMode = false
  128. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  129. Domain = Cfg.MustValue("server", "DOMAIN")
  130. // Extensions.
  131. newLogService()
  132. newMailService()
  133. }