config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/ilyakaznacheev/cleanenv"
  5. "github.com/joho/godotenv"
  6. "log"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "time"
  13. )
  14. type Config struct {
  15. Database struct {
  16. Driver string `yaml:"driver" env:"DB_DRIVER"`
  17. Port string `yaml:"port" env:"DB_PORT" env-default:"3306"`
  18. Host string `yaml:"host" env:"DB_HOST"`
  19. Name string `yaml:"name" env:"DB_DATABASE"`
  20. User string `yaml:"user" env:"DB_USERNAME"`
  21. Password string `yaml:"password" env:"DB_PASSWORD"`
  22. Charset string `yaml:"charset" env:"DB_CHARSET"`
  23. Prefix string `yaml:"prefix" env:"DB_PREFIX"`
  24. } `yaml:"database"`
  25. Redis struct {
  26. Host string `yaml:"host" env:"REDIS_HOST"`
  27. Password string `yaml:"password" env:"REDIS_PASSWORD"`
  28. Prefix string `yaml:"prefix" env:"REDIS_PREFIX"`
  29. } `yaml:"redis"`
  30. Log struct {
  31. Filename string `yaml:"filename"`
  32. MaxSize int `yaml:"max_size"`
  33. MaxBackups int `yaml:"max_backups"`
  34. MaxAge int `yaml:"max_age"`
  35. Compress bool `yaml:"compress"`
  36. } `yaml:"log"`
  37. Server struct {
  38. PidFile string `yaml:"pid_file"`
  39. RunMode string `yaml:"run_mode" env:"RUN_MODE"`
  40. HttpPort int `yaml:"http_port"`
  41. ReadTimeout time.Duration `yaml:"read_timeout"`
  42. WriteTimeout time.Duration `yaml:"write_timeout"`
  43. } `yaml:"server"`
  44. App struct {
  45. ProductionUrl string `yaml:"production_url"`
  46. TokenExpire int `yaml:"token_expire" env:"TOKEN_EXPIRE"`
  47. PageLimitMin int `yaml:"page_limit_min"`
  48. PageLimitMax int `yaml:"page_limit_max"`
  49. ImgHost string `yaml:"img_host"`
  50. ServiceTel string `yaml:"service_tel"`
  51. DataPath string `yaml:"data_path"`
  52. ExportPath string `yaml:"export_path"`
  53. WeixinPath string `yaml:"weixin_path"`
  54. } `yaml:"app"`
  55. Sms struct {
  56. AppID string `yaml:"app_id"`
  57. AccessKey string `yaml:"access_key"`
  58. AccessSecret string `yaml:"access_secret"`
  59. } `yaml:"sms"`
  60. Qiniu struct {
  61. Bucket string `yaml:"bucket"`
  62. AccessKey string `yaml:"access_key"`
  63. SecretKey string `yaml:"secret_key"`
  64. } `yaml:"qiniu"`
  65. }
  66. var Cfg Config
  67. // setup with option
  68. type Option struct {
  69. file string
  70. env string
  71. }
  72. type OptionFunc func(opt *Option)
  73. func WithFile(file string) OptionFunc {
  74. return func(opt *Option) {
  75. opt.file = file
  76. }
  77. }
  78. func WithEnv(env string) OptionFunc {
  79. return func(opt *Option) {
  80. opt.env = env
  81. }
  82. }
  83. func GetRootPath() string {
  84. dir := getCurrentAbPathByExecutable()
  85. if strings.Contains(dir, "tmp") {
  86. return getCurrentAbPathByCaller()
  87. }
  88. return dir
  89. }
  90. func getCurrentAbPathByExecutable() string {
  91. exePath, err := os.Executable()
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
  96. return res + "/"
  97. }
  98. func getCurrentAbPathByCaller() string {
  99. var abPath string
  100. _, filename, _, ok := runtime.Caller(0)
  101. if ok {
  102. abPath = filepath.Join(path.Dir(filename), "../..") + "/"
  103. }
  104. return abPath
  105. }
  106. func Setup(options ...OptionFunc) {
  107. rootPath := GetRootPath()
  108. fmt.Println(rootPath)
  109. opt := Option{
  110. file: rootPath + "config/config.yml",
  111. env: rootPath + ".env",
  112. //file: "config/config.yml",
  113. //env: ".env",
  114. }
  115. for _, fun := range options {
  116. fun(&opt)
  117. }
  118. err := godotenv.Load(opt.env)
  119. if err != nil {
  120. log.Fatal("Error loading .env file")
  121. }
  122. var config Config
  123. err = cleanenv.ReadConfig(opt.file, &config)
  124. if err != nil {
  125. panic(err)
  126. }
  127. config.Server.ReadTimeout = config.Server.ReadTimeout * time.Second
  128. config.Server.WriteTimeout = config.Server.ReadTimeout * time.Second
  129. Cfg = config
  130. }