自动更新管控端
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.
 
 
 
 
 
 

72 lines
1.4 KiB

package config
import (
"fmt"
"net"
"scagnet/util"
"go.uber.org/zap"
"gopkg.in/ini.v1"
)
type Config struct {
DeviceName string
Port string
LocalIP string
FilePath string
Version string
}
var G Config
// 本地ip
func GetLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
var ips []string
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
if len(ips) == 0 {
return "", fmt.Errorf("get local ip failed")
} else if len(ips) == 1 {
return ips[0], nil
} else {
// Select the one connected to the network
// when there are multiple network interfaces
// Is there a better way?
c, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ips[0], nil
}
defer c.Close()
return c.LocalAddr().(*net.UDPAddr).IP.String(), nil
}
}
// 读取配置文件
func ReadConfig() {
logger := util.NewProductionLogger()
defer logger.Sync()
// 加载文件
cfg, err := ini.Load("app.conf")
if err != nil {
logger.Error("读取配置文件失败", zap.Error(err))
return
}
//
iport, err := cfg.Section("rpc").Key("port").Int()
if err != nil {
logger.Error("读取配置文件失败", zap.Error(err))
return
}
G.Port = fmt.Sprintf("%d", iport)
}