mirror of https://github.com/shunfei/cronsun
pkg: 替换 util 包
parent
7f7e6b4ae4
commit
5d67135ac7
|
@ -9,10 +9,11 @@ import (
|
|||
client "github.com/coreos/etcd/clientv3"
|
||||
|
||||
"sunteng/commons/log"
|
||||
"sunteng/commons/util"
|
||||
|
||||
"github.com/shunfei/cronsun/conf"
|
||||
"github.com/shunfei/cronsun/models"
|
||||
"github.com/shunfei/cronsun/node/cron"
|
||||
"github.com/shunfei/cronsun/utils"
|
||||
)
|
||||
|
||||
// Node 执行 cron 命令服务的结构体
|
||||
|
@ -35,7 +36,7 @@ type Node struct {
|
|||
}
|
||||
|
||||
func NewNode(cfg *conf.Conf) (n *Node, err error) {
|
||||
ip, err := util.GetLocalIP()
|
||||
ip, err := utils.LocalIP()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
// 获取本机 ip
|
||||
// 获取第一个非 loopback ip
|
||||
func LocalIP() (net.IP, error) {
|
||||
tables, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, t := range tables {
|
||||
addrs, err := t.Addrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
if v4 := ipnet.IP.To4(); v4 != nil {
|
||||
return v4, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("cannot find local IP address")
|
||||
}
|
Loading…
Reference in New Issue