SJA APS后端代码
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.

104 lines
2.7 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 config
  2. import (
  3. "github.com/spf13/viper"
  4. "gopkg.in/yaml.v2"
  5. "leit.com/leit_seat_aps/common"
  6. "os"
  7. "fmt"
  8. )
  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. SalveDbType string `yaml:"salvedbtype"`
  18. SalveServer string `yaml:"salveserver"`
  19. SalveIp string `yaml:"salveip"`
  20. SalveUser string `yaml:"salveuser"`
  21. SalvePassword string `yaml:"salvepassword"`
  22. SalveDbName string `yaml:"salvedbname"`
  23. SalvePort int `yaml:"salveport"`
  24. Finr int `yaml:"finr"`
  25. Filefolder string `yaml:"filefolder"`
  26. Crontime string `yaml:"crontime"`
  27. TemplatePath string `yaml:"templatepath"`
  28. ReadTaskInterval int `yaml:"readtaskinterval"`
  29. PrinterType string `yaml:"printertype"`
  30. Inbox string `yaml:"inbox"`
  31. Outbox string `yaml:"outbox"`
  32. Day int `yaml:"day"`
  33. Num int `yaml:"num"`
  34. TimeInterval int64 `yaml:"timeinterval"`
  35. Printobjtype string `yaml:"printobjtype"`
  36. Msgtype string `yaml:"msgtype"`
  37. Begtime string `yaml:"begtime"`
  38. Endtime string `yaml:"endtime"`
  39. Shiptog bool `yaml:"shiptog"`
  40. Packtog bool `yaml:"packtog"`
  41. }
  42. var ConfValue EnvConfig
  43. //read yaml config
  44. //注:path为yaml或yml文件的路径
  45. func ReadYamlConfig(path string) (*EnvConfig, error) {
  46. f, err := os.Open(path)
  47. if err != nil {
  48. return nil, err
  49. } else {
  50. yaml.NewDecoder(f).Decode(&ConfValue)
  51. }
  52. defer f.Close()
  53. return &ConfValue, nil
  54. }
  55. var AppConfig Config
  56. type Config struct {
  57. *App `mapstructure:"app"`
  58. *ETCD `mapstructure:"etcd"`
  59. }
  60. type App struct {
  61. Port int `yaml:"port"`
  62. TaskNums int `yaml:"tasknums"`
  63. ErrNums int `yaml:"errnums"`
  64. ShellPath string `yaml:"shellpath"`
  65. LocalAddr string `yaml:"localaddr"`
  66. Mod string `yaml:"mod"`
  67. Name string `yaml:"name"`
  68. UseETCD bool `yaml:"useetcd"`
  69. }
  70. type ETCD struct {
  71. Addrs []string `yaml:"addrs"`
  72. Timeout int `yaml:"timeout"`
  73. LockLease int64 `yaml:"locklease"`
  74. ServiceLease int64 `yaml:"servicelease"`
  75. }
  76. func InitConfig() (err error) {
  77. baseDir, err := common.GetCurrentPath("config")
  78. fmt.Println("conf:", baseDir)
  79. if err != nil {
  80. return
  81. }
  82. viper.SetConfigName("app_config")
  83. viper.SetConfigType("yaml")
  84. viper.AddConfigPath(baseDir)
  85. if err = viper.ReadInConfig(); err != nil {
  86. return
  87. }
  88. if err = viper.Unmarshal(&AppConfig); err != nil {
  89. return
  90. }
  91. return nil
  92. }