package configs

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/cast"
	"github.com/spf13/viper"
	"os"
)

var (
	Server  ServerConfiguration
	Manager ServerManager
)

type ServerConfiguration struct {
	Port string `mapstructure:"port"`
	Host string `mapstructure:"host"`
}
type ArkShopConfiguration struct {
	ArkShopConfigUrl string `json:"arkShopConfigUrl"` //arkshop 配置文件位置
}
type ServerManager struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func init() {
	LoadServerConfiguration()
}

func LoadServerConfiguration() {
	//获取项目的执行路径
	path, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	config := viper.New()

	config.AddConfigPath(path + "/configs") //设置读取的文件路径
	config.SetConfigName("config")          //设置读取的文件名
	config.SetConfigType("yaml")            //设置文件的类型
	if err := config.ReadInConfig(); err != nil {
		panic(err)
	}
	portString := config.Get("server.port")
	if cast.ToString(portString) == "" {
		portString = "9777"
	}
	host := config.Get("server.host")

	Server = ServerConfiguration{
		Port: cast.ToString(portString),
		Host: cast.ToString(host),
	}
	//用户账号密码获取
	username := config.Get("server.manager.username")
	password := config.Get("server.manager.password")
	Manager = ServerManager{
		Username: cast.ToString(username),
		Password: cast.ToString(password),
	}

}
