setting.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package setting
  2. import (
  3. "errors"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "github.com/go-ini/ini"
  11. )
  12. type Database struct {
  13. Type string
  14. User string
  15. Password string
  16. Host string
  17. Name string
  18. ShowXormlog bool
  19. ShowGetSqllog bool
  20. CacheApiSql bool
  21. TablePrefix string
  22. FarmCode string
  23. }
  24. var DatabaseSetting = &Database{}
  25. var cfg *ini.File
  26. var CurrentPath string
  27. func GetCurrentPath() (string, error) {
  28. file, err := exec.LookPath(os.Args[0])
  29. if err != nil {
  30. return "", err
  31. }
  32. path, err := filepath.Abs(file)
  33. if err != nil {
  34. return "", err
  35. }
  36. //fmt.Println("path111:", path)
  37. if runtime.GOOS == "windows" {
  38. path = strings.Replace(path, "\\", "/", -1)
  39. }
  40. //fmt.Println("path222:", path)
  41. i := strings.LastIndex(path, "/")
  42. if i < 0 {
  43. return "", errors.New(`Can't find "/" or "\".`)
  44. }
  45. //fmt.Println("path333:", path)
  46. return string(path[0 : i+1]), nil
  47. }
  48. func Setup(dir string) {
  49. var err error
  50. if strings.Trim(dir, " ") == "" {
  51. CurrentPath, err = GetCurrentPath()
  52. } else {
  53. CurrentPath = dir
  54. }
  55. cfg, err = ini.Load(CurrentPath + "app.ini")
  56. if err != nil {
  57. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  58. }
  59. mapTo("database", DatabaseSetting)
  60. DatabaseSetting.Host = DatabaseSetting.Host + ":3326"
  61. DatabaseSetting.Type = "mysql"
  62. DatabaseSetting.User = "root"
  63. DatabaseSetting.Password = "root"
  64. DatabaseSetting.Name = "tmrwatch2"
  65. }
  66. func mapTo(section string, v interface{}) {
  67. err := cfg.Section(section).MapTo(v)
  68. if err != nil {
  69. log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
  70. }
  71. }