Skip to content

Commit f6bf318

Browse files
committed
utils:对工具类进行分组改造
1 parent 773b127 commit f6bf318

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4104
-304
lines changed

examples/pkg/app/user/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ func StartServer(wg *sync.WaitGroup) {
9595
fmt.Println("call user function [after] to do something...")
9696
job0 := "print now datetime"
9797
cancel0 := schedule.NewJob(job0, func() {
98-
fmt.Println("now: ", utils.FormatDate(time.Now(), utils.YYYY_MM_DD_HH_MM_SS_EN))
98+
fmt.Println("now: ", utils.Datetime().FormatDate(time.Now(), utils.YYYY_MM_DD_HH_MM_SS_EN))
9999
}).RunAt(schedule.EveryMinute)
100100
time.AfterFunc(time.Minute*3, cancel0)
101101

102102
job1 := "print hello"
103103
cancel1 := schedule.NewJob(job1, func() {
104104
time.Sleep(time.Second * 10)
105-
fmt.Println(utils.FormatDate(time.Now(), utils.YYYY_MM_DD_HH_MM_SS_EN), "hello")
105+
fmt.Println(utils.Datetime().FormatDate(time.Now(), utils.YYYY_MM_DD_HH_MM_SS_EN), "hello")
106106
sail.GetLogger().Info("print log info to console",
107107
zap.String("value", "go-sail"),
108108
zap.Errors("errors", []error{nil}))
@@ -141,7 +141,7 @@ func RegisterServicesToNacos(wg *sync.WaitGroup) {
141141

142142
nc := nacos.GetNamingClient()
143143
var param vo.RegisterInstanceParam
144-
localIp, err := utils.GetLocalIP()
144+
localIp, err := utils.IP().GetLocal()
145145
if err == nil {
146146
param.Ip = localIp
147147
}
@@ -168,7 +168,7 @@ func RegisterServicesToNacos(wg *sync.WaitGroup) {
168168
func UnregisterServiceFromNacos() {
169169
nc := nacos.GetNamingClient()
170170
var param vo.DeregisterInstanceParam
171-
localIp, err := utils.GetLocalIP()
171+
localIp, err := utils.IP().GetLocal()
172172
if err == nil {
173173
param.Ip = localIp
174174
}

sail/config/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func PrintTemplateConfig(format string, writeToFile ...string) {
3939
}
4040

4141
if len(writeToFile) > 0 {
42-
err := utils.FilePutContents(cfgStr, writeToFile[0])
42+
err := utils.File().PutContents(cfgStr, writeToFile[0])
4343
if err != nil {
4444
fmt.Printf("[GO-SAIL] <Config> dump config to file {%s} error: %s\n", writeToFile[0], err.Error())
4545
}

sail/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func printSummaryInfo(conf config.HttpServerConf, ginEngine *gin.Engine) {
1919
var (
2020
protocol = "http:"
2121
messages bytes.Buffer
22-
localIp, _ = utils.GetLocalIP()
22+
localIp, _ = utils.IP().GetLocal()
2323
delimiter = []byte(strings.Repeat("=", 88))
2424
subDelimiter = []byte(strings.Repeat("-", 88))
2525
repoLink = "Repository: https://github.com/keepchen/go-sail"

schedule/delay.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func (j *taskJob) RunAfter(delay time.Duration) (cancel CancelFunc) {
2626
j.task()
2727
return
2828
}
29-
if utils.RedisTryLock(j.lockerKey) {
29+
if utils.RedisLocker().TryLock(j.lockerKey) {
3030
defer func() {
31-
utils.RedisUnlock(j.lockerKey)
31+
utils.RedisLocker().Unlock(j.lockerKey)
3232
j.lockedByMe = false
3333
}()
3434
j.lockedByMe = true

schedule/interval.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func (j *taskJob) run() {
3434
j.task()
3535
return
3636
}
37-
if utils.RedisTryLock(j.lockerKey) {
37+
if utils.RedisLocker().TryLock(j.lockerKey) {
3838
defer func() {
39-
utils.RedisUnlock(j.lockerKey)
39+
utils.RedisLocker().Unlock(j.lockerKey)
4040
j.lockedByMe = false
4141
}()
4242
j.lockedByMe = true
@@ -51,7 +51,7 @@ func (j *taskJob) run() {
5151
//收到退出信号,终止任务
5252
case <-j.cancelTaskChan:
5353
if j.withoutOverlapping && j.lockedByMe {
54-
utils.RedisUnlock(j.lockerKey)
54+
utils.RedisLocker().Unlock(j.lockerKey)
5555
}
5656

5757
taskSchedules.mux.Lock()

schedule/specs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func (j *taskJob) RunAt(crontabExpr string) (cancel CancelFunc) {
4646
j.task()
4747
return
4848
}
49-
if utils.RedisTryLock(j.lockerKey) {
49+
if utils.RedisLocker().TryLock(j.lockerKey) {
5050
defer func() {
51-
utils.RedisUnlock(j.lockerKey)
51+
utils.RedisLocker().Unlock(j.lockerKey)
5252
j.lockedByMe = false
5353
}()
5454
j.lockedByMe = true

utils/aes.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,38 @@ import (
99
"io"
1010
)
1111

12-
// KEY 密钥
13-
const KEY = "fakeKeyChangeMe!"
12+
type aesImpl struct {
13+
}
14+
15+
// IAes aes接口
16+
type IAes interface {
17+
// Encode aes加密
18+
//
19+
// 使用CFB
20+
//
21+
// key应该是一个16或24或32位长度的字符
22+
Encode(rawString, key string) (string, error)
23+
// Decode aes解密
24+
//
25+
// 使用CFB
26+
//
27+
// key应该是一个16或24或32位长度的字符
28+
Decode(encryptedString, key string) (string, error)
29+
}
30+
31+
// Aes 实例化aes工具类
32+
func Aes() IAes {
33+
return &aesImpl{}
34+
}
35+
36+
var _ IAes = aesImpl{}
1437

15-
// AesEncode aes加密
38+
// Encode aes加密
1639
//
1740
// 使用CFB
1841
//
1942
// key应该是一个16或24或32位长度的字符
20-
func AesEncode(rawString, key string) (string, error) {
43+
func (aesImpl) Encode(rawString, key string) (string, error) {
2144
plainText := []byte(rawString)
2245

2346
block, err := aes.NewCipher([]byte(key))
@@ -38,12 +61,12 @@ func AesEncode(rawString, key string) (string, error) {
3861

3962
}
4063

41-
// AesDecode aes解密
64+
// Decode aes解密
4265
//
4366
// 使用CFB
4467
//
4568
// key应该是一个16或24或32位长度的字符
46-
func AesDecode(encryptedString, key string) (string, error) {
69+
func (aesImpl) Decode(encryptedString, key string) (string, error) {
4770
cipherText, err := base64.StdEncoding.DecodeString(encryptedString)
4871
if err != nil {
4972
return "", err

utils/aes_deprecated.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package utils
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
"crypto/rand"
7+
"encoding/base64"
8+
"errors"
9+
"io"
10+
)
11+
12+
// AesEncode aes加密
13+
//
14+
// Deprecated: AesEncode is deprecated,it will be removed in the future.
15+
//
16+
// Please use Aes().Encode() instead.
17+
//
18+
// 使用CFB
19+
//
20+
// key应该是一个16或24或32位长度的字符
21+
func AesEncode(rawString, key string) (string, error) {
22+
plainText := []byte(rawString)
23+
24+
block, err := aes.NewCipher([]byte(key))
25+
if err != nil {
26+
return "", err
27+
}
28+
29+
cipherText := make([]byte, aes.BlockSize+len(plainText))
30+
iv := cipherText[:aes.BlockSize]
31+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
32+
return "", err
33+
}
34+
35+
stream := cipher.NewCFBEncrypter(block, iv)
36+
stream.XORKeyStream(cipherText[aes.BlockSize:], plainText)
37+
38+
return base64.StdEncoding.EncodeToString(cipherText), nil
39+
40+
}
41+
42+
// AesDecode aes解密
43+
//
44+
// Deprecated: AesDecode is deprecated,it will be removed in the future.
45+
//
46+
// Please use Aes().Decode() instead.
47+
//
48+
// 使用CFB
49+
//
50+
// key应该是一个16或24或32位长度的字符
51+
func AesDecode(encryptedString, key string) (string, error) {
52+
cipherText, err := base64.StdEncoding.DecodeString(encryptedString)
53+
if err != nil {
54+
return "", err
55+
}
56+
57+
block, err := aes.NewCipher([]byte(key))
58+
if err != nil {
59+
return "", nil
60+
}
61+
62+
if len(cipherText) < aes.BlockSize {
63+
return "", errors.New("CipherText block size is too short")
64+
}
65+
66+
iv := cipherText[:aes.BlockSize]
67+
cipherText = cipherText[aes.BlockSize:]
68+
69+
stream := cipher.NewCFBDecrypter(block, iv)
70+
stream.XORKeyStream(cipherText, cipherText)
71+
72+
return string(cipherText), nil
73+
}

utils/aes_deprecated_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var rawStringDeprecated = "hello world!"
10+
11+
// KeyForTestDeprecated 测试密钥
12+
const KeyForTestDeprecated = "fakeKeyChangeMe!"
13+
14+
func TestAesEncode(t *testing.T) {
15+
encodedString, err := AesEncode(rawStringDeprecated, KeyForTestDeprecated)
16+
t.Log(encodedString)
17+
assert.NoError(t, err)
18+
}
19+
20+
func TestAesDecode(t *testing.T) {
21+
encodedString, err := AesEncode(rawStringDeprecated, KeyForTestDeprecated)
22+
t.Log(encodedString)
23+
assert.NoError(t, err)
24+
decodedString, err := AesDecode(encodedString, KeyForTestDeprecated)
25+
t.Log(decodedString)
26+
assert.NoError(t, err)
27+
assert.Equal(t, decodedString, rawStringDeprecated)
28+
}

utils/aes_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import (
88

99
var rawString = "hello world!"
1010

11-
func TestAesEncode(t *testing.T) {
12-
encodedString, err := AesEncode(rawString, KEY)
11+
// KeyForTest 测试密钥
12+
const KeyForTest = "fakeKeyChangeMe!"
13+
14+
func TestAesImplEncode(t *testing.T) {
15+
encodedString, err := Aes().Encode(rawString, KeyForTest)
1316
t.Log(encodedString)
1417
assert.NoError(t, err)
1518
}
1619

17-
func TestAesDecode(t *testing.T) {
18-
encodedString, err := AesEncode(rawString, KEY)
20+
func TestAesImplDecode(t *testing.T) {
21+
encodedString, err := Aes().Encode(rawString, KeyForTest)
1922
t.Log(encodedString)
2023
assert.NoError(t, err)
21-
decodedString, err := AesDecode(encodedString, KEY)
24+
decodedString, err := Aes().Decode(encodedString, KeyForTest)
2225
t.Log(decodedString)
2326
assert.NoError(t, err)
2427
assert.Equal(t, decodedString, rawString)

utils/base64.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ package utils
22

33
import "encoding/base64"
44

5-
//Base64Encode base64编码
6-
func Base64Encode(rawBytes []byte) string {
5+
type base64Impl struct {
6+
}
7+
8+
// IBase64 base64接口
9+
type IBase64 interface {
10+
// Encode base64编码
11+
Encode(rawBytes []byte) string
12+
// Decode base64解码
13+
Decode(encodedString string) ([]byte, error)
14+
}
15+
16+
// Base64 实例化base64工具类
17+
func Base64() IBase64 {
18+
return &base64Impl{}
19+
}
20+
21+
var _ IBase64 = base64Impl{}
22+
23+
// Encode base64编码
24+
func (base64Impl) Encode(rawBytes []byte) string {
725
return base64.StdEncoding.EncodeToString(rawBytes)
826
}
927

10-
//Base64Decode base64解码
11-
func Base64Decode(encodedString string) ([]byte, error) {
28+
// Decode base64解码
29+
func (base64Impl) Decode(encodedString string) ([]byte, error) {
1230
return base64.StdEncoding.DecodeString(encodedString)
1331
}

utils/base64_deprecated.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package utils
2+
3+
import "encoding/base64"
4+
5+
// Base64Encode base64编码
6+
//
7+
// Deprecated: Base64Encode is deprecated,it will be removed in the future.
8+
//
9+
// Please use Base64().Encode() instead.
10+
func Base64Encode(rawBytes []byte) string {
11+
return base64.StdEncoding.EncodeToString(rawBytes)
12+
}
13+
14+
// Base64Decode base64解码
15+
//
16+
// Deprecated: Base64Decode is deprecated,it will be removed in the future.
17+
//
18+
// Please use Base64().Decode() instead.
19+
func Base64Decode(encodedString string) ([]byte, error) {
20+
return base64.StdEncoding.DecodeString(encodedString)
21+
}

utils/base64_deprecated_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBase64Encode(t *testing.T) {
10+
encodedString := Base64Encode([]byte(rawString))
11+
t.Log(encodedString)
12+
assert.Equal(t, "aGVsbG8gd29ybGQh", encodedString)
13+
}
14+
15+
func TestBase64Decode(t *testing.T) {
16+
encodedString := Base64Encode([]byte(rawString))
17+
18+
decodeBytes, err := Base64Decode(encodedString)
19+
t.Log(rawString, encodedString, string(decodeBytes))
20+
assert.NoError(t, err)
21+
assert.Equal(t, string(decodeBytes), rawString)
22+
}

utils/base64_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func TestBase64Encode(t *testing.T) {
10-
encodedString := Base64Encode([]byte(rawString))
9+
func TestBase64ImplEncode(t *testing.T) {
10+
encodedString := Base64().Encode([]byte(rawString))
1111
t.Log(encodedString)
1212
assert.Equal(t, "aGVsbG8gd29ybGQh", encodedString)
1313
}
1414

15-
func TestBase64Decode(t *testing.T) {
16-
encodedString := Base64Encode([]byte(rawString))
15+
func TestBase64ImplDecode(t *testing.T) {
16+
encodedString := Base64().Encode([]byte(rawString))
1717

18-
decodeBytes, err := Base64Decode(encodedString)
18+
decodeBytes, err := Base64().Decode(encodedString)
1919
t.Log(rawString, encodedString, string(decodeBytes))
2020
assert.NoError(t, err)
2121
assert.Equal(t, string(decodeBytes), rawString)

0 commit comments

Comments
 (0)