conf.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. func newLogService() {
  40. log.NewLogger()
  41. }
  42. func newMailService() {
  43. // Check mailer setting.
  44. if Cfg.MustBool("mailer", "ENABLED") {
  45. MailService = &Mailer{
  46. Name: Cfg.MustValue("mailer", "NAME", AppName),
  47. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  48. User: Cfg.MustValue("mailer", "USER", "example@example.com"),
  49. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  50. }
  51. log.Info("Mail Service Enabled")
  52. }
  53. }
  54. func init() {
  55. var err error
  56. workDir, err := exeDir()
  57. if err != nil {
  58. fmt.Printf("Fail to get work directory: %s\n", err)
  59. os.Exit(2)
  60. }
  61. cfgPath := filepath.Join(workDir, "conf/app.ini")
  62. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  63. if err != nil {
  64. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  65. os.Exit(2)
  66. }
  67. Cfg.BlockMode = false
  68. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  69. if com.IsFile(cfgPath) {
  70. if err = Cfg.AppendFiles(cfgPath); err != nil {
  71. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  72. os.Exit(2)
  73. }
  74. }
  75. Cfg.BlockMode = false
  76. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  77. Domain = Cfg.MustValue("server", "DOMAIN")
  78. // Extensions.
  79. newLogService()
  80. newMailService()
  81. }