config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package configs
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/spf13/cast"
  6. "github.com/spf13/viper"
  7. "os"
  8. "xorm.io/xorm"
  9. )
  10. var (
  11. Server ServerConfiguration
  12. Manager ServerManager
  13. ARKJsonFilePath string
  14. Engine *xorm.Engine
  15. //RedisDb *redis.Client
  16. )
  17. type ServerConfiguration struct {
  18. Port string `mapstructure:"port"`
  19. Host string `mapstructure:"host"`
  20. }
  21. type ArkShopConfiguration struct {
  22. ArkShopConfigUrl string `json:"arkShopConfigUrl"` //arkshop 配置文件位置
  23. }
  24. type ServerManager struct {
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. }
  28. func init() {
  29. LoadServerConfiguration()
  30. ConfigInit()
  31. }
  32. func LoadServerConfiguration() {
  33. //获取项目的执行路径
  34. path, err := os.Getwd()
  35. if err != nil {
  36. panic(err)
  37. }
  38. config := viper.New()
  39. config.AddConfigPath(path + "/configs") //设置读取的文件路径
  40. config.SetConfigName("config") //设置读取的文件名
  41. config.SetConfigType("yaml") //设置文件的类型
  42. if err := config.ReadInConfig(); err != nil {
  43. panic(err)
  44. }
  45. portString := config.Get("server.port")
  46. if cast.ToString(portString) == "" {
  47. portString = "9777"
  48. }
  49. host := config.Get("server.host")
  50. Server = ServerConfiguration{
  51. Port: cast.ToString(portString),
  52. Host: cast.ToString(host),
  53. }
  54. //用户账号密码获取
  55. username := config.Get("server.manager.username")
  56. password := config.Get("server.manager.password")
  57. Manager = ServerManager{
  58. Username: cast.ToString(username),
  59. Password: cast.ToString(password),
  60. }
  61. ARKJsonFilePath = cast.ToString(config.Get("server.file.ark-file"))
  62. }
  63. func ConfigInit() {
  64. //获取项目的执行路径
  65. path, err := os.Getwd()
  66. if err != nil {
  67. panic(err)
  68. }
  69. config := viper.New()
  70. config.AddConfigPath(path + "/configs") //设置读取的文件路径
  71. config.SetConfigName("config") //设置读取的文件名
  72. config.SetConfigType("yaml") //设置文件的类型
  73. //尝试进行配置读取
  74. if err := config.ReadInConfig(); err != nil {
  75. panic(err)
  76. }
  77. fmt.Println(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
  78. Engine, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
  79. Engine.Ping() //连接测试
  80. //TODO 显示sql
  81. Engine.ShowSQL(true)
  82. Engine.Logger().ShowSQL(true)
  83. //RedisDb = redis.NewClient(&redis.Options{
  84. // Addr: fmt.Sprintf("%s:%s", config.Get("redis.host"), config.Get("redis.port")), // Redis地址
  85. // Password: "", // Redis密码,如果没有则为空字符串
  86. // DB: 0, // 使用默认DB
  87. //})
  88. ////开启debug模式
  89. //
  90. //Config = config
  91. fmt.Println("xorm 数据库orm框架初始化成功")
  92. //PayConfigInit()
  93. }