conf.go 3.8 KB

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