gid add ip
This commit is contained in:
parent
9626ddf00c
commit
6721d078d8
@ -2,13 +2,17 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -49,10 +53,43 @@ func PanicIf(cond bool, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenGid() string {
|
func getOneHexIp() string {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("cannot get ip, default to another call")
|
||||||
|
return gNode.Generate().Base58()
|
||||||
|
}
|
||||||
|
for _, address := range addrs {
|
||||||
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
ip := ipnet.IP.To4().String()
|
||||||
|
ns := strings.Split(ip, ".")
|
||||||
|
r := []byte{}
|
||||||
|
for _, n := range ns {
|
||||||
|
r = append(r, byte(MustAtoi(n)))
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("none ipv4, default to another call")
|
||||||
return gNode.Generate().Base58()
|
return gNode.Generate().Base58()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustAtoi 走must逻辑
|
||||||
|
func MustAtoi(s string) int {
|
||||||
|
r, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
E2P(errors.New("convert to int error: " + s))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenGid() string {
|
||||||
|
return getOneHexIp() + "-" + gNode.Generate().Base58()
|
||||||
|
}
|
||||||
|
|
||||||
var gNode *snowflake.Node = nil
|
var gNode *snowflake.Node = nil
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -226,7 +263,6 @@ func GetCurrentDir() string {
|
|||||||
func GetProjectDir() string {
|
func GetProjectDir() string {
|
||||||
_, file, _, _ := runtime.Caller(1)
|
_, file, _, _ := runtime.Caller(1)
|
||||||
for ; !strings.HasSuffix(file, "/dtm"); file = filepath.Dir(file) {
|
for ; !strings.HasSuffix(file, "/dtm"); file = filepath.Dir(file) {
|
||||||
|
|
||||||
}
|
}
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -94,8 +93,3 @@ func TestResty(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.NotEqual(t, nil, err2)
|
assert.NotEqual(t, nil, err2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCaller(t *testing.T) {
|
|
||||||
p := GetProjectDir()
|
|
||||||
assert.Equal(t, true, strings.HasSuffix(filepath.Dir(p), "common"))
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user