From 0cf3101f9378f51707a6e698f050b77c630ea506 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Tue, 21 May 2024 17:36:33 +0800 Subject: [PATCH] ip --- app/mix/controller/healthcheck.go | 27 +++++++++++++++++++++++++++ app/mix/controller/init.go | 1 + 2 files changed, 28 insertions(+) diff --git a/app/mix/controller/healthcheck.go b/app/mix/controller/healthcheck.go index 32a4ff6f..1d4d6ebd 100644 --- a/app/mix/controller/healthcheck.go +++ b/app/mix/controller/healthcheck.go @@ -2,7 +2,9 @@ package controller import ( "github.com/gin-gonic/gin" + "net" "service/bizcommon/util" + "strings" "time" ) @@ -13,3 +15,28 @@ func Healthcheck(ctx *gin.Context) { } ReplyOk(ctx, data) } + +func TestIp(ctx *gin.Context) { + ReplyOk(ctx, getIp()) +} + +func getIp() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + 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/app/mix/controller/init.go b/app/mix/controller/init.go index 92073ff4..c5773b05 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -55,6 +55,7 @@ func Init(r *gin.Engine) { // healthcheck r.GET("/healthcheck", Healthcheck) + r.GET("/test/ip", TestIp) r.GET("/timeout", Timeout) // Hello World!