config.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package configs
  2. import (
  3. _ "github.com/go-sql-driver/mysql"
  4. "github.com/spf13/cast"
  5. "github.com/spf13/viper"
  6. "os"
  7. )
  8. var (
  9. Server ServerConfiguration
  10. Manager ServerManager
  11. ARKJsonFilePath string
  12. )
  13. type ServerConfiguration struct {
  14. Port string `mapstructure:"port"`
  15. Host string `mapstructure:"host"`
  16. }
  17. type ArkShopConfiguration struct {
  18. ArkShopConfigUrl string `json:"arkShopConfigUrl"` //arkshop 配置文件位置
  19. }
  20. type ServerManager struct {
  21. Username string `json:"username"`
  22. Password string `json:"password"`
  23. }
  24. func init() {
  25. LoadServerConfiguration()
  26. }
  27. func LoadServerConfiguration() {
  28. //获取项目的执行路径
  29. path, err := os.Getwd()
  30. if err != nil {
  31. panic(err)
  32. }
  33. config := viper.New()
  34. config.AddConfigPath(path + "/configs") //设置读取的文件路径
  35. config.SetConfigName("config") //设置读取的文件名
  36. config.SetConfigType("yaml") //设置文件的类型
  37. if err := config.ReadInConfig(); err != nil {
  38. panic(err)
  39. }
  40. portString := config.Get("server.port")
  41. if cast.ToString(portString) == "" {
  42. portString = "9777"
  43. }
  44. host := config.Get("server.host")
  45. Server = ServerConfiguration{
  46. Port: cast.ToString(portString),
  47. Host: cast.ToString(host),
  48. }
  49. //用户账号密码获取
  50. username := config.Get("server.manager.username")
  51. password := config.Get("server.manager.password")
  52. Manager = ServerManager{
  53. Username: cast.ToString(username),
  54. Password: cast.ToString(password),
  55. }
  56. ARKJsonFilePath = cast.ToString(config.Get("server.file.ark-file"))
  57. }