This commit is contained in:
lwl0608 2023-12-24 13:24:01 +08:00
parent 5cc64f205d
commit ac89ddbf74
2 changed files with 50 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"net"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
@ -21,6 +22,7 @@ import (
"service/library/redis" "service/library/redis"
"service/library/sms" "service/library/sms"
"service/library/validator" "service/library/validator"
"strings"
"time" "time"
) )
@ -95,7 +97,7 @@ func main() {
validator.InitDefaultNotNullValidator() validator.InitDefaultNotNullValidator()
controller.Init(router) controller.Init(router)
srv := &http.Server{ 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, Handler: router,
} }
httpserver.StartHttpServer(srv, cfg.DefaultConfig) httpserver.StartHttpServer(srv, cfg.DefaultConfig)
@ -107,3 +109,25 @@ func PrintAndExit(msg string) {
time.Sleep(1) //wait logger flush time.Sleep(1) //wait logger flush
os.Exit(1) 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
}

25
main.go
View File

@ -4,10 +4,12 @@ import (
"fmt" "fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/bwmarrin/snowflake" "github.com/bwmarrin/snowflake"
"net"
"os" "os"
"service/library/configcenter" "service/library/configcenter"
"service/library/redis" "service/library/redis"
"strconv" "strconv"
"strings"
) )
func main() { func main() {
@ -18,8 +20,31 @@ func main() {
n, _ := snowflake.NewNode(6) n, _ := snowflake.NewNode(6)
fmt.Println(n.Generate().Time()) 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() { func UploadImage() {
// yourEndpoint填写Bucket对应的Endpoint以华东1杭州为例填写为https://oss-cn-hangzhou.aliyuncs.com。其它Region请按实际情况填写。 // yourEndpoint填写Bucket对应的Endpoint以华东1杭州为例填写为https://oss-cn-hangzhou.aliyuncs.com。其它Region请按实际情况填写。
// yourAccessKeyId以及yourAccessKeySecret分别填写从STS服务获取的临时访问密钥AccessKey ID和AccessKey Secret // yourAccessKeyId以及yourAccessKeySecret分别填写从STS服务获取的临时访问密钥AccessKey ID和AccessKey Secret