diff --git a/app/mix/cmd/main.go b/app/mix/cmd/main.go index a0e5af37..ae796b64 100644 --- a/app/mix/cmd/main.go +++ b/app/mix/cmd/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net" "net/http" "os" "runtime" @@ -21,6 +22,7 @@ import ( "service/library/redis" "service/library/sms" "service/library/validator" + "strings" "time" ) @@ -95,7 +97,7 @@ func main() { validator.InitDefaultNotNullValidator() controller.Init(router) srv := &http.Server{ - Addr: fmt.Sprintf("%s:%d", cfg.App.Ip, cfg.App.Port), + Addr: fmt.Sprintf("%s:%d", GetIp(), cfg.App.Port), Handler: router, } httpserver.StartHttpServer(srv, cfg.DefaultConfig) @@ -107,3 +109,25 @@ func PrintAndExit(msg string) { time.Sleep(1) //wait logger flush os.Exit(1) } + +func GetIp() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + PrintAndExit("get ip fail") + return "127.0.0.1" + } + retIp := "" + for _, address := range addrs { + // 检查ip地址判断是否回环地址 + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + ip := ipnet.IP.String() + if strings.HasPrefix(ip, "172.") { + retIp = ip + break + } + } + } + } + return retIp +} diff --git a/main.go b/main.go index 2dd1f6c9..d0a7500d 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,12 @@ import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/bwmarrin/snowflake" + "net" "os" "service/library/configcenter" "service/library/redis" "strconv" + "strings" ) func main() { @@ -18,8 +20,31 @@ func main() { n, _ := snowflake.NewNode(6) fmt.Println(n.Generate().Time()) + + fmt.Println(GetIp()) } +func GetIp() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + fmt.Println(err) + return "127.0.0.1" + } + retIp := "" + for _, address := range addrs { + // 检查ip地址判断是否回环地址 + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + ip := ipnet.IP.String() + if strings.HasPrefix(ip, "172.") { + retIp = ip + break + } + } + } + } + return retIp +} func UploadImage() { // yourEndpoint填写Bucket对应的Endpoint,以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com。其它Region请按实际情况填写。 // yourAccessKeyId以及yourAccessKeySecret分别填写从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。