conf.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. )
  14. var (
  15. AppVer string
  16. AppName string
  17. Domain string
  18. Cfg *goconfig.ConfigFile
  19. )
  20. func exeDir() (string, error) {
  21. file, err := exec.LookPath(os.Args[0])
  22. if err != nil {
  23. return "", err
  24. }
  25. p, err := filepath.Abs(file)
  26. if err != nil {
  27. return "", err
  28. }
  29. return path.Dir(p), nil
  30. }
  31. func init() {
  32. var err error
  33. workDir, err := exeDir()
  34. if err != nil {
  35. fmt.Printf("Fail to get work directory: %s\n", err)
  36. os.Exit(2)
  37. }
  38. cfgPath := filepath.Join(workDir, "conf/app.ini")
  39. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  40. if err != nil {
  41. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  42. os.Exit(2)
  43. }
  44. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  45. if com.IsFile(cfgPath) {
  46. if err = Cfg.AppendFiles(cfgPath); err != nil {
  47. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  48. os.Exit(2)
  49. }
  50. }
  51. Cfg.BlockMode = false
  52. AppName = Cfg.MustValue("", "APP_NAME")
  53. Domain = Cfg.MustValue("server", "DOMAIN")
  54. }