SJA工艺
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package conf
  2. import (
  3. "LAPP_SJA_ME/utils"
  4. "github.com/spf13/viper"
  5. "gopkg.in/yaml.v2"
  6. "os"
  7. )
  8. var DbConfig EnvConfig
  9. type EnvConfig struct {
  10. DbType string `yaml:"dbtype"`
  11. Server string `yaml:"server"`
  12. Ip string `yaml:"ip"`
  13. User string `yaml:"user"`
  14. Password string `yaml:"password"`
  15. DbName string `yaml:"dbname"`
  16. Port int `yaml:"port"`
  17. TemplatePath string `yaml:"templatepath"`
  18. ReadTaskInterval int `yaml:"readtaskinterval"`
  19. PrinterType string `yaml:"printertype"`
  20. Printnum int `yaml:"printnum"`
  21. Msgtype string `yaml:"msgtype"`
  22. Finr int `yaml:"finr"`
  23. Inbox string `yaml:"inbox"`
  24. Outbox string `yaml:"outbox"`
  25. Log string `yaml:"log"`
  26. App string `yaml:"app"`
  27. AppType string `yaml:"apptype"`
  28. }
  29. //read yaml config
  30. //注:path为yaml或yml文件的路径
  31. func ReadYamlConfig() error {
  32. path, err := utils.GetCurrentPath("conf/config.yaml")
  33. if err != nil {
  34. return err
  35. }
  36. f, err := os.Open(path)
  37. if err != nil {
  38. return err
  39. } else {
  40. yaml.NewDecoder(f).Decode(&DbConfig)
  41. }
  42. defer f.Close()
  43. return nil
  44. }
  45. var AppInfo Config
  46. type Config struct {
  47. *App `mapstructure:"app"`
  48. *ETCD `mapstructure:"etcd"`
  49. }
  50. type App struct {
  51. Port int `yaml:"port"`
  52. TaskNums int `yaml:"tasknums"`
  53. ErrNums int `yaml:"errnums"`
  54. ShellPath string `yaml:"shellpath"`
  55. LocalAddr string `yaml:"localaddr"`
  56. Mod string `yaml:"mod"`
  57. Name string `yaml:"name"`
  58. UseETCD bool `yaml:"useetcd"`
  59. }
  60. type ETCD struct {
  61. Addrs []string `yaml:"addrs"`
  62. Timeout int `yaml:"timeout"`
  63. LockLease int64 `yaml:"locklease"`
  64. ServiceLease int64 `yaml:"servicelease"`
  65. }
  66. func InitConfig() (err error) {
  67. baseDir, err := utils.GetCurrentPath("conf")
  68. if err != nil {
  69. return
  70. }
  71. viper.SetConfigName("app_config")
  72. viper.SetConfigType("yaml")
  73. viper.AddConfigPath(baseDir)
  74. if err = viper.ReadInConfig(); err != nil {
  75. return
  76. }
  77. if err = viper.Unmarshal(&AppInfo); err != nil {
  78. return
  79. }
  80. return nil
  81. }