Compare commits
129 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d82c3abf6 | ||
|
|
55034efa87 | ||
|
|
3ff45d6f49 | ||
|
|
2b2c285c90 | ||
|
|
29ad1bc741 | ||
|
|
a263ab5938 | ||
|
|
6b34d32a8f | ||
|
|
3ff6ca5905 | ||
|
|
59ce71f72e | ||
|
|
49da653ed8 | ||
|
|
12db6910dc | ||
|
|
3ce1aa14b5 | ||
|
|
c67fbfa849 | ||
|
|
72b94c5035 | ||
|
|
01ea13d283 | ||
|
|
91378b26dd | ||
|
|
46dc74195b | ||
|
|
63316b6b23 | ||
|
|
1b8600e04d | ||
|
|
11141d022f | ||
|
|
8fd29bd81c | ||
|
|
820779e614 | ||
|
|
d45dc35ea6 | ||
|
|
b3280ee290 | ||
|
|
ff0e89aa5f | ||
|
|
6ff5ba67e8 | ||
|
|
e7474523ba | ||
|
|
0b928aed14 | ||
|
|
52dcd3da1f | ||
|
|
0260d6f735 | ||
|
|
364636ea61 | ||
|
|
12f816cb5e | ||
|
|
ef70d3465d | ||
|
|
a3a9e726cb | ||
|
|
0c65774c82 | ||
|
|
fa31e547a2 | ||
|
|
c406536511 | ||
|
|
35f48e4f5d | ||
|
|
851e982ab9 | ||
|
|
956186613f | ||
|
|
aeb2a0c4f9 | ||
|
|
4f05671af2 | ||
|
|
6b6170b183 | ||
|
|
29f2aa2654 | ||
|
|
ed234b58bc | ||
|
|
cfcad531f4 | ||
|
|
12ffdfd849 | ||
|
|
c2be7d70b8 | ||
|
|
efe723bc51 | ||
|
|
501559e3b8 | ||
|
|
de8b3c2195 | ||
|
|
31aeb21e09 | ||
|
|
f1cf89a78c | ||
|
|
d6df5e1c48 | ||
|
|
049b9b52dd | ||
|
|
c53259008e | ||
|
|
6e0a0a099d | ||
|
|
f4313de55f | ||
|
|
fd3823255c | ||
|
|
79823f4317 | ||
|
|
99fd015066 | ||
|
|
f63c2d867d | ||
|
|
eefb497c20 | ||
|
|
1c8564d817 | ||
|
|
1359997f48 | ||
|
|
4a52d43273 | ||
|
|
fb68de23c1 | ||
|
|
c1b7cf11c3 | ||
|
|
311751596c | ||
|
|
4132dee4bc | ||
|
|
8e886cd410 | ||
|
|
d6f896298d | ||
|
|
2ca8aa4b44 | ||
|
|
783052a0b4 | ||
|
|
5bda9e4b51 | ||
|
|
f7cbf051bd | ||
|
|
4d7e82d0a2 | ||
|
|
bf2ffeb15c | ||
|
|
7b3290800d | ||
|
|
eaebb6df5a | ||
|
|
acdddf5e12 | ||
|
|
1224758b5b | ||
|
|
5b32343a2c | ||
|
|
8afd21f2f4 | ||
|
|
3e76bf6e5a | ||
|
|
ed74adebac | ||
|
|
1400dff676 | ||
|
|
8e3360b368 | ||
|
|
e80441639c | ||
|
|
07bf994b68 | ||
|
|
344ef4d597 | ||
|
|
144b9b9519 | ||
|
|
38841d6d75 | ||
|
|
6d1603e253 | ||
|
|
6ce23c9913 | ||
|
|
fd8c9adbf7 | ||
|
|
ea40dc7534 | ||
|
|
37c8a8c955 | ||
|
|
2a0e4bba4b | ||
|
|
dd252b7b87 | ||
|
|
2de63863a7 | ||
|
|
5d00d53ea5 | ||
|
|
dcf04d64bd | ||
|
|
d36f7d01df | ||
|
|
a0ff24adb6 | ||
|
|
5597a0af6c | ||
|
|
c196a85a59 | ||
|
|
ba638d4e1d | ||
|
|
7f8abccd2a | ||
|
|
c0f540cc2c | ||
|
|
bd905ff1a9 | ||
|
|
3b8d1b4cd8 | ||
|
|
f86180b93c | ||
|
|
cfb1864fd2 | ||
|
|
1d0a66a156 | ||
|
|
b4d4f6460e | ||
|
|
e2de9799c0 | ||
|
|
9b9c6471f7 | ||
|
|
d30b10baee | ||
|
|
55f7189a1c | ||
|
|
0ff6fb002d | ||
|
|
a5d34565c5 | ||
|
|
3c17ba0a8b | ||
|
|
a8c0c64071 | ||
|
|
82ed22a464 | ||
|
|
b190479d44 | ||
|
|
ed7b586137 | ||
|
|
ea19635fe5 | ||
|
|
29cd7da6e4 |
@@ -76,8 +76,10 @@ function build() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# build sql
|
# build sql
|
||||||
echo "building sql ..."
|
if [ $TAG = "plus" ]; then
|
||||||
${ROOT}/sql.sh
|
echo "building sql ..."
|
||||||
|
${ROOT}/sql.sh
|
||||||
|
fi
|
||||||
|
|
||||||
# copy files
|
# copy files
|
||||||
echo "copying ..."
|
echo "copying ..."
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -68,6 +69,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Println("done")
|
fmt.Println("done")
|
||||||
})
|
})
|
||||||
|
app.On("goman", func() {
|
||||||
|
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||||
|
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
|
} else {
|
||||||
|
instancesJSON, err := json.MarshalIndent(reply.Params, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(instancesJSON))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
app.Run(func() {
|
app.Run(func() {
|
||||||
nodes.NewAPINode().Start()
|
nodes.NewAPINode().Start()
|
||||||
})
|
})
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -4,20 +4,20 @@ go 1.15
|
|||||||
|
|
||||||
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
|
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
|
||||||
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||||
github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
|
github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
|
||||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183
|
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183
|
||||||
|
github.com/andybalholm/brotli v1.0.4
|
||||||
github.com/cespare/xxhash/v2 v2.1.1
|
github.com/cespare/xxhash/v2 v2.1.1
|
||||||
github.com/go-acme/lego/v4 v4.5.2
|
github.com/go-acme/lego/v4 v4.5.2
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible
|
github.com/go-yaml/yaml v2.1.0+incompatible
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
||||||
github.com/json-iterator/go v1.1.11 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
|
|||||||
8
go.sum
8
go.sum
@@ -50,6 +50,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
|
|||||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183 h1:dkj8/dxOQ4L1XpwCzRLqukvUBbxuNdz3FeyvHFnRjmo=
|
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183 h1:dkj8/dxOQ4L1XpwCzRLqukvUBbxuNdz3FeyvHFnRjmo=
|
||||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
|
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
|
||||||
|
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||||
|
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
||||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
@@ -232,6 +234,8 @@ github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7Q
|
|||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24 h1:1cGulkD2SNJJRok5OKwyhP/Ddm+PgSWKOupn0cR36/A=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
||||||
@@ -248,6 +252,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
|
|||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
||||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
@@ -318,6 +324,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
|
|||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
|
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
|
||||||
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
|
|||||||
@@ -13,9 +13,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,14 +55,12 @@ func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var requestId int64 = 1_0000_0000_0000_0000
|
|
||||||
|
|
||||||
bulk := &strings.Builder{}
|
bulk := &strings.Builder{}
|
||||||
indexName := this.FormatVariables(this.config.Index)
|
indexName := this.FormatVariables(this.config.Index)
|
||||||
typeName := this.FormatVariables(this.config.MappingType)
|
typeName := this.FormatVariables(this.config.MappingType)
|
||||||
for _, accessLog := range accessLogs {
|
for _, accessLog := range accessLogs {
|
||||||
if len(accessLog.RequestId) == 0 {
|
if len(accessLog.RequestId) == 0 {
|
||||||
accessLog.RequestId = strconv.FormatInt(time.Now().UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + fmt.Sprintf("%08d", 1)
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
opData, err := json.Marshal(map[string]interface{}{
|
opData, err := json.Marshal(map[string]interface{}{
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
package accesslogs
|
package accesslogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -5,7 +7,6 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
@@ -49,23 +50,6 @@ func (this *StorageManager) Start() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
this.locker.Lock()
|
|
||||||
storage, ok := this.storageMap[policyId]
|
|
||||||
this.locker.Unlock()
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !storage.IsOk() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return storage.Write(accessLogs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop 更新
|
// Loop 更新
|
||||||
func (this *StorageManager) Loop() error {
|
func (this *StorageManager) Loop() error {
|
||||||
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)
|
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)
|
||||||
|
|||||||
15
internal/accesslogs/storage_manager_write.go
Normal file
15
internal/accesslogs/storage_manager_write.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
//go:build community
|
||||||
|
// +build community
|
||||||
|
|
||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package configs
|
package configs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/go-yaml/yaml"
|
"github.com/go-yaml/yaml"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -11,9 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var sharedAPIConfig *APIConfig = nil
|
var sharedAPIConfig *APIConfig = nil
|
||||||
var PaddingId string
|
|
||||||
|
|
||||||
// API节点配置
|
// APIConfig API节点配置
|
||||||
type APIConfig struct {
|
type APIConfig struct {
|
||||||
NodeId string `yaml:"nodeId" json:"nodeId"`
|
NodeId string `yaml:"nodeId" json:"nodeId"`
|
||||||
Secret string `yaml:"secret" json:"secret"`
|
Secret string `yaml:"secret" json:"secret"`
|
||||||
@@ -21,7 +19,7 @@ type APIConfig struct {
|
|||||||
numberId int64 // 数字ID
|
numberId int64 // 数字ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取共享配置
|
// SharedAPIConfig 获取共享配置
|
||||||
func SharedAPIConfig() (*APIConfig, error) {
|
func SharedAPIConfig() (*APIConfig, error) {
|
||||||
sharedLocker.Lock()
|
sharedLocker.Lock()
|
||||||
defer sharedLocker.Unlock()
|
defer sharedLocker.Unlock()
|
||||||
@@ -72,7 +70,7 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
{
|
{
|
||||||
dbConfigFile := Tea.ConfigFile("db.yaml")
|
dbConfigFile := Tea.ConfigFile("db.yaml")
|
||||||
_, err := os.Stat(dbConfigFile)
|
_, err := os.Stat(dbConfigFile)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
paths := []string{}
|
paths := []string{}
|
||||||
homeDir, homeErr := os.UserHomeDir()
|
homeDir, homeErr := os.UserHomeDir()
|
||||||
if homeErr == nil {
|
if homeErr == nil {
|
||||||
@@ -96,18 +94,18 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置数字ID
|
// SetNumberId 设置数字ID
|
||||||
func (this *APIConfig) SetNumberId(numberId int64) {
|
func (this *APIConfig) SetNumberId(numberId int64) {
|
||||||
this.numberId = numberId
|
this.numberId = numberId
|
||||||
PaddingId = fmt.Sprintf("%08d", numberId)
|
teaconst.NodeId = numberId
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取数字ID
|
// NumberId 获取数字ID
|
||||||
func (this *APIConfig) NumberId() int64 {
|
func (this *APIConfig) NumberId() int64 {
|
||||||
return this.numberId
|
return this.numberId
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存到文件
|
// WriteFile 保存到文件
|
||||||
func (this *APIConfig) WriteFile(path string) error {
|
func (this *APIConfig) WriteFile(path string) error {
|
||||||
data, err := yaml.Marshal(this)
|
data, err := yaml.Marshal(this)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.3.2"
|
Version = "0.3.7"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,10 +18,10 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.3.2"
|
NodeVersion = "0.3.7"
|
||||||
UserNodeVersion = "0.0.10"
|
UserNodeVersion = "0.2.1"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.3"
|
MonitorNodeVersion = "0.0.3"
|
||||||
DNSNodeVersion = "0.2.0"
|
DNSNodeVersion = "0.2.1"
|
||||||
ReportNodeVersion = "0.1.0"
|
ReportNodeVersion = "0.1.0"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ package teaconst
|
|||||||
var (
|
var (
|
||||||
IsPlus = false
|
IsPlus = false
|
||||||
MaxNodes int32 = 0
|
MaxNodes int32 = 0
|
||||||
|
NodeId int64 = 0
|
||||||
)
|
)
|
||||||
|
|||||||
80
internal/db/models/accounts/user_account_daily_stat_dao.go
Normal file
80
internal/db/models/accounts/user_account_daily_stat_dao.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserAccountDailyStatDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserAccountDailyStatDAO() *UserAccountDailyStatDAO {
|
||||||
|
return dbs.NewDAO(&UserAccountDailyStatDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserAccountDailyStats",
|
||||||
|
Model: new(UserAccountDailyStat),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserAccountDailyStatDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserAccountDailyStatDAO *UserAccountDailyStatDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserAccountDailyStatDAO = NewUserAccountDailyStatDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDailyStat 更新当天统计数据
|
||||||
|
func (this *UserAccountDailyStatDAO) UpdateDailyStat(tx *dbs.Tx) error {
|
||||||
|
var day = timeutil.Format("Ymd")
|
||||||
|
var month = timeutil.Format("Ym")
|
||||||
|
income, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountIncomeEventTypes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
expense, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountExpenseEventTypes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if expense < 0 {
|
||||||
|
expense = -expense
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
|
"day": day,
|
||||||
|
"month": month,
|
||||||
|
"income": income,
|
||||||
|
"expense": expense,
|
||||||
|
}, maps.Map{
|
||||||
|
"income": income,
|
||||||
|
"expense": expense,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindDailyStats 查看按天统计
|
||||||
|
func (this *UserAccountDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Between("day", dayFrom, dayTo).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindMonthlyStats 查看某月统计
|
||||||
|
func (this *UserAccountDailyStatDAO) FindMonthlyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Result("SUM(income) AS income", "SUM(expense) AS expense", "month").
|
||||||
|
Between("day", dayFrom, dayTo).
|
||||||
|
Group("month").
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
22
internal/db/models/accounts/user_account_daily_stat_model.go
Normal file
22
internal/db/models/accounts/user_account_daily_stat_model.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
// UserAccountDailyStat 账户每日统计
|
||||||
|
type UserAccountDailyStat struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Day string `field:"day"` // YYYYMMDD
|
||||||
|
Month string `field:"month"` // YYYYMM
|
||||||
|
Income float64 `field:"income"` // 收入
|
||||||
|
Expense float64 `field:"expense"` // 支出
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserAccountDailyStatOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Day interface{} // YYYYMMDD
|
||||||
|
Month interface{} // YYYYMM
|
||||||
|
Income interface{} // 收入
|
||||||
|
Expense interface{} // 支出
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserAccountDailyStatOperator() *UserAccountDailyStatOperator {
|
||||||
|
return &UserAccountDailyStatOperator{}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
253
internal/db/models/accounts/user_account_dao.go
Normal file
253
internal/db/models/accounts/user_account_dao.go
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
goman.New(func() {
|
||||||
|
// 自动支付账单任务
|
||||||
|
var ticker = time.NewTicker(12 * time.Hour)
|
||||||
|
for range ticker.C {
|
||||||
|
if SharedUserAccountDAO.Instance != nil {
|
||||||
|
err := SharedUserAccountDAO.Instance.RunTx(func(tx *dbs.Tx) error {
|
||||||
|
return SharedUserAccountDAO.PayBills(tx)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("USER_ACCOUNT_DAO", "pay bills task failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserAccountDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserAccountDAO() *UserAccountDAO {
|
||||||
|
return dbs.NewDAO(&UserAccountDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserAccounts",
|
||||||
|
Model: new(UserAccount),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserAccountDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserAccountDAO *UserAccountDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserAccountDAO = NewUserAccountDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindUserAccountWithUserId 根据用户ID查找用户账户
|
||||||
|
func (this *UserAccountDAO) FindUserAccountWithUserId(tx *dbs.Tx, userId int64) (*UserAccount, error) {
|
||||||
|
if userId <= 0 {
|
||||||
|
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户是否存在
|
||||||
|
user, err := models.SharedUserDAO.FindEnabledUser(tx, userId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
account, err := this.Query(tx).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if account != nil {
|
||||||
|
return account.(*UserAccount), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var op = NewUserAccountOperator()
|
||||||
|
op.UserId = userId
|
||||||
|
_, err = this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return this.FindUserAccountWithUserId(tx, userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindUserAccountWithAccountId 根据ID查找用户账户
|
||||||
|
func (this *UserAccountDAO) FindUserAccountWithAccountId(tx *dbs.Tx, accountId int64) (*UserAccount, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(accountId).
|
||||||
|
Find()
|
||||||
|
if one != nil {
|
||||||
|
return one.(*UserAccount), nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserAccount 操作用户账户
|
||||||
|
func (this *UserAccountDAO) UpdateUserAccount(tx *dbs.Tx, accountId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||||
|
account, err := this.FindUserAccountWithAccountId(tx, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if account == nil {
|
||||||
|
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
||||||
|
}
|
||||||
|
var userId = int64(account.UserId)
|
||||||
|
var deltaFloat64 = float64(delta)
|
||||||
|
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
|
||||||
|
return errors.New("not enough account quota to decrease")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 操作账户
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(account.Id).
|
||||||
|
Set("total", dbs.SQL("total+:delta")).
|
||||||
|
Param("delta", delta).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成日志
|
||||||
|
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, accountId, delta, 0, eventType, description, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserAccountFrozen 操作用户账户冻结余额
|
||||||
|
func (this *UserAccountDAO) UpdateUserAccountFrozen(tx *dbs.Tx, userId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||||
|
account, err := this.FindUserAccountWithUserId(tx, userId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var deltaFloat64 = float64(delta)
|
||||||
|
if deltaFloat64 < 0 && account.TotalFrozen < -deltaFloat64 {
|
||||||
|
return errors.New("not enough account frozen quota to decrease")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 操作账户
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(account.Id).
|
||||||
|
Set("totalFrozen", dbs.SQL("total+:delta")).
|
||||||
|
Param("delta", delta).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成日志
|
||||||
|
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, int64(account.Id), 0, delta, eventType, description, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountAllAccounts 计算所有账户数量
|
||||||
|
func (this *UserAccountDAO) CountAllAccounts(tx *dbs.Tx, keyword string) (int64, error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
||||||
|
query.Param("keyword", keyword)
|
||||||
|
} else {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
||||||
|
}
|
||||||
|
return query.Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAccounts 列出单页账户
|
||||||
|
func (this *UserAccountDAO) ListAccounts(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*UserAccount, err error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
||||||
|
query.Param("keyword", keyword)
|
||||||
|
} else {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
|
DescPk().
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PayBills 尝试自动支付账单
|
||||||
|
func (this *UserAccountDAO) PayBills(tx *dbs.Tx) error {
|
||||||
|
bills, err := models.SharedUserBillDAO.FindUnpaidBills(tx, 10000)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先支付久远的
|
||||||
|
lists.Reverse(bills)
|
||||||
|
|
||||||
|
for _, bill := range bills {
|
||||||
|
if bill.Amount <= 0 {
|
||||||
|
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
account, err := SharedUserAccountDAO.FindUserAccountWithUserId(tx, int64(bill.UserId))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if account == nil || account.Total < bill.Amount {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 扣款
|
||||||
|
err = SharedUserAccountDAO.UpdateUserAccount(tx, int64(account.Id), -float32(bill.Amount), userconfigs.AccountEventTypePayBill, "支付账单"+bill.Code, maps.Map{"billId": bill.Id})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 改为已支付
|
||||||
|
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckUserAccount 检查用户账户
|
||||||
|
func (this *UserAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
|
||||||
|
exists, err := this.Query(tx).
|
||||||
|
Pk(accountId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
18
internal/db/models/accounts/user_account_dao_test.go
Normal file
18
internal/db/models/accounts/user_account_dao_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUserAccountDAO_PayBills(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := NewUserAccountDAO().PayBills(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
128
internal/db/models/accounts/user_account_log_dao.go
Normal file
128
internal/db/models/accounts/user_account_log_dao.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserAccountLogDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserAccountLogDAO() *UserAccountLogDAO {
|
||||||
|
return dbs.NewDAO(&UserAccountLogDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserAccountLogs",
|
||||||
|
Model: new(UserAccountLog),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserAccountLogDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserAccountLogDAO *UserAccountLogDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserAccountLogDAO = NewUserAccountLogDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAccountLog 生成用户账户日志
|
||||||
|
func (this *UserAccountLogDAO) CreateAccountLog(tx *dbs.Tx, userId int64, accountId int64, delta float32, deltaFrozen float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||||
|
var op = NewUserAccountLogOperator()
|
||||||
|
op.UserId = userId
|
||||||
|
op.AccountId = accountId
|
||||||
|
op.Delta = delta
|
||||||
|
op.DeltaFrozen = deltaFrozen
|
||||||
|
|
||||||
|
account, err := SharedUserAccountDAO.FindUserAccountWithAccountId(tx, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if account == nil {
|
||||||
|
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
||||||
|
}
|
||||||
|
op.Total = account.Total
|
||||||
|
op.TotalFrozen = account.TotalFrozen
|
||||||
|
|
||||||
|
op.EventType = eventType
|
||||||
|
op.Description = description
|
||||||
|
|
||||||
|
if params == nil {
|
||||||
|
params = maps.Map{}
|
||||||
|
}
|
||||||
|
op.Params = params.AsJSON()
|
||||||
|
|
||||||
|
op.Day = timeutil.Format("Ymd")
|
||||||
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return SharedUserAccountDailyStatDAO.UpdateDailyStat(tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountAccountLogs 计算日志数量
|
||||||
|
func (this *UserAccountLogDAO) CountAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string) (int64, error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
if accountId > 0 {
|
||||||
|
query.Attr("accountId", accountId)
|
||||||
|
}
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
||||||
|
query.Param("keyword", "%"+keyword+"%")
|
||||||
|
}
|
||||||
|
if len(eventType) > 0 {
|
||||||
|
query.Attr("eventType", eventType)
|
||||||
|
}
|
||||||
|
return query.Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAccountLogs 列出单页日志
|
||||||
|
func (this *UserAccountLogDAO) ListAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string, offset int64, size int64) (result []*UserAccountLog, err error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
if accountId > 0 {
|
||||||
|
query.Attr("accountId", accountId)
|
||||||
|
}
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
||||||
|
query.Param("keyword", "%"+keyword+"%")
|
||||||
|
}
|
||||||
|
if len(eventType) > 0 {
|
||||||
|
query.Attr("eventType", eventType)
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
|
DescPk().
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumDailyEventTypes 统计某天数据总和
|
||||||
|
func (this *UserAccountLogDAO) SumDailyEventTypes(tx *dbs.Tx, day string, eventTypes []userconfigs.AccountEventType) (float32, error) {
|
||||||
|
if len(eventTypes) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
Attr("eventType", eventTypes).
|
||||||
|
Sum("delta", 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return types.Float32(result), nil
|
||||||
|
}
|
||||||
6
internal/db/models/accounts/user_account_log_dao_test.go
Normal file
6
internal/db/models/accounts/user_account_log_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
36
internal/db/models/accounts/user_account_log_model.go
Normal file
36
internal/db/models/accounts/user_account_log_model.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
// UserAccountLog 用户账户日志
|
||||||
|
type UserAccountLog struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
AccountId uint64 `field:"accountId"` // 账户ID
|
||||||
|
Delta float64 `field:"delta"` // 操作余额的数量(可为负)
|
||||||
|
DeltaFrozen float64 `field:"deltaFrozen"` // 操作冻结的数量(可为负)
|
||||||
|
Total float64 `field:"total"` // 操作后余额
|
||||||
|
TotalFrozen float64 `field:"totalFrozen"` // 操作后冻结余额
|
||||||
|
EventType string `field:"eventType"` // 类型
|
||||||
|
Description string `field:"description"` // 描述文字
|
||||||
|
Day string `field:"day"` // YYYYMMDD
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 时间
|
||||||
|
Params string `field:"params"` // 参数
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserAccountLogOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
AccountId interface{} // 账户ID
|
||||||
|
Delta interface{} // 操作余额的数量(可为负)
|
||||||
|
DeltaFrozen interface{} // 操作冻结的数量(可为负)
|
||||||
|
Total interface{} // 操作后余额
|
||||||
|
TotalFrozen interface{} // 操作后冻结余额
|
||||||
|
EventType interface{} // 类型
|
||||||
|
Description interface{} // 描述文字
|
||||||
|
Day interface{} // YYYYMMDD
|
||||||
|
CreatedAt interface{} // 时间
|
||||||
|
Params interface{} // 参数
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserAccountLogOperator() *UserAccountLogOperator {
|
||||||
|
return &UserAccountLogOperator{}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
20
internal/db/models/accounts/user_account_model.go
Normal file
20
internal/db/models/accounts/user_account_model.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
// UserAccount 用户账号
|
||||||
|
type UserAccount struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
Total float64 `field:"total"` // 可用总余额
|
||||||
|
TotalFrozen float64 `field:"totalFrozen"` // 冻结余额
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserAccountOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
Total interface{} // 可用总余额
|
||||||
|
TotalFrozen interface{} // 冻结余额
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserAccountOperator() *UserAccountOperator {
|
||||||
|
return &UserAccountOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/user_account_model_ext.go
Normal file
1
internal/db/models/accounts/user_account_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
@@ -8,10 +8,13 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -219,7 +222,6 @@ func (this *APINodeDAO) CountAllEnabledAndOnAPINodes(tx *dbs.Tx) (int64, error)
|
|||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// CountAllEnabledAndOnOfflineAPINodes 计算API节点数量
|
// CountAllEnabledAndOnOfflineAPINodes 计算API节点数量
|
||||||
func (this *APINodeDAO) CountAllEnabledAndOnOfflineAPINodes(tx *dbs.Tx) (int64, error) {
|
func (this *APINodeDAO) CountAllEnabledAndOnOfflineAPINodes(tx *dbs.Tx) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -305,3 +307,67 @@ func (this *APINodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (i
|
|||||||
Param("version", utils.VersionToLong(version)).
|
Param("version", utils.VersionToLong(version)).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountAllEnabledAPINodesWithSSLPolicyIds 计算使用SSL策略的所有API节点数量
|
||||||
|
func (this *APINodeDAO) CountAllEnabledAPINodesWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
|
||||||
|
if len(sslPolicyIds) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
policyStringIds := []string{}
|
||||||
|
for _, policyId := range sslPolicyIds {
|
||||||
|
policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
|
||||||
|
}
|
||||||
|
return this.Query(tx).
|
||||||
|
State(APINodeStateEnabled).
|
||||||
|
Where("(FIND_IN_SET(JSON_EXTRACT(https, '$.sslPolicyRef.sslPolicyId'), :policyIds) OR FIND_IN_SET(JSON_EXTRACT(restHTTPS, '$.sslPolicyRef.sslPolicyId'), :policyIds))").
|
||||||
|
Param("policyIds", strings.Join(policyStringIds, ",")).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledAPIAccessIPs 获取所有的API可访问IP地址
|
||||||
|
func (this *APINodeDAO) FindAllEnabledAPIAccessIPs(tx *dbs.Tx, cacheMap *utils.CacheMap) ([]string, error) {
|
||||||
|
var cacheKey = this.Table + ":FindAllEnabledAPIAccessIPs"
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.([]string), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
State(APINodeStateEnabled).
|
||||||
|
Result("JSON_EXTRACT(accessAddrs, '$[*].host') AS host").
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
for _, one := range ones {
|
||||||
|
var host = one.GetString("host")
|
||||||
|
if len(host) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var ips = []string{}
|
||||||
|
err = json.Unmarshal([]byte(host), &ips)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ip := range ips {
|
||||||
|
if !lists.ContainsString(result, ip) {
|
||||||
|
if net.ParseIP(ip) == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -27,6 +28,12 @@ func TestAPINodeDAO_FindEnabledAPINodeIdWithAddr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAPINodeDAO_FindAllEnabledAPIAccessIPs(t *testing.T) {
|
||||||
|
var cacheMap = utils.NewCacheMap()
|
||||||
|
t.Log(NewAPINodeDAO().FindAllEnabledAPIAccessIPs(nil, cacheMap))
|
||||||
|
t.Log(NewAPINodeDAO().FindAllEnabledAPIAccessIPs(nil, cacheMap))
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkAPINodeDAO_New(b *testing.B) {
|
func BenchmarkAPINodeDAO_New(b *testing.B) {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(1)
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DecodeHTTP 解析HTTP配置
|
// DecodeHTTP 解析HTTP配置
|
||||||
@@ -27,11 +27,7 @@ func (this *APINode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DecodeHTTPS 解析HTTPS配置
|
// DecodeHTTPS 解析HTTPS配置
|
||||||
func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) {
|
func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
|
||||||
if cacheMap == nil {
|
|
||||||
cacheMap = maps.Map{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !IsNotNull(this.Https) {
|
if !IsNotNull(this.Https) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -123,9 +119,9 @@ func (this *APINode) DecodeRestHTTP() (*serverconfigs.HTTPProtocolConfig, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DecodeRestHTTPS 解析HTTPS配置
|
// DecodeRestHTTPS 解析HTTPS配置
|
||||||
func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) {
|
func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
if this.RestIsOn != 1 {
|
if this.RestIsOn != 1 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
@@ -93,16 +93,3 @@ func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error {
|
|||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPlus 判断是否为企业版
|
|
||||||
func (this *AuthorityKeyDAO) IsPlus(tx *dbs.Tx) (bool, error) {
|
|
||||||
key, err := this.ReadKey(tx)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if key == nil {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
teaconst.IsPlus = key.DayTo >= timeutil.Format("Y-m-d")
|
|
||||||
return teaconst.IsPlus, nil
|
|
||||||
}
|
|
||||||
|
|||||||
14
internal/db/models/authority/authority_key_dao_community.go
Normal file
14
internal/db/models/authority/authority_key_dao_community.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
//go:build community
|
||||||
|
// +build community
|
||||||
|
|
||||||
|
package authority
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsPlus 判断是否为企业版
|
||||||
|
func (this *AuthorityKeyDAO) IsPlus(tx *dbs.Tx) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -85,7 +86,7 @@ func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browser
|
|||||||
|
|
||||||
browserId, err := this.Query(tx).
|
browserId, err := this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :browserName)").
|
Where("JSON_CONTAINS(codes, :browserName)").
|
||||||
Param("browserName", "\""+browserName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("browserName", strconv.Quote(browserName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -85,7 +86,7 @@ func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemNam
|
|||||||
|
|
||||||
systemId, err := this.Query(tx).
|
systemId, err := this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :systemName)").
|
Where("JSON_CONTAINS(codes, :systemName)").
|
||||||
Param("systemName", "\""+systemName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("systemName", strconv.Quote(systemName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -49,7 +50,9 @@ type NSAccessLogDAOWrapper struct {
|
|||||||
func init() {
|
func init() {
|
||||||
initializer := NewDBNodeInitializer()
|
initializer := NewDBNodeInitializer()
|
||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
go initializer.Start()
|
goman.New(func() {
|
||||||
|
initializer.Start()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +356,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
||||||
}
|
}
|
||||||
@@ -397,7 +400,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -58,14 +59,13 @@ func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledDNSDomain 查找启用中的条目
|
// FindEnabledDNSDomain 查找启用中的条目
|
||||||
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap maps.Map) (*DNSDomain, error) {
|
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap *utils.CacheMap) (*DNSDomain, error) {
|
||||||
if cacheMap == nil {
|
|
||||||
cacheMap = maps.Map{}
|
|
||||||
}
|
|
||||||
var cacheKey = this.Table + ":record:" + types.String(domainId)
|
var cacheKey = this.Table + ":record:" + types.String(domainId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
if cacheMap != nil {
|
||||||
if cache != nil {
|
cache, _ := cacheMap.Get(cacheKey)
|
||||||
return cache.(*DNSDomain), nil
|
if cache != nil {
|
||||||
|
return cache.(*DNSDomain), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
@@ -75,7 +75,9 @@ func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cache
|
|||||||
if result == nil {
|
if result == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cacheMap[cacheKey] = result
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
return result.(*DNSDomain), err
|
return result.(*DNSDomain), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +216,7 @@ func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeC
|
|||||||
// ExistAvailableDomains 判断是否有域名可选
|
// ExistAvailableDomains 判断是否有域名可选
|
||||||
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
|
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
|
||||||
subQuery, err := SharedDNSProviderDAO.Query(tx).
|
subQuery, err := SharedDNSProviderDAO.Query(tx).
|
||||||
Where("state=1"). // 这里要使用非变量
|
Where("state=1"). // 这里要使用非变量
|
||||||
ResultPk().
|
ResultPk().
|
||||||
AsSQL()
|
AsSQL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -263,7 +265,6 @@ func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordNa
|
|||||||
func (this *DNSDomainDAO) FindEnabledDomainWithName(tx *dbs.Tx, providerId int64, domainName string) (*DNSDomain, error) {
|
func (this *DNSDomainDAO) FindEnabledDomainWithName(tx *dbs.Tx, providerId int64, domainName string) (*DNSDomain, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
State(DNSDomainStateEnabled).
|
State(DNSDomainStateEnabled).
|
||||||
Attr("isOn", true).
|
|
||||||
Attr("providerId", providerId).
|
Attr("providerId", providerId).
|
||||||
Attr("name", domainName).
|
Attr("name", domainName).
|
||||||
Find()
|
Find()
|
||||||
@@ -280,3 +281,11 @@ func (this *DNSDomainDAO) UpdateDomainIsUp(tx *dbs.Tx, domainId int64, isUp bool
|
|||||||
Set("isUp", isUp).
|
Set("isUp", isUp).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateDomainIsDeleted 设置域名为删除
|
||||||
|
func (this *DNSDomainDAO) UpdateDomainIsDeleted(tx *dbs.Tx, domainId int64, isDeleted bool) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(domainId).
|
||||||
|
Set("isDeleted", isDeleted).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type DNSDomain struct {
|
|||||||
Routes string `field:"routes"` // 线路数据
|
Routes string `field:"routes"` // 线路数据
|
||||||
IsUp uint8 `field:"isUp"` // 是否在线
|
IsUp uint8 `field:"isUp"` // 是否在线
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
|
IsDeleted uint8 `field:"isDeleted"` // 是否已删除
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSDomainOperator struct {
|
type DNSDomainOperator struct {
|
||||||
@@ -33,6 +34,7 @@ type DNSDomainOperator struct {
|
|||||||
Routes interface{} // 线路数据
|
Routes interface{} // 线路数据
|
||||||
IsUp interface{} // 是否在线
|
IsUp interface{} // 是否在线
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
IsDeleted interface{} // 是否已删除
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSDomainOperator() *DNSDomainOperator {
|
func NewDNSDomainOperator() *DNSDomainOperator {
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查IP地址
|
// 检查IP地址
|
||||||
ipAddr, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, nodeconfigs.NodeRoleNode)
|
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, nodeconfigs.NodeRoleNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,29 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -28,10 +33,53 @@ type HTTPAccessLogDAO dbs.DAO
|
|||||||
|
|
||||||
var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
||||||
|
|
||||||
|
// 队列
|
||||||
|
var oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
|
||||||
|
var accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
|
||||||
|
var accessLogQueueMaxLength = 100_000
|
||||||
|
var accessLogQueuePercent = 100 // 0-100
|
||||||
|
var accessLogCountPerSecond = 10_000 // 0 表示不限制
|
||||||
|
var accessLogConfigJSON = []byte{}
|
||||||
|
var accessLogQueueChanged = make(chan zero.Zero, 1)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dbs.OnReady(func() {
|
dbs.OnReady(func() {
|
||||||
SharedHTTPAccessLogDAO = NewHTTPAccessLogDAO()
|
SharedHTTPAccessLogDAO = NewHTTPAccessLogDAO()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 队列相关
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 检查队列变化
|
||||||
|
goman.New(func() {
|
||||||
|
var ticker = time.NewTicker(60 * time.Second)
|
||||||
|
|
||||||
|
// 先执行一次初始化
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
|
||||||
|
// 循环执行
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
case <-accessLogQueueChanged:
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 导出队列内容
|
||||||
|
goman.New(func() {
|
||||||
|
var ticker = time.NewTicker(1 * time.Second)
|
||||||
|
for range ticker.C {
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(tx, accessLogCountPerSecond)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
||||||
@@ -47,6 +95,31 @@ func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
|||||||
|
|
||||||
// CreateHTTPAccessLogs 创建访问日志
|
// CreateHTTPAccessLogs 创建访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error {
|
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
// 写入队列
|
||||||
|
var queue = accessLogQueue // 这样写非常重要,防止在写入过程中队列有切换
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
if accessLog.FirewallPolicyId == 0 { // 如果是WAF记录,则采取采样率
|
||||||
|
// 采样率
|
||||||
|
if accessLogQueuePercent <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if accessLogQueuePercent < 100 && rands.Int(1, 100) > accessLogQueuePercent {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case queue <- accessLog:
|
||||||
|
default:
|
||||||
|
// 超出的丢弃
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DumpAccessLogsFromQueue 从队列导入访问日志
|
||||||
|
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) error {
|
||||||
dao := randomHTTPAccessLogDAO()
|
dao := randomHTTPAccessLogDAO()
|
||||||
if dao == nil {
|
if dao == nil {
|
||||||
dao = &HTTPAccessLogDAOWrapper{
|
dao = &HTTPAccessLogDAOWrapper{
|
||||||
@@ -54,75 +127,102 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
|
|||||||
NodeId: 0,
|
NodeId: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.CreateHTTPAccessLogsWithDAO(tx, dao, accessLogs)
|
|
||||||
|
if size <= 0 {
|
||||||
|
size = 1_000_000
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制变量,防止中途改变
|
||||||
|
var oldQueue = oldAccessLogQueue
|
||||||
|
var newQueue = accessLogQueue
|
||||||
|
|
||||||
|
Loop:
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
// old
|
||||||
|
select {
|
||||||
|
case accessLog := <-oldQueue:
|
||||||
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue Loop
|
||||||
|
default:
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// new
|
||||||
|
select {
|
||||||
|
case accessLog := <-newQueue:
|
||||||
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue Loop
|
||||||
|
default:
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateHTTPAccessLogsWithDAO 使用特定的DAO创建访问日志
|
// CreateHTTPAccessLog 写入单条访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *HTTPAccessLogDAOWrapper, accessLogs []*pb.HTTPAccessLog) error {
|
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
||||||
if daoWrapper == nil {
|
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
||||||
return errors.New("dao should not be nil")
|
tableDef, err := findHTTPAccessLogTable(dao.Instance, day, false)
|
||||||
}
|
if err != nil {
|
||||||
if len(accessLogs) == 0 {
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dao := daoWrapper.DAO
|
fields := map[string]interface{}{}
|
||||||
|
fields["serverId"] = accessLog.ServerId
|
||||||
|
fields["nodeId"] = accessLog.NodeId
|
||||||
|
fields["status"] = accessLog.Status
|
||||||
|
fields["createdAt"] = accessLog.Timestamp
|
||||||
|
fields["requestId"] = accessLog.RequestId
|
||||||
|
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
||||||
|
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
||||||
|
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
||||||
|
fields["firewallRuleId"] = accessLog.FirewallRuleId
|
||||||
|
|
||||||
// TODO 改成事务批量提交,以加快速度
|
if len(accessLog.RequestBody) > 0 {
|
||||||
|
fields["requestBody"] = accessLog.RequestBody
|
||||||
|
accessLog.RequestBody = nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
if tableDef.HasRemoteAddr {
|
||||||
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
fields["remoteAddr"] = accessLog.RemoteAddr
|
||||||
tableDef, err := findHTTPAccessLogTable(dao.Instance, day, false)
|
}
|
||||||
if err != nil {
|
if tableDef.HasDomain {
|
||||||
return err
|
fields["domain"] = accessLog.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := map[string]interface{}{}
|
content, err := json.Marshal(accessLog)
|
||||||
fields["serverId"] = accessLog.ServerId
|
if err != nil {
|
||||||
fields["nodeId"] = accessLog.NodeId
|
return err
|
||||||
fields["status"] = accessLog.Status
|
}
|
||||||
fields["createdAt"] = accessLog.Timestamp
|
fields["content"] = content
|
||||||
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
|
|
||||||
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
|
||||||
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
|
||||||
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
|
||||||
fields["firewallRuleId"] = accessLog.FirewallRuleId
|
|
||||||
|
|
||||||
// TODO 根据集群、服务设置获取IP
|
_, err = dao.Query(tx).
|
||||||
if tableDef.HasRemoteAddr {
|
Table(tableDef.Name).
|
||||||
fields["remoteAddr"] = accessLog.RemoteAddr
|
Sets(fields).
|
||||||
}
|
Insert()
|
||||||
if tableDef.HasDomain {
|
if err != nil {
|
||||||
fields["domain"] = accessLog.Host
|
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
|
||||||
}
|
if strings.Contains(err.Error(), "1146") {
|
||||||
|
tableDef, err = findHTTPAccessLogTable(dao.Instance, day, true)
|
||||||
content, err := json.Marshal(accessLog)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
fields["content"] = content
|
|
||||||
|
|
||||||
_, err = dao.Query(tx).
|
|
||||||
Table(tableDef.Name).
|
|
||||||
Sets(fields).
|
|
||||||
Insert()
|
|
||||||
if err != nil {
|
|
||||||
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
|
|
||||||
if strings.Contains(err.Error(), "1146") {
|
|
||||||
tableDef, err = findHTTPAccessLogTable(dao.Instance, day, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = dao.Query(tx).
|
|
||||||
Table(tableDef.Name).
|
|
||||||
Sets(fields).
|
|
||||||
Insert()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logs.Println("HTTP_ACCESS_LOG", err.Error())
|
|
||||||
}
|
}
|
||||||
|
_, err = dao.Query(tx).
|
||||||
|
Table(tableDef.Name).
|
||||||
|
Sets(fields).
|
||||||
|
Insert()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,6 +432,11 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
query.Param("intKeyword2", types.Int(pieces[1]))
|
query.Param("intKeyword2", types.Int(pieces[1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if regexp.MustCompile(`^\d{20,}\s*\.?$`).MatchString(keyword) {
|
||||||
|
where += " OR requestId=:requestId"
|
||||||
|
query.Param("requestId", strings.TrimRight(keyword, ". "))
|
||||||
|
}
|
||||||
|
|
||||||
query.Where("("+where+")").
|
query.Where("("+where+")").
|
||||||
Param("keyword", "%"+keyword+"%")
|
Param("keyword", "%"+keyword+"%")
|
||||||
if useOriginKeyword {
|
if useOriginKeyword {
|
||||||
@@ -407,7 +512,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
|
|
||||||
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
||||||
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
|
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
|
||||||
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
|
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
|
||||||
return nil, errors.New("invalid requestId")
|
return nil, errors.New("invalid requestId")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,3 +566,42 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetupQueue 建立队列
|
||||||
|
func (this *HTTPAccessLogDAO) SetupQueue() {
|
||||||
|
configJSON, err := SharedSysSettingDAO.ReadSetting(nil, systemconfigs.SettingCodeAccessLogQueue)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "read settings failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(configJSON) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.Compare(accessLogConfigJSON, configJSON) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
accessLogConfigJSON = configJSON
|
||||||
|
|
||||||
|
var config = &serverconfigs.AccessLogQueueConfig{}
|
||||||
|
err = json.Unmarshal(configJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "decode settings failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accessLogQueuePercent = config.Percent
|
||||||
|
accessLogCountPerSecond = config.CountPerSecond
|
||||||
|
if config.MaxLength <= 0 {
|
||||||
|
config.MaxLength = 100_000
|
||||||
|
}
|
||||||
|
|
||||||
|
if accessLogQueueMaxLength != config.MaxLength {
|
||||||
|
accessLogQueueMaxLength = config.MaxLength
|
||||||
|
oldAccessLogQueue = accessLogQueue
|
||||||
|
accessLogQueue = make(chan *pb.HTTPAccessLog, config.MaxLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
remotelogs.Println("HTTP_ACCESS_LOG_QUEUE", "change queue max length: "+types.String(config.MaxLength)+", percent: "+types.String(config.Percent)+", countPerSecond: "+types.String(config.CountPerSecond))
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateHTTPAccessLogs(t *testing.T) {
|
func TestCreateHTTPAccessLog(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
|
|
||||||
err := NewDBNodeInitializer().loop()
|
err := NewDBNodeInitializer().loop()
|
||||||
@@ -26,9 +28,19 @@ func TestCreateHTTPAccessLogs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
dao := randomHTTPAccessLogDAO()
|
dao := randomHTTPAccessLogDAO()
|
||||||
t.Log("dao:", dao)
|
t.Log("dao:", dao)
|
||||||
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLogsWithDAO(tx, dao, []*pb.HTTPAccessLog{accessLog})
|
|
||||||
if err != nil {
|
// 先初始化
|
||||||
t.Fatal(err)
|
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
|
||||||
|
var before = time.Now()
|
||||||
|
defer func() {
|
||||||
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}()
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ func (this *HTTPAccessLog) ToPB() (*pb.HTTPAccessLog, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
p.RequestId = this.RequestId
|
p.RequestId = this.RequestId
|
||||||
|
p.RequestBody = []byte(this.RequestBody)
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,12 +96,12 @@ func (this *HTTPAuthPolicyDAO) UpdateHTTPAuthPolicy(tx *dbs.Tx, policyId int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposePolicyConfig 组合配置
|
// ComposePolicyConfig 组合配置
|
||||||
func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPAuthPolicy, error) {
|
func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPAuthPolicy, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.HTTPAuthPolicy), nil
|
return cache.(*serverconfigs.HTTPAuthPolicy), nil
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,9 @@ func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, c
|
|||||||
}
|
}
|
||||||
config.Params = params
|
config.Params = params
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -225,12 +225,12 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeCachePolicy 组合配置
|
// ComposeCachePolicy 组合配置
|
||||||
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPCachePolicy, error) {
|
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPCachePolicy, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.HTTPCachePolicy), nil
|
return cache.(*serverconfigs.HTTPCachePolicy), nil
|
||||||
}
|
}
|
||||||
@@ -292,7 +292,9 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
|
|||||||
config.CacheRefs = refs
|
config.CacheRefs = refs
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -62,6 +63,11 @@ func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(tx *dbs.Tx, policyI
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = this.NotifyDisable(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return this.NotifyUpdate(tx, policyId)
|
return this.NotifyUpdate(tx, policyId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +91,19 @@ func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(tx *dbs.Tx, id int
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledHTTPFirewallPolicyBasic 获取WAF策略基本信息
|
||||||
|
func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicyBasic(tx *dbs.Tx, policyId int64) (*HTTPFirewallPolicy, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(policyId).
|
||||||
|
Result("id", "name", "serverId", "isOn").
|
||||||
|
Attr("state", HTTPFirewallPolicyStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*HTTPFirewallPolicy), err
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllEnabledFirewallPolicies 查找所有可用策略
|
// FindAllEnabledFirewallPolicies 查找所有可用策略
|
||||||
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (result []*HTTPFirewallPolicy, err error) {
|
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (result []*HTTPFirewallPolicy, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
@@ -297,12 +316,12 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, keywo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeFirewallPolicy 组合策略配置
|
// ComposeFirewallPolicy 组合策略配置
|
||||||
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*firewallconfigs.HTTPFirewallPolicy, error) {
|
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*firewallconfigs.HTTPFirewallPolicy, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
var cacheKey = this.Table + ":config:" + types.String(policyId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*firewallconfigs.HTTPFirewallPolicy), nil
|
return cache.(*firewallconfigs.HTTPFirewallPolicy), nil
|
||||||
}
|
}
|
||||||
@@ -392,7 +411,9 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
|
|||||||
config.BlockOptions = blockAction
|
config.BlockOptions = blockAction
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
@@ -447,6 +468,19 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledFirewallPolicyWithIPListId 查找使用某个IPList的策略
|
||||||
|
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyWithIPListId(tx *dbs.Tx, ipListId int64) (*HTTPFirewallPolicy, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
State(HTTPFirewallPolicyStateEnabled).
|
||||||
|
Where("(JSON_CONTAINS(inbound, :listQuery, '$.whiteListRef') OR JSON_CONTAINS(inbound, :listQuery, '$.blackListRef'))").
|
||||||
|
Param("listQuery", maps.Map{"isOn": true, "listId": ipListId}.AsJSON()).
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return one.(*HTTPFirewallPolicy), err
|
||||||
|
}
|
||||||
|
|
||||||
// FindEnabledFirewallPolicyIdWithRuleGroupId 查找包含某个规则分组的策略ID
|
// FindEnabledFirewallPolicyIdWithRuleGroupId 查找包含某个规则分组的策略ID
|
||||||
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdWithRuleGroupId(tx *dbs.Tx, ruleGroupId int64) (int64, error) {
|
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdWithRuleGroupId(tx *dbs.Tx, ruleGroupId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -466,6 +500,23 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyServerId(tx *dbs.Tx, poli
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindFirewallPolicyIdsWithServerId 查找服务独立关联的策略IDs
|
||||||
|
func (this *HTTPFirewallPolicyDAO) FindFirewallPolicyIdsWithServerId(tx *dbs.Tx, serverId int64) ([]int64, error) {
|
||||||
|
var result = []int64{}
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
State(HTTPFirewallPolicyStateEnabled).
|
||||||
|
Result("id").
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*HTTPFirewallPolicy).Id))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||||
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithHTTPFirewallPolicyId(tx, policyId)
|
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithHTTPFirewallPolicyId(tx, policyId)
|
||||||
@@ -492,3 +543,65 @@ func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) erro
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyDisable 通知禁用
|
||||||
|
func (this *HTTPFirewallPolicyDAO) NotifyDisable(tx *dbs.Tx, policyId int64) error {
|
||||||
|
if policyId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 禁用IP名单
|
||||||
|
inboundString, err := this.Query(tx).
|
||||||
|
Pk(policyId).
|
||||||
|
Result("inbound").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(inboundString) > 0 {
|
||||||
|
var inboundConfig = &firewallconfigs.HTTPFirewallInboundConfig{}
|
||||||
|
err = json.Unmarshal([]byte(inboundString), inboundConfig)
|
||||||
|
if err != nil {
|
||||||
|
// 不处理错误
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if inboundConfig.AllowListRef != nil && inboundConfig.AllowListRef.ListId > 0 {
|
||||||
|
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.AllowListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.AllowListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if inboundConfig.DenyListRef != nil && inboundConfig.DenyListRef.ListId > 0 {
|
||||||
|
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.DenyListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.DenyListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if inboundConfig.GreyListRef != nil && inboundConfig.GreyListRef.ListId > 0 {
|
||||||
|
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.GreyListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.GreyListRef.ListId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
|
|||||||
config.Name = group.Name
|
config.Name = group.Name
|
||||||
config.Description = group.Description
|
config.Description = group.Description
|
||||||
config.Code = group.Code
|
config.Code = group.Code
|
||||||
|
config.IsTemplate = group.IsTemplate == 1
|
||||||
|
|
||||||
if IsNotNull(group.Sets) {
|
if IsNotNull(group.Sets) {
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -125,6 +126,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroupFromConfig(tx *dbs.Tx, groupCon
|
|||||||
op.Description = groupConfig.Description
|
op.Description = groupConfig.Description
|
||||||
op.State = HTTPFirewallRuleGroupStateEnabled
|
op.State = HTTPFirewallRuleGroupStateEnabled
|
||||||
op.Code = groupConfig.Code
|
op.Code = groupConfig.Code
|
||||||
|
op.IsTemplate = groupConfig.IsTemplate
|
||||||
|
|
||||||
// sets
|
// sets
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -163,11 +165,12 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroupIsOn(tx *dbs.Tx, groupId int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateGroup 创建分组
|
// CreateGroup 创建分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, description string) (int64, error) {
|
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, code string, description string) (int64, error) {
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
op := NewHTTPFirewallRuleGroupOperator()
|
||||||
op.State = HTTPFirewallRuleStateEnabled
|
op.State = HTTPFirewallRuleStateEnabled
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
op.Code = code
|
||||||
op.Description = description
|
op.Description = description
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -177,7 +180,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGroup 修改分组
|
// UpdateGroup 修改分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, description string) error {
|
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, code string, description string) error {
|
||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
@@ -185,6 +188,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
|
|||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
op.Code = code
|
||||||
op.Description = description
|
op.Description = description
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -194,13 +198,13 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGroupSets 修改分组中的规则集
|
// UpdateGroupSets 修改分组中的规则集
|
||||||
func (this *HTTPFirewallRuleGroupDAO) UpdateGroupSets(tx *dbs.Tx, groupId int64, setsJSON []byte) error {
|
func (this *HTTPFirewallRuleGroupDAO) UpdateGroupSets(tx *dbs.Tx, groupId int64, setRefsJSON []byte) error {
|
||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
op := NewHTTPFirewallRuleGroupOperator()
|
||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.Sets = setsJSON
|
op.Sets = setRefsJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 防火墙规则分组
|
// HTTPFirewallRuleGroup 防火墙规则分组
|
||||||
type HTTPFirewallRuleGroup struct {
|
type HTTPFirewallRuleGroup struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
Code string `field:"code"` // 代号
|
Code string `field:"code"` // 代号
|
||||||
|
IsTemplate uint8 `field:"isTemplate"` // 是否为预置模板
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
@@ -20,6 +21,7 @@ type HTTPFirewallRuleGroupOperator struct {
|
|||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
Code interface{} // 代号
|
Code interface{} // 代号
|
||||||
|
IsTemplate interface{} // 是否为预置模板
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ func (this *HTTPFirewallRuleSetDAO) ComposeFirewallRuleSet(tx *dbs.Tx, setId int
|
|||||||
config.Description = set.Description
|
config.Description = set.Description
|
||||||
config.Code = set.Code
|
config.Code = set.Code
|
||||||
config.Connector = set.Connector
|
config.Connector = set.Connector
|
||||||
|
config.IgnoreLocal = set.IgnoreLocal == 1
|
||||||
|
|
||||||
if IsNotNull(set.Rules) {
|
if IsNotNull(set.Rules) {
|
||||||
ruleRefs := []*firewallconfigs.HTTPFirewallRuleRef{}
|
ruleRefs := []*firewallconfigs.HTTPFirewallRuleRef{}
|
||||||
@@ -139,6 +140,7 @@ func (this *HTTPFirewallRuleSetDAO) CreateOrUpdateSetFromConfig(tx *dbs.Tx, setC
|
|||||||
op.Name = setConfig.Name
|
op.Name = setConfig.Name
|
||||||
op.Description = setConfig.Description
|
op.Description = setConfig.Description
|
||||||
op.Connector = setConfig.Connector
|
op.Connector = setConfig.Connector
|
||||||
|
op.IgnoreLocal = setConfig.IgnoreLocal
|
||||||
|
|
||||||
if len(setConfig.Actions) == 0 {
|
if len(setConfig.Actions) == 0 {
|
||||||
op.Actions = "[]"
|
op.Actions = "[]"
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type HTTPFirewallRuleSet struct {
|
|||||||
Action string `field:"action"` // 执行的动作(过期)
|
Action string `field:"action"` // 执行的动作(过期)
|
||||||
ActionOptions string `field:"actionOptions"` // 动作的选项(过期)
|
ActionOptions string `field:"actionOptions"` // 动作的选项(过期)
|
||||||
Actions string `field:"actions"` // 一组动作
|
Actions string `field:"actions"` // 一组动作
|
||||||
|
IgnoreLocal uint8 `field:"ignoreLocal"` // 忽略局域网请求
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPFirewallRuleSetOperator struct {
|
type HTTPFirewallRuleSetOperator struct {
|
||||||
@@ -33,6 +34,7 @@ type HTTPFirewallRuleSetOperator struct {
|
|||||||
Action interface{} // 执行的动作(过期)
|
Action interface{} // 执行的动作(过期)
|
||||||
ActionOptions interface{} // 动作的选项(过期)
|
ActionOptions interface{} // 动作的选项(过期)
|
||||||
Actions interface{} // 一组动作
|
Actions interface{} // 一组动作
|
||||||
|
IgnoreLocal interface{} // 忽略局域网请求
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPFirewallRuleSetOperator() *HTTPFirewallRuleSetOperator {
|
func NewHTTPFirewallRuleSetOperator() *HTTPFirewallRuleSetOperator {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -80,22 +80,69 @@ func (this *HTTPHeaderDAO) FindHTTPHeaderName(tx *dbs.Tx, id int64) (string, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateHeader 创建Header
|
// CreateHeader 创建Header
|
||||||
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (int64, error) {
|
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, userId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) (int64, error) {
|
||||||
op := NewHTTPHeaderOperator()
|
op := NewHTTPHeaderOperator()
|
||||||
|
op.UserId = userId
|
||||||
op.State = HTTPHeaderStateEnabled
|
op.State = HTTPHeaderStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Value = value
|
op.Value = value
|
||||||
|
|
||||||
statusConfig := &shared.HTTPStatusConfig{
|
// status
|
||||||
Always: true,
|
var statusConfig *shared.HTTPStatusConfig
|
||||||
|
if len(status) == 0 {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: false,
|
||||||
|
Codes: status,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
statusJSON, err := json.Marshal(statusConfig)
|
statusJSON, err := json.Marshal(statusConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
op.Status = statusJSON
|
op.Status = statusJSON
|
||||||
|
|
||||||
|
op.DisableRedirect = disableRedirect
|
||||||
|
op.ShouldAppend = shouldAppend
|
||||||
|
op.ShouldReplace = shouldReplace
|
||||||
|
|
||||||
|
if len(replaceValues) == 0 {
|
||||||
|
op.ReplaceValues = "[]"
|
||||||
|
} else {
|
||||||
|
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.ReplaceValues = replaceValuesJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(methods) == 0 {
|
||||||
|
op.Methods = "[]"
|
||||||
|
} else {
|
||||||
|
methodsJSON, err := json.Marshal(methods)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Methods = methodsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(domains) == 0 {
|
||||||
|
op.Domains = "[]"
|
||||||
|
} else {
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
}
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -104,7 +151,7 @@ func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateHeader 修改Header
|
// UpdateHeader 修改Header
|
||||||
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string) error {
|
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) error {
|
||||||
if headerId <= 0 {
|
if headerId <= 0 {
|
||||||
return errors.New("invalid headerId")
|
return errors.New("invalid headerId")
|
||||||
}
|
}
|
||||||
@@ -113,7 +160,63 @@ func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string,
|
|||||||
op.Id = headerId
|
op.Id = headerId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Value = value
|
op.Value = value
|
||||||
err := this.Save(tx, op)
|
|
||||||
|
// status
|
||||||
|
var statusConfig *shared.HTTPStatusConfig
|
||||||
|
if len(status) == 0 {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: false,
|
||||||
|
Codes: status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
statusJSON, err := json.Marshal(statusConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Status = statusJSON
|
||||||
|
|
||||||
|
op.DisableRedirect = disableRedirect
|
||||||
|
op.ShouldAppend = shouldAppend
|
||||||
|
op.ShouldReplace = shouldReplace
|
||||||
|
|
||||||
|
if len(replaceValues) == 0 {
|
||||||
|
op.ReplaceValues = "[]"
|
||||||
|
} else {
|
||||||
|
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.ReplaceValues = replaceValuesJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(methods) == 0 {
|
||||||
|
op.Methods = "[]"
|
||||||
|
} else {
|
||||||
|
methodsJSON, err := json.Marshal(methods)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Methods = methodsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(domains) == 0 {
|
||||||
|
op.Domains = "[]"
|
||||||
|
} else {
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -136,7 +239,21 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
|||||||
config.IsOn = header.IsOn == 1
|
config.IsOn = header.IsOn == 1
|
||||||
config.Name = header.Name
|
config.Name = header.Name
|
||||||
config.Value = header.Value
|
config.Value = header.Value
|
||||||
|
config.DisableRedirect = header.DisableRedirect == 1
|
||||||
|
config.ShouldAppend = header.ShouldAppend == 1
|
||||||
|
|
||||||
|
// replace
|
||||||
|
config.ShouldReplace = header.ShouldReplace == 1
|
||||||
|
if len(header.ReplaceValues) > 0 {
|
||||||
|
var values = []*shared.HTTPHeaderReplaceValue{}
|
||||||
|
err = json.Unmarshal([]byte(header.ReplaceValues), &values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.ReplaceValues = values
|
||||||
|
}
|
||||||
|
|
||||||
|
// status
|
||||||
if len(header.Status) > 0 {
|
if len(header.Status) > 0 {
|
||||||
status := &shared.HTTPStatusConfig{}
|
status := &shared.HTTPStatusConfig{}
|
||||||
err = json.Unmarshal([]byte(header.Status), status)
|
err = json.Unmarshal([]byte(header.Status), status)
|
||||||
@@ -146,6 +263,26 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
|||||||
config.Status = status
|
config.Status = status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(header.Methods) > 0 {
|
||||||
|
var methods = []string{}
|
||||||
|
err = json.Unmarshal([]byte(header.Methods), &methods)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Methods = methods
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(header.Domains) > 0 {
|
||||||
|
var domains = []string{}
|
||||||
|
err = json.Unmarshal([]byte(header.Domains), &domains)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Domains = domains
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,44 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// HTTP Header
|
// HTTPHeader HTTP Header
|
||||||
type HTTPHeader struct {
|
type HTTPHeader struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
TemplateId uint32 `field:"templateId"` // 模版ID
|
TemplateId uint32 `field:"templateId"` // 模版ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Value string `field:"value"` // 值
|
Value string `field:"value"` // 值
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
Status string `field:"status"` // 状态码设置
|
Status string `field:"status"` // 状态码设置
|
||||||
State uint8 `field:"state"` // 状态
|
DisableRedirect uint8 `field:"disableRedirect"` // 是否不支持跳转
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
ShouldAppend uint8 `field:"shouldAppend"` // 是否为附加
|
||||||
|
ShouldReplace uint8 `field:"shouldReplace"` // 是否替换变量
|
||||||
|
ReplaceValues string `field:"replaceValues"` // 替换的值
|
||||||
|
Methods string `field:"methods"` // 支持的方法
|
||||||
|
Domains string `field:"domains"` // 支持的域名
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPHeaderOperator struct {
|
type HTTPHeaderOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
TemplateId interface{} // 模版ID
|
TemplateId interface{} // 模版ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Value interface{} // 值
|
Value interface{} // 值
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
Status interface{} // 状态码设置
|
Status interface{} // 状态码设置
|
||||||
State interface{} // 状态
|
DisableRedirect interface{} // 是否不支持跳转
|
||||||
CreatedAt interface{} // 创建时间
|
ShouldAppend interface{} // 是否为附加
|
||||||
|
ShouldReplace interface{} // 是否替换变量
|
||||||
|
ReplaceValues interface{} // 替换的值
|
||||||
|
Methods interface{} // 支持的方法
|
||||||
|
Domains interface{} // 支持的域名
|
||||||
|
State interface{} // 状态
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPHeaderOperator() *HTTPHeaderOperator {
|
func NewHTTPHeaderOperator() *HTTPHeaderOperator {
|
||||||
|
|||||||
@@ -186,48 +186,6 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
|||||||
config.Id = int64(policy.Id)
|
config.Id = int64(policy.Id)
|
||||||
config.IsOn = policy.IsOn == 1
|
config.IsOn = policy.IsOn == 1
|
||||||
|
|
||||||
// AddHeaders
|
|
||||||
if len(policy.AddHeaders) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.AddHeaders), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
config.AddHeaders = append(config.AddHeaders, headerConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTrailers
|
|
||||||
if len(policy.AddTrailers) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.AddTrailers), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
resultRefs := []*shared.HTTPHeaderRef{}
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if headerConfig == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resultRefs = append(resultRefs, ref)
|
|
||||||
config.AddTrailers = append(config.AddTrailers, headerConfig)
|
|
||||||
}
|
|
||||||
config.AddHeaderRefs = resultRefs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetHeaders
|
// SetHeaders
|
||||||
if len(policy.SetHeaders) > 0 {
|
if len(policy.SetHeaders) > 0 {
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
refs := []*shared.HTTPHeaderRef{}
|
||||||
@@ -252,30 +210,6 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceHeaders
|
|
||||||
if len(policy.ReplaceHeaders) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.ReplaceHeaders), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
resultRefs := []*shared.HTTPHeaderRef{}
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if headerConfig == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resultRefs = append(resultRefs, ref)
|
|
||||||
config.ReplaceHeaders = append(config.ReplaceHeaders, headerConfig)
|
|
||||||
}
|
|
||||||
config.ReplaceHeaderRefs = resultRefs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete Headers
|
// Delete Headers
|
||||||
if len(policy.DeleteHeaders) > 0 {
|
if len(policy.DeleteHeaders) > 0 {
|
||||||
headers := []string{}
|
headers := []string{}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -85,7 +86,7 @@ func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLocation 创建路由规则
|
// CreateLocation 创建路由规则
|
||||||
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte) (int64, error) {
|
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte, domains []string) (int64, error) {
|
||||||
op := NewHTTPLocationOperator()
|
op := NewHTTPLocationOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPLocationStateEnabled
|
op.State = HTTPLocationStateEnabled
|
||||||
@@ -99,7 +100,16 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
|
|||||||
op.Conds = condsJSON
|
op.Conds = condsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
if domains == nil {
|
||||||
|
domains = []string{}
|
||||||
|
}
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -107,7 +117,7 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateLocation 修改路由规则
|
// UpdateLocation 修改路由规则
|
||||||
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte) error {
|
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte, domains []string) error {
|
||||||
if locationId <= 0 {
|
if locationId <= 0 {
|
||||||
return errors.New("invalid locationId")
|
return errors.New("invalid locationId")
|
||||||
}
|
}
|
||||||
@@ -123,7 +133,16 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
|
|||||||
op.Conds = condsJSON
|
op.Conds = condsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
if domains == nil {
|
||||||
|
domains = []string{}
|
||||||
|
}
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -131,12 +150,12 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeLocationConfig 组合配置
|
// ComposeLocationConfig 组合配置
|
||||||
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap maps.Map) (*serverconfigs.HTTPLocationConfig, error) {
|
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPLocationConfig, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(locationId)
|
var cacheKey = this.Table + ":config:" + types.String(locationId)
|
||||||
var cacheConfig = cacheMap.Get(cacheKey)
|
var cacheConfig, _ = cacheMap.Get(cacheKey)
|
||||||
if cacheConfig != nil {
|
if cacheConfig != nil {
|
||||||
return cacheConfig.(*serverconfigs.HTTPLocationConfig), nil
|
return cacheConfig.(*serverconfigs.HTTPLocationConfig), nil
|
||||||
}
|
}
|
||||||
@@ -194,7 +213,21 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64,
|
|||||||
config.Conds = conds
|
config.Conds = conds
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
// domains
|
||||||
|
if len(location.Domains) > 0 {
|
||||||
|
var domains = []string{}
|
||||||
|
err = json.Unmarshal([]byte(location.Domains), &domains)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(domains) > 0 {
|
||||||
|
config.Domains = domains
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
@@ -259,7 +292,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConvertLocationRefs 转换引用为配置
|
// ConvertLocationRefs 转换引用为配置
|
||||||
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap maps.Map) (locations []*serverconfigs.HTTPLocationConfig, err error) {
|
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap *utils.CacheMap) (locations []*serverconfigs.HTTPLocationConfig, err error) {
|
||||||
for _, ref := range refs {
|
for _, ref := range refs {
|
||||||
config, err := this.ComposeLocationConfig(tx, ref.LocationId, cacheMap)
|
config, err := this.ComposeLocationConfig(tx, ref.LocationId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type HTTPLocation struct {
|
|||||||
UrlPrefix string `field:"urlPrefix"` // URL前缀
|
UrlPrefix string `field:"urlPrefix"` // URL前缀
|
||||||
IsBreak uint8 `field:"isBreak"` // 是否终止匹配
|
IsBreak uint8 `field:"isBreak"` // 是否终止匹配
|
||||||
Conds string `field:"conds"` // 匹配条件
|
Conds string `field:"conds"` // 匹配条件
|
||||||
|
Domains string `field:"domains"` // 专属域名
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPLocationOperator struct {
|
type HTTPLocationOperator struct {
|
||||||
@@ -37,6 +38,7 @@ type HTTPLocationOperator struct {
|
|||||||
UrlPrefix interface{} // URL前缀
|
UrlPrefix interface{} // URL前缀
|
||||||
IsBreak interface{} // 是否终止匹配
|
IsBreak interface{} // 是否终止匹配
|
||||||
Conds interface{} // 匹配条件
|
Conds interface{} // 匹配条件
|
||||||
|
Domains interface{} // 专属域名
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPLocationOperator() *HTTPLocationOperator {
|
func NewHTTPLocationOperator() *HTTPLocationOperator {
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -133,12 +133,12 @@ func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposePageConfig 组合配置
|
// ComposePageConfig 组合配置
|
||||||
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap maps.Map) (*serverconfigs.HTTPPageConfig, error) {
|
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPPageConfig, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(pageId)
|
var cacheKey = this.Table + ":config:" + types.String(pageId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.HTTPPageConfig), nil
|
return cache.(*serverconfigs.HTTPPageConfig), nil
|
||||||
}
|
}
|
||||||
@@ -175,7 +175,9 @@ func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -77,12 +77,12 @@ func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeRewriteRule 构造配置
|
// ComposeRewriteRule 构造配置
|
||||||
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap maps.Map) (*serverconfigs.HTTPRewriteRule, error) {
|
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPRewriteRule, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(rewriteRuleId)
|
var cacheKey = this.Table + ":config:" + types.String(rewriteRuleId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.HTTPRewriteRule), nil
|
return cache.(*serverconfigs.HTTPRewriteRule), nil
|
||||||
}
|
}
|
||||||
@@ -116,7 +116,9 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
|
|||||||
config.Conds = conds
|
config.Conds = conds
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
@@ -75,12 +76,12 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeWebConfig 组合配置
|
// ComposeWebConfig 组合配置
|
||||||
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.Map) (*serverconfigs.HTTPWebConfig, error) {
|
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPWebConfig, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(webId)
|
var cacheKey = this.Table + ":config:" + types.String(webId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.HTTPWebConfig), nil
|
return cache.(*serverconfigs.HTTPWebConfig), nil
|
||||||
}
|
}
|
||||||
@@ -418,7 +419,24 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.
|
|||||||
config.RemoteAddr = remoteAddrConfig
|
config.RemoteAddr = remoteAddrConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
// mergeSlashes
|
||||||
|
config.MergeSlashes = web.MergeSlashes == 1
|
||||||
|
|
||||||
|
// 请求限制
|
||||||
|
if len(web.RequestLimit) > 0 {
|
||||||
|
var requestLimitConfig = &serverconfigs.HTTPRequestLimitConfig{}
|
||||||
|
if len(web.RequestLimit) > 0 {
|
||||||
|
err = json.Unmarshal([]byte(web.RequestLimit), requestLimitConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.RequestLimit = requestLimitConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
@@ -1034,6 +1052,8 @@ func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedi
|
|||||||
return this.NotifyUpdate(tx, webId)
|
return this.NotifyUpdate(tx, webId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 通用设置
|
||||||
|
|
||||||
// FindWebHostRedirects 查找主机跳转
|
// FindWebHostRedirects 查找主机跳转
|
||||||
func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) {
|
func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||||
col, err := this.Query(tx).
|
col, err := this.Query(tx).
|
||||||
@@ -1046,6 +1066,59 @@ func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, e
|
|||||||
return []byte(col), nil
|
return []byte(col), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateWebCommon 修改通用设置
|
||||||
|
func (this *HTTPWebDAO) UpdateWebCommon(tx *dbs.Tx, webId int64, mergeSlashes bool) error {
|
||||||
|
if webId <= 0 {
|
||||||
|
return errors.New("invalid webId")
|
||||||
|
}
|
||||||
|
var op = NewHTTPWebOperator()
|
||||||
|
op.Id = webId
|
||||||
|
op.MergeSlashes = mergeSlashes
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateWebRequestLimit 修改服务的请求限制
|
||||||
|
func (this *HTTPWebDAO) UpdateWebRequestLimit(tx *dbs.Tx, webId int64, config *serverconfigs.HTTPRequestLimitConfig) error {
|
||||||
|
configJSON, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Set("requestLimit", configJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindWebRequestLimit 获取服务的请求限制
|
||||||
|
func (this *HTTPWebDAO) FindWebRequestLimit(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPRequestLimitConfig, error) {
|
||||||
|
configString, err := this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Result("requestLimit").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var config = &serverconfigs.HTTPRequestLimitConfig{}
|
||||||
|
if len(configString) == 0 {
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
err = json.Unmarshal([]byte(configString), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||||
// server
|
// server
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ type HTTPWeb struct {
|
|||||||
Auth string `field:"auth"` // 认证策略配置
|
Auth string `field:"auth"` // 认证策略配置
|
||||||
Webp string `field:"webp"` // WebP配置
|
Webp string `field:"webp"` // WebP配置
|
||||||
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
|
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
|
||||||
|
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
|
||||||
|
RequestLimit string `field:"requestLimit"` // 请求限制
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPWebOperator struct {
|
type HTTPWebOperator struct {
|
||||||
@@ -65,6 +67,8 @@ type HTTPWebOperator struct {
|
|||||||
Auth interface{} // 认证策略配置
|
Auth interface{} // 认证策略配置
|
||||||
Webp interface{} // WebP配置
|
Webp interface{} // WebP配置
|
||||||
RemoteAddr interface{} // 客户端IP配置
|
RemoteAddr interface{} // 客户端IP配置
|
||||||
|
MergeSlashes interface{} // 是否合并路径中的斜杠
|
||||||
|
RequestLimit interface{} // 请求限制
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -75,6 +76,41 @@ func (this *IPItemDAO) DisableIPItem(tx *dbs.Tx, id int64) error {
|
|||||||
return this.NotifyUpdate(tx, id)
|
return this.NotifyUpdate(tx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DisableIPItemsWithListId 禁用某个IP名单内的所有IP
|
||||||
|
func (this *IPItemDAO) DisableIPItemsWithListId(tx *dbs.Tx, listId int64) error {
|
||||||
|
for {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
Attr("listId", listId).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Limit(1000).
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(ones) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var itemId = one.(*IPItem).Id
|
||||||
|
version, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(itemId).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Set("version", version).
|
||||||
|
Set("state", IPItemStateDisabled).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindEnabledIPItem 查找启用中的条目
|
// FindEnabledIPItem 查找启用中的条目
|
||||||
func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error) {
|
func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
@@ -87,18 +123,34 @@ func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error)
|
|||||||
return result.(*IPItem), err
|
return result.(*IPItem), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableOldIPItem 根据IP删除以前的旧记录
|
// DeleteOldItem 根据IP删除以前的旧记录
|
||||||
func (this *IPItemDAO) DisableOldIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
func (this *IPItemDAO) DeleteOldItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
||||||
return this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
|
UseIndex("ipFrom").
|
||||||
Attr("listId", listId).
|
Attr("listId", listId).
|
||||||
Attr("ipFrom", ipFrom).
|
Attr("ipFrom", ipFrom).
|
||||||
Attr("ipTo", ipTo).
|
Attr("ipTo", ipTo).
|
||||||
Set("state", IPItemStateDisabled).
|
Delete()
|
||||||
UpdateQuickly()
|
// 这里不通知更新
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateIPItem 创建IP
|
// CreateIPItem 创建IP
|
||||||
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType, eventLevel string) (int64, error) {
|
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
|
||||||
|
listId int64,
|
||||||
|
ipFrom string,
|
||||||
|
ipTo string,
|
||||||
|
expiredAt int64,
|
||||||
|
reason string,
|
||||||
|
itemType IPItemType,
|
||||||
|
eventLevel string,
|
||||||
|
nodeId int64,
|
||||||
|
serverId int64,
|
||||||
|
sourceNodeId int64,
|
||||||
|
sourceServerId int64,
|
||||||
|
sourceHTTPFirewallPolicyId int64,
|
||||||
|
sourceHTTPFirewallRuleGroupId int64,
|
||||||
|
sourceHTTPFirewallRuleSetId int64) (int64, error) {
|
||||||
version, err := SharedIPListDAO.IncreaseVersion(tx)
|
version, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -118,6 +170,15 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipT
|
|||||||
expiredAt = 0
|
expiredAt = 0
|
||||||
}
|
}
|
||||||
op.ExpiredAt = expiredAt
|
op.ExpiredAt = expiredAt
|
||||||
|
|
||||||
|
op.NodeId = nodeId
|
||||||
|
op.ServerId = serverId
|
||||||
|
op.SourceNodeId = sourceNodeId
|
||||||
|
op.SourceServerId = sourceServerId
|
||||||
|
op.SourceHTTPFirewallPolicyId = sourceHTTPFirewallPolicyId
|
||||||
|
op.SourceHTTPFirewallRuleGroupId = sourceHTTPFirewallRuleGroupId
|
||||||
|
op.SourceHTTPFirewallRuleSetId = sourceHTTPFirewallRuleSetId
|
||||||
|
|
||||||
op.State = IPItemStateEnabled
|
op.State = IPItemStateEnabled
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -125,6 +186,11 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipT
|
|||||||
}
|
}
|
||||||
itemId := types.Int64(op.Id)
|
itemId := types.Int64(op.Id)
|
||||||
|
|
||||||
|
// 全局名单不需要即时更新,防止数量过多而导致性能问题
|
||||||
|
if listId == firewallconfigs.GlobalListId {
|
||||||
|
return itemId, nil
|
||||||
|
}
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx, itemId)
|
err = this.NotifyUpdate(tx, itemId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -177,18 +243,39 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountIPItemsWithListId 计算IP数量
|
// CountIPItemsWithListId 计算IP数量
|
||||||
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64) (int64, error) {
|
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, keyword string) (int64, error) {
|
||||||
return this.Query(tx).
|
var query = this.Query(tx).
|
||||||
State(IPItemStateEnabled).
|
State(IPItemStateEnabled).
|
||||||
Attr("listId", listId).
|
Attr("listId", listId)
|
||||||
Count()
|
if len(keyword) > 0 {
|
||||||
|
query.Where("(ipFrom LIKE :keyword OR ipTo LIKE :keyword)").
|
||||||
|
Param("keyword", "%"+keyword+"%")
|
||||||
|
}
|
||||||
|
if len(ipFrom) > 0 {
|
||||||
|
query.Attr("ipFrom", ipFrom)
|
||||||
|
}
|
||||||
|
if len(ipTo) > 0 {
|
||||||
|
query.Attr("ipTo", ipTo)
|
||||||
|
}
|
||||||
|
return query.Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListIPItemsWithListId 查找IP列表
|
// ListIPItemsWithListId 查找IP列表
|
||||||
func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, offset int64, size int64) (result []*IPItem, err error) {
|
func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, keyword string, ipFrom string, ipTo string, offset int64, size int64) (result []*IPItem, err error) {
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
State(IPItemStateEnabled).
|
State(IPItemStateEnabled).
|
||||||
Attr("listId", listId).
|
Attr("listId", listId)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Where("(ipFrom LIKE :keyword OR ipTo LIKE :keyword)").
|
||||||
|
Param("keyword", "%"+keyword+"%")
|
||||||
|
}
|
||||||
|
if len(ipFrom) > 0 {
|
||||||
|
query.Attr("ipFrom", ipFrom)
|
||||||
|
}
|
||||||
|
if len(ipTo) > 0 {
|
||||||
|
query.Attr("ipTo", ipTo)
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
DescPk().
|
DescPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
@@ -199,12 +286,40 @@ func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, offset in
|
|||||||
|
|
||||||
// ListIPItemsAfterVersion 根据版本号查找IP列表
|
// ListIPItemsAfterVersion 根据版本号查找IP列表
|
||||||
func (this *IPItemDAO) ListIPItemsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*IPItem, err error) {
|
func (this *IPItemDAO) ListIPItemsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*IPItem, err error) {
|
||||||
|
// 将过期的设置为已删除,这样是为了在 expiredAt<UNIX_TIMESTAMP()边缘节点让过期的IP有一个执行删除的机会
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
Where("(expiredAt>0 AND expiredAt<=:timestamp)").
|
||||||
|
Param("timestamp", time.Now().Unix()).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Limit(100).
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var expiredId = one.GetInt64("id")
|
||||||
|
newVersion, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Pk(expiredId).
|
||||||
|
Set("state", IPItemStateDisabled).
|
||||||
|
Set("expiredAt", 0).
|
||||||
|
Set("version", newVersion).
|
||||||
|
Update()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
// 这里不要设置状态参数,因为我们要知道哪些是删除的
|
// 这里不要设置状态参数,因为我们要知道哪些是删除的
|
||||||
Gt("version", version).
|
Gt("version", version).
|
||||||
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
|
|
||||||
Param("expiredAt", time.Now().Unix()).
|
|
||||||
Asc("version").
|
Asc("version").
|
||||||
|
Asc("id").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -243,6 +358,7 @@ func (this *IPItemDAO) FindEnabledItemContainsIP(tx *dbs.Tx, listId int64, ip ui
|
|||||||
// FindEnabledItemsWithIP 根据IP查找Item
|
// FindEnabledItemsWithIP 根据IP查找Item
|
||||||
func (this *IPItemDAO) FindEnabledItemsWithIP(tx *dbs.Tx, ip string) (result []*IPItem, err error) {
|
func (this *IPItemDAO) FindEnabledItemsWithIP(tx *dbs.Tx, ip string) (result []*IPItem, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
Attr("ipFrom", ip).
|
Attr("ipFrom", ip).
|
||||||
Attr("ipTo", "").
|
Attr("ipTo", "").
|
||||||
Where("(expiredAt=0 OR expiredAt>:nowTime)").
|
Where("(expiredAt=0 OR expiredAt>:nowTime)").
|
||||||
@@ -262,6 +378,47 @@ func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error)
|
|||||||
Exist()
|
Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountAllEnabledIPItems 计算数量
|
||||||
|
func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64) (int64, error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if len(ip) > 0 {
|
||||||
|
query.Attr("ipFrom", ip)
|
||||||
|
}
|
||||||
|
if listId > 0 {
|
||||||
|
query.Attr("listId", listId)
|
||||||
|
} else {
|
||||||
|
query.Where("(listId=" + types.String(firewallconfigs.GlobalListId) + " OR listId IN (SELECT id FROM " + SharedIPListDAO.Table + " WHERE state=1))")
|
||||||
|
}
|
||||||
|
return query.
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
|
||||||
|
Param("expiredAt", time.Now().Unix()).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAllEnabledIPItems 搜索所有IP
|
||||||
|
func (this *IPItemDAO) ListAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64, offset int64, size int64) (result []*IPItem, err error) {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if len(ip) > 0 {
|
||||||
|
query.Attr("ipFrom", ip)
|
||||||
|
}
|
||||||
|
if listId > 0 {
|
||||||
|
query.Attr("listId", listId)
|
||||||
|
} else {
|
||||||
|
query.Where("(listId=" + types.String(firewallconfigs.GlobalListId) + " OR listId IN (SELECT id FROM " + SharedIPListDAO.Table + " WHERE state=1))")
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
|
||||||
|
Param("expiredAt", time.Now().Unix()).
|
||||||
|
DescPk().
|
||||||
|
Offset(offset).
|
||||||
|
Size(size).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
||||||
// 获取ListId
|
// 获取ListId
|
||||||
@@ -274,6 +431,37 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if listId == firewallconfigs.GlobalListId {
|
||||||
|
sourceNodeId, err := this.Query(tx).
|
||||||
|
Pk(itemId).
|
||||||
|
Result("sourceNodeId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if sourceNodeId > 0 {
|
||||||
|
clusterIds, err := SharedNodeDAO.FindEnabledNodeClusterIds(tx, sourceNodeId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeIPItemChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeIPItemChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
httpFirewallPolicyIds, err := SharedHTTPFirewallPolicyDAO.FindEnabledFirewallPolicyIdsWithIPListId(tx, listId)
|
httpFirewallPolicyIds, err := SharedHTTPFirewallPolicyDAO.FindEnabledFirewallPolicyIdsWithIPListId(tx, listId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -16,3 +16,14 @@ func TestIPItemDAO_NotifyClustersUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIPItemDAO_DisableIPItemsWithListId(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := SharedIPItemDAO.DisableIPItemsWithListId(tx, 67)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,38 +1,52 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// IP
|
// IPItem IP
|
||||||
type IPItem struct {
|
type IPItem struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
ListId uint32 `field:"listId"` // 所属名单ID
|
ListId uint32 `field:"listId"` // 所属名单ID
|
||||||
Type string `field:"type"` // 类型
|
Type string `field:"type"` // 类型
|
||||||
IpFrom string `field:"ipFrom"` // 开始IP
|
IpFrom string `field:"ipFrom"` // 开始IP
|
||||||
IpTo string `field:"ipTo"` // 结束IP
|
IpTo string `field:"ipTo"` // 结束IP
|
||||||
IpFromLong uint64 `field:"ipFromLong"` // 开始IP整型
|
IpFromLong uint64 `field:"ipFromLong"` // 开始IP整型
|
||||||
IpToLong uint64 `field:"ipToLong"` // 结束IP整型
|
IpToLong uint64 `field:"ipToLong"` // 结束IP整型
|
||||||
Version uint64 `field:"version"` // 版本
|
Version uint64 `field:"version"` // 版本
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
||||||
Reason string `field:"reason"` // 加入说明
|
Reason string `field:"reason"` // 加入说明
|
||||||
EventLevel string `field:"eventLevel"` // 事件级别
|
EventLevel string `field:"eventLevel"` // 事件级别
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
|
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
|
||||||
|
ServerId uint32 `field:"serverId"` // 有效范围服务ID
|
||||||
|
NodeId uint32 `field:"nodeId"` // 有效范围节点ID
|
||||||
|
SourceNodeId uint32 `field:"sourceNodeId"` // 来源节点ID
|
||||||
|
SourceServerId uint32 `field:"sourceServerId"` // 来源服务ID
|
||||||
|
SourceHTTPFirewallPolicyId uint32 `field:"sourceHTTPFirewallPolicyId"` // 来源策略ID
|
||||||
|
SourceHTTPFirewallRuleGroupId uint32 `field:"sourceHTTPFirewallRuleGroupId"` // 来源规则集分组ID
|
||||||
|
SourceHTTPFirewallRuleSetId uint32 `field:"sourceHTTPFirewallRuleSetId"` // 来源规则集ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPItemOperator struct {
|
type IPItemOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
ListId interface{} // 所属名单ID
|
ListId interface{} // 所属名单ID
|
||||||
Type interface{} // 类型
|
Type interface{} // 类型
|
||||||
IpFrom interface{} // 开始IP
|
IpFrom interface{} // 开始IP
|
||||||
IpTo interface{} // 结束IP
|
IpTo interface{} // 结束IP
|
||||||
IpFromLong interface{} // 开始IP整型
|
IpFromLong interface{} // 开始IP整型
|
||||||
IpToLong interface{} // 结束IP整型
|
IpToLong interface{} // 结束IP整型
|
||||||
Version interface{} // 版本
|
Version interface{} // 版本
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
UpdatedAt interface{} // 修改时间
|
UpdatedAt interface{} // 修改时间
|
||||||
Reason interface{} // 加入说明
|
Reason interface{} // 加入说明
|
||||||
EventLevel interface{} // 事件级别
|
EventLevel interface{} // 事件级别
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
ExpiredAt interface{} // 过期时间
|
ExpiredAt interface{} // 过期时间
|
||||||
|
ServerId interface{} // 有效范围服务ID
|
||||||
|
NodeId interface{} // 有效范围节点ID
|
||||||
|
SourceNodeId interface{} // 来源节点ID
|
||||||
|
SourceServerId interface{} // 来源服务ID
|
||||||
|
SourceHTTPFirewallPolicyId interface{} // 来源策略ID
|
||||||
|
SourceHTTPFirewallRuleGroupId interface{} // 来源规则集分组ID
|
||||||
|
SourceHTTPFirewallRuleSetId interface{} // 来源规则集ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPItemOperator() *IPItemOperator {
|
func NewIPItemOperator() *IPItemOperator {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -16,7 +18,16 @@ const (
|
|||||||
IPListStateDisabled = 0 // 已禁用
|
IPListStateDisabled = 0 // 已禁用
|
||||||
)
|
)
|
||||||
|
|
||||||
var listTypeCacheMap = map[int64]string{} // listId => type
|
var listTypeCacheMap = map[int64]*IPList{} // listId => *IPList
|
||||||
|
var DefaultGlobalIPList = &IPList{
|
||||||
|
Id: uint32(firewallconfigs.GlobalListId),
|
||||||
|
Name: "全局封锁名单",
|
||||||
|
IsPublic: 1,
|
||||||
|
IsGlobal: 1,
|
||||||
|
Type: "black",
|
||||||
|
State: IPListStateEnabled,
|
||||||
|
IsOn: 1,
|
||||||
|
}
|
||||||
|
|
||||||
type IPListDAO dbs.DAO
|
type IPListDAO dbs.DAO
|
||||||
|
|
||||||
@@ -58,7 +69,19 @@ func (this *IPListDAO) DisableIPList(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledIPList 查找启用中的条目
|
// FindEnabledIPList 查找启用中的条目
|
||||||
func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64) (*IPList, error) {
|
func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64, cacheMap *utils.CacheMap) (*IPList, error) {
|
||||||
|
if id == firewallconfigs.GlobalListId {
|
||||||
|
return DefaultGlobalIPList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheKey = this.Table + ":FindEnabledIPList:" + types.String(id)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.(*IPList), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
Attr("state", IPListStateEnabled).
|
Attr("state", IPListStateEnabled).
|
||||||
@@ -66,6 +89,11 @@ func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64) (*IPList, error)
|
|||||||
if result == nil {
|
if result == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return result.(*IPList), err
|
return result.(*IPList), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,38 +105,39 @@ func (this *IPListDAO) FindIPListName(tx *dbs.Tx, id int64) (string, error) {
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindIPListTypeCacheable 获取名单类型
|
// FindIPListCacheable 获取名单
|
||||||
func (this *IPListDAO) FindIPListTypeCacheable(tx *dbs.Tx, listId int64) (string, error) {
|
func (this *IPListDAO) FindIPListCacheable(tx *dbs.Tx, listId int64) (*IPList, error) {
|
||||||
|
// 全局黑名单
|
||||||
|
if listId == firewallconfigs.GlobalListId {
|
||||||
|
return DefaultGlobalIPList, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 检查缓存
|
// 检查缓存
|
||||||
SharedCacheLocker.RLock()
|
SharedCacheLocker.RLock()
|
||||||
listType, ok := listTypeCacheMap[listId]
|
list, ok := listTypeCacheMap[listId]
|
||||||
SharedCacheLocker.RUnlock()
|
SharedCacheLocker.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return listType, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
listType, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(listId).
|
Pk(listId).
|
||||||
Result("type").
|
Result("isGlobal", "type", "state", "id", "isPublic", "isGlobal").
|
||||||
FindStringCol("")
|
Find()
|
||||||
if err != nil {
|
if err != nil || one == nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
if len(listType) == 0 {
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存缓存
|
// 保存缓存
|
||||||
SharedCacheLocker.Lock()
|
SharedCacheLocker.Lock()
|
||||||
listTypeCacheMap[listId] = listType
|
listTypeCacheMap[listId] = one.(*IPList)
|
||||||
SharedCacheLocker.Unlock()
|
SharedCacheLocker.Unlock()
|
||||||
|
|
||||||
return listType, nil
|
return one.(*IPList), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateIPList 创建名单
|
// CreateIPList 创建名单
|
||||||
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool) (int64, error) {
|
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool, isGlobal bool) (int64, error) {
|
||||||
op := NewIPListOperator()
|
op := NewIPListOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
@@ -121,6 +150,7 @@ func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs
|
|||||||
}
|
}
|
||||||
op.Description = description
|
op.Description = description
|
||||||
op.IsPublic = isPublic
|
op.IsPublic = isPublic
|
||||||
|
op.IsGlobal = isGlobal
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type IPList struct {
|
|||||||
Actions string `field:"actions"` // IP触发的动作
|
Actions string `field:"actions"` // IP触发的动作
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
IsPublic uint8 `field:"isPublic"` // 是否公用
|
IsPublic uint8 `field:"isPublic"` // 是否公用
|
||||||
|
IsGlobal uint8 `field:"isGlobal"` // 是否全局
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPListOperator struct {
|
type IPListOperator struct {
|
||||||
@@ -31,6 +32,7 @@ type IPListOperator struct {
|
|||||||
Actions interface{} // IP触发的动作
|
Actions interface{} // IP触发的动作
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
IsPublic interface{} // 是否公用
|
IsPublic interface{} // 是否公用
|
||||||
|
IsGlobal interface{} // 是否全局
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPListOperator() *IPListOperator {
|
func NewIPListOperator() *IPListOperator {
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ const (
|
|||||||
type MessageType = string
|
type MessageType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// 这里的命名问题(首字母大写)为历史遗留问题,暂不修改
|
||||||
|
|
||||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
||||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
||||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
||||||
@@ -36,8 +38,9 @@ const (
|
|||||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||||
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功
|
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功(用户)
|
||||||
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败(用户)
|
||||||
|
MessageTypeServerNamesRequireAuditing MessageType = "serverNamesRequireAuditing" // 服务域名需要审核(管理员)
|
||||||
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
||||||
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
||||||
MessageTypeIPAddrUp MessageType = "IPAddrUp" // IP地址上线
|
MessageTypeIPAddrUp MessageType = "IPAddrUp" // IP地址上线
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -55,12 +56,12 @@ func (this *MessageMediaInstanceDAO) DisableMessageMediaInstance(tx *dbs.Tx, id
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledMessageMediaInstance 查找启用中的条目
|
// FindEnabledMessageMediaInstance 查找启用中的条目
|
||||||
func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap maps.Map) (*MessageMediaInstance, error) {
|
func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap *utils.CacheMap) (*MessageMediaInstance, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":record:" + types.String(instanceId)
|
var cacheKey = this.Table + ":record:" + types.String(instanceId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*MessageMediaInstance), nil
|
return cache.(*MessageMediaInstance), nil
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,9 @@ func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = result
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return result.(*MessageMediaInstance), err
|
return result.(*MessageMediaInstance), err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
@@ -57,13 +57,13 @@ func (this *MessageRecipientDAO) DisableMessageRecipient(tx *dbs.Tx, id int64) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledMessageRecipient 查找启用中的条目
|
// FindEnabledMessageRecipient 查找启用中的条目
|
||||||
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap maps.Map,
|
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap *utils.CacheMap,
|
||||||
) (*MessageRecipient, error) {
|
) (*MessageRecipient, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":record:" + types.String(recipientId)
|
var cacheKey = this.Table + ":record:" + types.String(recipientId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*MessageRecipient), nil
|
return cache.(*MessageRecipient), nil
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,9 @@ func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipie
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = result
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return result.(*MessageRecipient), err
|
return result.(*MessageRecipient), err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -45,14 +47,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMessageTaskDAO.CleanExpiredMessageTasks(nil, 30) // 只保留30天
|
err := SharedMessageTaskDAO.CleanExpiredMessageTasks(nil, 30) // 只保留30天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("SharedMessageTaskDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("SharedMessageTaskDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +96,10 @@ func (this *MessageTaskDAO) FindEnabledMessageTask(tx *dbs.Tx, id int64) (*Messa
|
|||||||
|
|
||||||
// CreateMessageTask 创建任务
|
// CreateMessageTask 创建任务
|
||||||
func (this *MessageTaskDAO) CreateMessageTask(tx *dbs.Tx, recipientId int64, instanceId int64, user string, subject string, body string, isPrimary bool) (int64, error) {
|
func (this *MessageTaskDAO) CreateMessageTask(tx *dbs.Tx, recipientId int64, instanceId int64, user string, subject string, body string, isPrimary bool) (int64, error) {
|
||||||
|
if !teaconst.IsPlus {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
var hash = stringutil.Md5(types.String(recipientId) + "@" + types.String(instanceId) + "@" + user + "@" + subject + "@" + types.String(isPrimary))
|
var hash = stringutil.Md5(types.String(recipientId) + "@" + types.String(instanceId) + "@" + user + "@" + subject + "@" + types.String(isPrimary))
|
||||||
recipientInstanceId, err := SharedMessageRecipientDAO.FindRecipientInstanceId(tx, recipientId)
|
recipientInstanceId, err := SharedMessageRecipientDAO.FindRecipientInstanceId(tx, recipientId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -196,6 +202,10 @@ func (this *MessageTaskDAO) UpdateMessageTaskStatus(tx *dbs.Tx, taskId int64, st
|
|||||||
|
|
||||||
// CreateMessageTasks 从集群、节点或者服务中创建任务
|
// CreateMessageTasks 从集群、节点或者服务中创建任务
|
||||||
func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64, nodeId int64, serverId int64, messageType MessageType, subject string, body string) error {
|
func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64, nodeId int64, serverId int64, messageType MessageType, subject string, body string) error {
|
||||||
|
if !teaconst.IsPlus {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
receivers, err := SharedMessageReceiverDAO.FindEnabledBestFitReceivers(tx, role, clusterId, nodeId, serverId, messageType)
|
receivers, err := SharedMessageReceiverDAO.FindEnabledBestFitReceivers(tx, role, clusterId, nodeId, serverId, messageType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -16,14 +17,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMessageTaskLogDAO.CleanExpiredLogs(nil, 30) // 只保留30天
|
err := SharedMessageTaskLogDAO.CleanExpiredLogs(nil, 30) // 只保留30天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("SharedMessageTaskLogDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("SharedMessageTaskLogDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -77,6 +78,12 @@ func (this *MetricItemDAO) DisableMetricItem(tx *dbs.Tx, itemId int64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = SharedMetricSumStatDAO.DeleteItemStats(tx, itemId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +236,16 @@ func (this *MetricItemDAO) ListEnabledItems(tx *dbs.Tx, category serverconfigs.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAllPublicItems 取得公用的指标
|
// FindAllPublicItems 取得公用的指标
|
||||||
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (result []*MetricItem, err error) {
|
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string, cacheMap *utils.CacheMap) (result []*MetricItem, err error) {
|
||||||
|
if cacheMap == nil {
|
||||||
|
cacheMap = utils.NewCacheMap()
|
||||||
|
}
|
||||||
|
var cacheKey = this.Table + ":FindAllPublicItems:" + category
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.([]*MetricItem), nil
|
||||||
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(MetricItemStateEnabled).
|
State(MetricItemStateEnabled).
|
||||||
Attr("userId", 0).
|
Attr("userId", 0).
|
||||||
@@ -238,6 +254,14 @@ func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (resu
|
|||||||
DescPk().
|
DescPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -20,14 +22,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMetricStatDAO.Clean(nil, 120) // 只保留120天
|
err := SharedMetricStatDAO.Clean(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("SharedMetricStatDAO: clean expired data failed: " + err.Error())
|
logs.Println("SharedMetricStatDAO: clean expired data failed: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,12 +463,39 @@ func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clean 清理数据
|
// Clean 清理数据
|
||||||
func (this *MetricStatDAO) Clean(tx *dbs.Tx, days int64) error {
|
func (this *MetricStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
_, err := this.Query(tx).
|
for _, category := range serverconfigs.FindAllMetricItemCategoryCodes() {
|
||||||
Lt("createdDay", timeutil.FormatTime("Ymd", time.Now().Unix()-days*86400)).
|
var offset int64 = 0
|
||||||
Delete()
|
var size int64 = 100
|
||||||
if err != nil {
|
for {
|
||||||
return err
|
items, err := SharedMetricItemDAO.ListEnabledItems(tx, category, offset, size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
var config = &serverconfigs.MetricItemConfig{
|
||||||
|
Id: int64(item.Id),
|
||||||
|
Period: int(item.Period),
|
||||||
|
PeriodUnit: item.PeriodUnit,
|
||||||
|
}
|
||||||
|
var expiresDay = config.ServerExpiresDay()
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", item.Id).
|
||||||
|
Lte("createdDay", expiresDay).
|
||||||
|
UseIndex("createdDay").
|
||||||
|
Limit(100_000). // 一次性不要删除太多,防止阻塞其他操作
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
@@ -10,14 +11,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNewMetricStatDAO_InsertMany(t *testing.T) {
|
func TestNewMetricStatDAO_InsertMany(t *testing.T) {
|
||||||
for i := 0; i <= 10_000_000; i++ {
|
for i := 0; i <= 1; i++ {
|
||||||
err := NewMetricStatDAO().CreateStat(nil, types.String(i)+"_v1", 18, int64(rands.Int(0, 10000)), int64(rands.Int(0, 10000)), int64(rands.Int(0, 100)), []string{"/html" + types.String(i)}, 1, timeutil.Format("Ymd"), 0)
|
err := NewMetricStatDAO().CreateStat(nil, types.String(i)+"_v1", 18, int64(rands.Int(0, 10000)), int64(rands.Int(0, 10000)), int64(rands.Int(0, 100)), []string{"/html" + types.String(i)}, 1, timeutil.Format("Ymd"), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if i % 10000 == 0 {
|
if i%10000 == 0 {
|
||||||
t.Log(i)
|
t.Log(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.Log("done")
|
t.Log("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMetricStatDAO_Clean(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := NewMetricStatDAO().Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,35 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MetricSumStatDAO dbs.DAO
|
type MetricSumStatDAO dbs.DAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedMetricSumStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SharedMetricSumStatDAO: clean expired data failed: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
||||||
return dbs.NewDAO(&MetricSumStatDAO{
|
return dbs.NewDAO(&MetricSumStatDAO{
|
||||||
DAOObject: dbs.DAOObject{
|
DAOObject: dbs.DAOObject{
|
||||||
@@ -32,14 +53,15 @@ func init() {
|
|||||||
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"nodeId": nodeId,
|
"nodeId": nodeId,
|
||||||
"serverId": serverId,
|
"serverId": serverId,
|
||||||
"itemId": itemId,
|
"itemId": itemId,
|
||||||
"version": version,
|
"version": version,
|
||||||
"time": time,
|
"time": time,
|
||||||
"count": count,
|
"count": count,
|
||||||
"total": total,
|
"total": total,
|
||||||
|
"createdDay": timeutil.Format("Ymd"),
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"count": count,
|
"count": count,
|
||||||
"total": total,
|
"total": total,
|
||||||
@@ -84,6 +106,7 @@ func (this *MetricSumStatDAO) FindSumAtTime(tx *dbs.Tx, time string, itemId int6
|
|||||||
// FindServerSum 查找某个服务的统计数据
|
// FindServerSum 查找某个服务的统计数据
|
||||||
func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("server_item_time").
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -102,6 +125,7 @@ func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time str
|
|||||||
// FindClusterSum 查找集群上的统计数据
|
// FindClusterSum 查找集群上的统计数据
|
||||||
func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("cluster_item_time").
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -120,6 +144,7 @@ func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time s
|
|||||||
// FindNodeSum 查找节点上的统计数据
|
// FindNodeSum 查找节点上的统计数据
|
||||||
func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("node_item_time").
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -134,3 +159,50 @@ func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string,
|
|||||||
}
|
}
|
||||||
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteItemStats 删除某个指标相关的统计数据
|
||||||
|
func (this *MetricSumStatDAO) DeleteItemStats(tx *dbs.Tx, itemId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", itemId).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理数据
|
||||||
|
func (this *MetricSumStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
for _, category := range serverconfigs.FindAllMetricItemCategoryCodes() {
|
||||||
|
var offset int64 = 0
|
||||||
|
var size int64 = 100
|
||||||
|
for {
|
||||||
|
items, err := SharedMetricItemDAO.ListEnabledItems(tx, category, offset, size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
var config = &serverconfigs.MetricItemConfig{
|
||||||
|
Id: int64(item.Id),
|
||||||
|
Period: int(item.Period),
|
||||||
|
PeriodUnit: item.PeriodUnit,
|
||||||
|
}
|
||||||
|
var expiresDay = config.ServerExpiresDay()
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", item.Id).
|
||||||
|
Where("(createdDay IS NULL OR createdDay<:day)").
|
||||||
|
Param("day", expiresDay).
|
||||||
|
UseIndex("createdDay").
|
||||||
|
Limit(100_000). // 一次性不要删除太多,防止阻塞其他操作
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,4 +3,15 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMetricSumStatDAO_Clean(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := NewMetricSumStatDAO().Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,27 +2,29 @@ package models
|
|||||||
|
|
||||||
// MetricSumStat 指标统计总和数据
|
// MetricSumStat 指标统计总和数据
|
||||||
type MetricSumStat struct {
|
type MetricSumStat struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
ServerId uint32 `field:"serverId"` // 服务ID
|
ServerId uint32 `field:"serverId"` // 服务ID
|
||||||
ItemId uint64 `field:"itemId"` // 指标
|
ItemId uint64 `field:"itemId"` // 指标
|
||||||
Count uint64 `field:"count"` // 数量
|
Count uint64 `field:"count"` // 数量
|
||||||
Total float64 `field:"total"` // 总和
|
Total float64 `field:"total"` // 总和
|
||||||
Time string `field:"time"` // 分钟值YYYYMMDDHHII
|
Time string `field:"time"` // 分钟值YYYYMMDDHHII
|
||||||
Version uint32 `field:"version"` // 版本号
|
Version uint32 `field:"version"` // 版本号
|
||||||
|
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricSumStatOperator struct {
|
type MetricSumStatOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId interface{} // 集群ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId interface{} // 服务ID
|
||||||
ItemId interface{} // 指标
|
ItemId interface{} // 指标
|
||||||
Count interface{} // 数量
|
Count interface{} // 数量
|
||||||
Total interface{} // 总和
|
Total interface{} // 总和
|
||||||
Time interface{} // 分钟值YYYYMMDDHHII
|
Time interface{} // 分钟值YYYYMMDDHHII
|
||||||
Version interface{} // 版本号
|
Version interface{} // 版本号
|
||||||
|
CreatedDay interface{} // 创建日期YYYYMMDD
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package nameservers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedNSRecordHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedNSRecordHourlyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
@@ -177,7 +178,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCluster 修改集群
|
// UpdateCluster 修改集群
|
||||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string) error {
|
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, nodeTCPMaxConnections int32) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return errors.New("invalid clusterId")
|
return errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
@@ -186,8 +187,23 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
|
|||||||
op.Name = name
|
op.Name = name
|
||||||
op.GrantId = grantId
|
op.GrantId = grantId
|
||||||
op.InstallDir = installDir
|
op.InstallDir = installDir
|
||||||
|
op.TimeZone = timezone
|
||||||
|
|
||||||
|
if nodeMaxThreads < 0 {
|
||||||
|
nodeMaxThreads = 0
|
||||||
|
}
|
||||||
|
op.NodeMaxThreads = nodeMaxThreads
|
||||||
|
|
||||||
|
if nodeTCPMaxConnections < 0 {
|
||||||
|
nodeTCPMaxConnections = 0
|
||||||
|
}
|
||||||
|
op.NodeTCPMaxConnections = nodeTCPMaxConnections
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, clusterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledClusters 计算所有集群数量
|
// CountAllEnabledClusters 计算所有集群数量
|
||||||
@@ -403,14 +419,13 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterDNSInfo 查找DNS信息
|
// FindClusterDNSInfo 查找DNS信息
|
||||||
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (*NodeCluster, error) {
|
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
||||||
if cacheMap == nil {
|
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
|
||||||
cacheMap = maps.Map{}
|
if cacheMap != nil {
|
||||||
}
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
var cacheKey = this.Table + ":record:" + types.String(clusterId)
|
if ok {
|
||||||
var cache = cacheMap.Get(cacheKey)
|
return cache.(*NodeCluster), nil
|
||||||
if cache != nil {
|
}
|
||||||
return cache.(*NodeCluster), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
@@ -423,7 +438,9 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
|
|||||||
if one == nil {
|
if one == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
cacheMap[cacheKey] = one
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, one)
|
||||||
|
}
|
||||||
return one.(*NodeCluster), nil
|
return one.(*NodeCluster), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -483,7 +500,15 @@ func (this *NodeClusterDAO) FindClusterAdminId(tx *dbs.Tx, clusterId int64) (int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterTOAConfig 查找集群的TOA设置
|
// FindClusterTOAConfig 查找集群的TOA设置
|
||||||
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*nodeconfigs.TOAConfig, error) {
|
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.TOAConfig, error) {
|
||||||
|
var cacheKey = this.Table + ":FindClusterTOAConfig:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.(*nodeconfigs.TOAConfig), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toa, err := this.Query(tx).
|
toa, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
Result("toa").
|
Result("toa").
|
||||||
@@ -500,6 +525,11 @@ func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -569,6 +599,22 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledNodeClusterIds 查找所有可用的集群
|
||||||
|
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIds(tx *dbs.Tx) ([]int64, error) {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
State(NodeClusterStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result = []int64{}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*NodeCluster).Id))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
|
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
|
||||||
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) (result []int64, err error) {
|
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) (result []int64, err error) {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
@@ -583,12 +629,12 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *db
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
|
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
|
||||||
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) {
|
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":FindClusterHTTPFirewallPolicyId:" + types.String(clusterId)
|
var cacheKey = this.Table + ":FindClusterHTTPFirewallPolicyId:" + types.String(clusterId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(int64), nil
|
return cache.(int64), nil
|
||||||
}
|
}
|
||||||
@@ -601,7 +647,9 @@ func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterI
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = firewallPolicyId
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, firewallPolicyId)
|
||||||
|
}
|
||||||
|
|
||||||
return firewallPolicyId, nil
|
return firewallPolicyId, nil
|
||||||
}
|
}
|
||||||
@@ -619,12 +667,12 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(tx *dbs.Tx, clust
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
|
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
|
||||||
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) {
|
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":FindClusterHTTPCachePolicyId:" + types.String(clusterId)
|
var cacheKey = this.Table + ":FindClusterHTTPCachePolicyId:" + types.String(clusterId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(int64), nil
|
return cache.(int64), nil
|
||||||
}
|
}
|
||||||
@@ -637,7 +685,9 @@ func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId i
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = cachePolicyId
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, cachePolicyId)
|
||||||
|
}
|
||||||
|
|
||||||
return cachePolicyId, nil
|
return cachePolicyId, nil
|
||||||
}
|
}
|
||||||
@@ -716,10 +766,19 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clust
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindNodeClusterSystemServices 查找集群的所有服务设置
|
// FindNodeClusterSystemServices 查找集群的所有服务设置
|
||||||
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64) (services map[string]maps.Map, err error) {
|
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (services map[string]maps.Map, err error) {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return nil, errors.New("invalid clusterId")
|
return nil, errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cacheKey = this.Table + ":FindNodeClusterSystemServices:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.(map[string]maps.Map), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
service, err := this.Query(tx).
|
service, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
Result("systemServices").
|
Result("systemServices").
|
||||||
@@ -734,6 +793,11 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, servicesMap)
|
||||||
|
}
|
||||||
|
|
||||||
return servicesMap, nil
|
return servicesMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -811,6 +875,29 @@ func (this *NodeClusterDAO) ExistsEnabledCluster(tx *dbs.Tx, clusterId int64) (b
|
|||||||
Exist()
|
Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindClusterBasicInfo 查找集群基础信息
|
||||||
|
func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
||||||
|
var cacheKey = this.Table + ":FindClusterBasicInfo:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.(*NodeCluster), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cluster, err := this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("timeZone", "nodeMaxThreads", "nodeTCPMaxConnections", "cachePolicyId", "httpFirewallPolicyId").
|
||||||
|
Find()
|
||||||
|
if err != nil || cluster == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, cluster)
|
||||||
|
}
|
||||||
|
return cluster.(*NodeCluster), nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -35,7 +37,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableFirewallAction 启用条目
|
||||||
func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
|
func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -44,7 +46,7 @@ func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id ui
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableFirewallAction 禁用条目
|
||||||
func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error {
|
func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(actionId).
|
Pk(actionId).
|
||||||
@@ -56,7 +58,7 @@ func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, acti
|
|||||||
return this.NotifyUpdate(tx, actionId)
|
return this.NotifyUpdate(tx, actionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledFirewallAction 查找启用中的条目
|
||||||
func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) {
|
func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(actionId).
|
Pk(actionId).
|
||||||
@@ -68,7 +70,7 @@ func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx,
|
|||||||
return result.(*NodeClusterFirewallAction), err
|
return result.(*NodeClusterFirewallAction), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindFirewallActionName 根据主键查找名称
|
||||||
func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
|
func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -76,7 +78,7 @@ func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建动作
|
// CreateFirewallAction 创建动作
|
||||||
func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) {
|
func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) {
|
||||||
if params == nil {
|
if params == nil {
|
||||||
params = maps.Map{}
|
params = maps.Map{}
|
||||||
@@ -101,7 +103,7 @@ func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, admin
|
|||||||
return actionId, nil
|
return actionId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改动作
|
// UpdateFirewallAction 修改动作
|
||||||
func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error {
|
func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error {
|
||||||
if actionId <= 0 {
|
if actionId <= 0 {
|
||||||
return errors.New("invalid actionId")
|
return errors.New("invalid actionId")
|
||||||
@@ -124,17 +126,33 @@ func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actio
|
|||||||
return this.NotifyUpdate(tx, actionId)
|
return this.NotifyUpdate(tx, actionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找所有集群的动作
|
// FindAllEnabledFirewallActions 查找所有集群的动作
|
||||||
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (result []*NodeClusterFirewallAction, err error) {
|
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []*NodeClusterFirewallAction, err error) {
|
||||||
|
var cacheKey = this.Table + ":FindAllEnabledFirewallActions:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.([]*NodeClusterFirewallAction), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
State(NodeClusterFirewallActionStateEnabled).
|
State(NodeClusterFirewallActionStateEnabled).
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组合配置
|
// ComposeFirewallActionConfig 组合配置
|
||||||
func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) {
|
func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) {
|
||||||
if action == nil {
|
if action == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -153,7 +171,7 @@ func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算动作数量
|
// CountAllEnabledFirewallActions 计算动作数量
|
||||||
func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) {
|
func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(NodeClusterFirewallActionStateEnabled).
|
State(NodeClusterFirewallActionStateEnabled).
|
||||||
@@ -161,7 +179,7 @@ func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs
|
|||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error {
|
func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error {
|
||||||
clusterId, err := this.Query(tx).
|
clusterId, err := this.Query(tx).
|
||||||
Pk(actionId).
|
Pk(actionId).
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -112,16 +114,32 @@ func (this *NodeClusterMetricItemDAO) FindAllClusterItems(tx *dbs.Tx, clusterId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAllClusterItemIds 查找某个集群的指标Ids
|
// FindAllClusterItemIds 查找某个集群的指标Ids
|
||||||
func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64) (result []int64, err error) {
|
func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []int64, err error) {
|
||||||
|
var cacheKey = this.Table + ":FindAllClusterItemIds:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.([]int64), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
State(NodeClusterMetricItemStateEnabled).
|
State(NodeClusterMetricItemStateEnabled).
|
||||||
Result("itemId").
|
Result("itemId").
|
||||||
DescPk().
|
DescPk().
|
||||||
FindAll()
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
for _, one := range ones {
|
for _, one := range ones {
|
||||||
result = append(result, int64(one.(*NodeClusterMetricItem).ItemId))
|
result = append(result, int64(one.(*NodeClusterMetricItem).ItemId))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,57 +2,63 @@ package models
|
|||||||
|
|
||||||
// NodeCluster 节点集群
|
// NodeCluster 节点集群
|
||||||
type NodeCluster struct {
|
type NodeCluster struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
||||||
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
||||||
InstallDir string `field:"installDir"` // 安装目录
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
GrantId uint32 `field:"grantId"` // 默认认证方式
|
GrantId uint32 `field:"grantId"` // 默认认证方式
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
||||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
HealthCheck string `field:"healthCheck"` // 健康检查
|
HealthCheck string `field:"healthCheck"` // 健康检查
|
||||||
DnsName string `field:"dnsName"` // DNS名称
|
DnsName string `field:"dnsName"` // DNS名称
|
||||||
DnsDomainId uint32 `field:"dnsDomainId"` // 域名ID
|
DnsDomainId uint32 `field:"dnsDomainId"` // 域名ID
|
||||||
Dns string `field:"dns"` // DNS配置
|
Dns string `field:"dns"` // DNS配置
|
||||||
Toa string `field:"toa"` // TOA配置
|
Toa string `field:"toa"` // TOA配置
|
||||||
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
|
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
|
||||||
HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID
|
HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID
|
||||||
AccessLog string `field:"accessLog"` // 访问日志设置
|
AccessLog string `field:"accessLog"` // 访问日志设置
|
||||||
SystemServices string `field:"systemServices"` // 系统服务设置
|
SystemServices string `field:"systemServices"` // 系统服务设置
|
||||||
|
TimeZone string `field:"timeZone"` // 时区
|
||||||
|
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
|
||||||
|
NodeTCPMaxConnections uint32 `field:"nodeTCPMaxConnections"` // TCP最大连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeClusterOperator struct {
|
type NodeClusterOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
UseAllAPINodes interface{} // 是否使用所有API节点
|
UseAllAPINodes interface{} // 是否使用所有API节点
|
||||||
ApiNodes interface{} // 使用的API节点
|
ApiNodes interface{} // 使用的API节点
|
||||||
InstallDir interface{} // 安装目录
|
InstallDir interface{} // 安装目录
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
GrantId interface{} // 默认认证方式
|
GrantId interface{} // 默认认证方式
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
AutoRegister interface{} // 是否开启自动注册
|
AutoRegister interface{} // 是否开启自动注册
|
||||||
UniqueId interface{} // 唯一ID
|
UniqueId interface{} // 唯一ID
|
||||||
Secret interface{} // 密钥
|
Secret interface{} // 密钥
|
||||||
HealthCheck interface{} // 健康检查
|
HealthCheck interface{} // 健康检查
|
||||||
DnsName interface{} // DNS名称
|
DnsName interface{} // DNS名称
|
||||||
DnsDomainId interface{} // 域名ID
|
DnsDomainId interface{} // 域名ID
|
||||||
Dns interface{} // DNS配置
|
Dns interface{} // DNS配置
|
||||||
Toa interface{} // TOA配置
|
Toa interface{} // TOA配置
|
||||||
CachePolicyId interface{} // 缓存策略ID
|
CachePolicyId interface{} // 缓存策略ID
|
||||||
HttpFirewallPolicyId interface{} // WAF策略ID
|
HttpFirewallPolicyId interface{} // WAF策略ID
|
||||||
AccessLog interface{} // 访问日志设置
|
AccessLog interface{} // 访问日志设置
|
||||||
SystemServices interface{} // 系统服务设置
|
SystemServices interface{} // 系统服务设置
|
||||||
|
TimeZone interface{} // 时区
|
||||||
|
NodeMaxThreads interface{} // 节点最大线程数
|
||||||
|
NodeTCPMaxConnections interface{} // TCP最大连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeClusterOperator() *NodeClusterOperator {
|
func NewNodeClusterOperator() *NodeClusterOperator {
|
||||||
|
|||||||
@@ -315,7 +315,7 @@ func (this *NodeDAO) ListEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
case configutils.BoolStateAll:
|
case configutils.BoolStateAll:
|
||||||
// 所有
|
// 所有
|
||||||
case configutils.BoolStateYes:
|
case configutils.BoolStateYes:
|
||||||
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
query.Where("isActive AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
||||||
case configutils.BoolStateNo:
|
case configutils.BoolStateNo:
|
||||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||||
}
|
}
|
||||||
@@ -498,6 +498,22 @@ func (this *NodeDAO) FindAllEnabledNodesWithClusterId(tx *dbs.Tx, clusterId int6
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledNodeIdsWithClusterId 获取一个集群的所有节点Ids
|
||||||
|
func (this *NodeDAO) FindAllEnabledNodeIdsWithClusterId(tx *dbs.Tx, clusterId int64) (result []int64, err error) {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
State(NodeStateEnabled).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*Node).Id))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllInactiveNodesWithClusterId 取得一个集群离线的节点
|
// FindAllInactiveNodesWithClusterId 取得一个集群离线的节点
|
||||||
func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*Node, err error) {
|
func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*Node, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
@@ -553,7 +569,7 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
case configutils.BoolStateAll:
|
case configutils.BoolStateAll:
|
||||||
// 所有
|
// 所有
|
||||||
case configutils.BoolStateYes:
|
case configutils.BoolStateYes:
|
||||||
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
query.Where("isActive AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
||||||
case configutils.BoolStateNo:
|
case configutils.BoolStateNo:
|
||||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||||
}
|
}
|
||||||
@@ -581,6 +597,7 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(nodeId).
|
Pk(nodeId).
|
||||||
|
Set("isActive", true).
|
||||||
Set("status", string(statusJSON)).
|
Set("status", string(statusJSON)).
|
||||||
Update()
|
Update()
|
||||||
return err
|
return err
|
||||||
@@ -685,7 +702,11 @@ func (this *NodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *N
|
|||||||
|
|
||||||
// ComposeNodeConfig 组合配置
|
// ComposeNodeConfig 组合配置
|
||||||
// TODO 提升运行速度
|
// TODO 提升运行速度
|
||||||
func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.Map) (*nodeconfigs.NodeConfig, error) {
|
func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils.CacheMap) (*nodeconfigs.NodeConfig, error) {
|
||||||
|
if cacheMap == nil {
|
||||||
|
cacheMap = utils.NewCacheMap()
|
||||||
|
}
|
||||||
|
|
||||||
node, err := this.FindEnabledNode(tx, nodeId)
|
node, err := this.FindEnabledNode(tx, nodeId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -706,6 +727,13 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
RegionId: int64(node.RegionId),
|
RegionId: int64(node.RegionId),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API节点IP
|
||||||
|
apiNodeIPs, err := SharedAPINodeDAO.FindAllEnabledAPIAccessIPs(tx, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.AllowedIPs = append(config.AllowedIPs, apiNodeIPs...)
|
||||||
|
|
||||||
// 获取所有的服务
|
// 获取所有的服务
|
||||||
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(tx, int64(node.Id))
|
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(tx, int64(node.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -713,7 +741,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap)
|
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -721,14 +749,27 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
config.Servers = append(config.Servers, serverConfig)
|
config.Servers = append(config.Servers, serverConfig)
|
||||||
|
|
||||||
|
if server.IsOn == 1 && server.SupportCNAME == 1 {
|
||||||
|
config.SupportCNAME = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全局设置
|
// 全局设置
|
||||||
// TODO 根据用户的不同读取不同的全局设置
|
// TODO 根据用户的不同读取不同的全局设置
|
||||||
settingJSON, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
|
var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
|
||||||
if err != nil {
|
settingJSONCache, ok := cacheMap.Get(settingCacheKey)
|
||||||
return nil, err
|
var settingJSON = []byte{}
|
||||||
|
if ok {
|
||||||
|
settingJSON = settingJSONCache.([]byte)
|
||||||
|
} else {
|
||||||
|
settingJSON, err = SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cacheMap.Put(settingCacheKey, settingJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(settingJSON) > 0 {
|
if len(settingJSON) > 0 {
|
||||||
globalConfig := &serverconfigs.GlobalConfig{}
|
globalConfig := &serverconfigs.GlobalConfig{}
|
||||||
err = json.Unmarshal(settingJSON, globalConfig)
|
err = json.Unmarshal(settingJSON, globalConfig)
|
||||||
@@ -741,11 +782,17 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
var primaryClusterId = int64(node.ClusterId)
|
var primaryClusterId = int64(node.ClusterId)
|
||||||
var clusterIds = []int64{primaryClusterId}
|
var clusterIds = []int64{primaryClusterId}
|
||||||
clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
|
clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
|
||||||
|
var clusterIndex = 0
|
||||||
for _, clusterId := range clusterIds {
|
for _, clusterId := range clusterIds {
|
||||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
nodeCluster, err := SharedNodeClusterDAO.FindClusterBasicInfo(tx, clusterId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if nodeCluster == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpFirewallPolicyId = int64(nodeCluster.HttpFirewallPolicyId)
|
||||||
if httpFirewallPolicyId > 0 {
|
if httpFirewallPolicyId > 0 {
|
||||||
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, cacheMap)
|
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -757,10 +804,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 缓存策略
|
// 缓存策略
|
||||||
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
|
var httpCachePolicyId = int64(nodeCluster.CachePolicyId)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if httpCachePolicyId > 0 {
|
if httpCachePolicyId > 0 {
|
||||||
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId, cacheMap)
|
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -770,6 +814,22 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
config.HTTPCachePolicies = append(config.HTTPCachePolicies, cachePolicy)
|
config.HTTPCachePolicies = append(config.HTTPCachePolicies, cachePolicy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 时区
|
||||||
|
if len(config.TimeZone) == 0 {
|
||||||
|
var timeZone = nodeCluster.TimeZone
|
||||||
|
if len(timeZone) > 0 {
|
||||||
|
config.TimeZone = timeZone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最大线程数、TCP连接数
|
||||||
|
if clusterIndex == 0 {
|
||||||
|
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
|
||||||
|
config.TCPMaxConnections = int(nodeCluster.NodeTCPMaxConnections)
|
||||||
|
}
|
||||||
|
|
||||||
|
clusterIndex++
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存最大容量设置
|
// 缓存最大容量设置
|
||||||
@@ -796,14 +856,14 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TOA
|
// TOA
|
||||||
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId)
|
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.TOA = toaConfig
|
config.TOA = toaConfig
|
||||||
|
|
||||||
// 系统服务
|
// 系统服务
|
||||||
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId)
|
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -812,7 +872,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 防火墙动作
|
// 防火墙动作
|
||||||
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId)
|
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -827,7 +887,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 集群指标
|
// 集群指标
|
||||||
metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId))
|
metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId), cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -843,7 +903,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 公用指标
|
// 公用指标
|
||||||
publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP)
|
publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1398,7 +1458,7 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged, 0)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -46,13 +46,13 @@ func TestNodeDAO_ComposeNodeConfig(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
var cacheMap = maps.Map{}
|
var cacheMap = utils.NewCacheMap()
|
||||||
nodeConfig, err := SharedNodeDAO.ComposeNodeConfig(tx, 48, cacheMap)
|
nodeConfig, err := SharedNodeDAO.ComposeNodeConfig(tx, 48, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(len(nodeConfig.Servers), "servers")
|
t.Log(len(nodeConfig.Servers), "servers")
|
||||||
t.Log(len(cacheMap), "items")
|
t.Log(cacheMap.Len(), "items")
|
||||||
|
|
||||||
// old: 77ms => new: 56ms
|
// old: 77ms => new: 56ms
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateGrant 创建认证信息
|
// CreateGrant 创建认证信息
|
||||||
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) (grantId int64, err error) {
|
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64, su bool) (grantId int64, err error) {
|
||||||
op := NewNodeGrantOperator()
|
op := NewNodeGrantOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -83,11 +83,12 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
|
|||||||
case "user":
|
case "user":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.Password = password
|
op.Password = password
|
||||||
op.Su = false // TODO 需要做到前端可以配置
|
|
||||||
case "privateKey":
|
case "privateKey":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.PrivateKey = privateKey
|
op.PrivateKey = privateKey
|
||||||
|
op.Passphrase = passphrase
|
||||||
}
|
}
|
||||||
|
op.Su = su
|
||||||
op.Description = description
|
op.Description = description
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
op.State = NodeGrantStateEnabled
|
op.State = NodeGrantStateEnabled
|
||||||
@@ -96,7 +97,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGrant 修改认证信息
|
// UpdateGrant 修改认证信息
|
||||||
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) error {
|
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64, su bool) error {
|
||||||
if grantId <= 0 {
|
if grantId <= 0 {
|
||||||
return errors.New("invalid grantId")
|
return errors.New("invalid grantId")
|
||||||
}
|
}
|
||||||
@@ -110,11 +111,12 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
|
|||||||
case "user":
|
case "user":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.Password = password
|
op.Password = password
|
||||||
op.Su = false // TODO 需要做到前端可以配置
|
|
||||||
case "privateKey":
|
case "privateKey":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.PrivateKey = privateKey
|
op.PrivateKey = privateKey
|
||||||
|
op.Passphrase = passphrase
|
||||||
}
|
}
|
||||||
|
op.Su = su
|
||||||
op.Description = description
|
op.Description = description
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ type NodeGrant struct {
|
|||||||
Username string `field:"username"` // 用户名
|
Username string `field:"username"` // 用户名
|
||||||
Password string `field:"password"` // 密码
|
Password string `field:"password"` // 密码
|
||||||
Su uint8 `field:"su"` // 是否需要su
|
Su uint8 `field:"su"` // 是否需要su
|
||||||
PrivateKey string `field:"privateKey"` // 密钥
|
PrivateKey string `field:"privateKey"` // 私钥
|
||||||
|
Passphrase string `field:"passphrase"` // 私钥密码
|
||||||
Description string `field:"description"` // 备注
|
Description string `field:"description"` // 备注
|
||||||
NodeId uint32 `field:"nodeId"` // 专有节点
|
NodeId uint32 `field:"nodeId"` // 专有节点
|
||||||
Role string `field:"role"` // 角色
|
Role string `field:"role"` // 角色
|
||||||
@@ -25,7 +26,8 @@ type NodeGrantOperator struct {
|
|||||||
Username interface{} // 用户名
|
Username interface{} // 用户名
|
||||||
Password interface{} // 密码
|
Password interface{} // 密码
|
||||||
Su interface{} // 是否需要su
|
Su interface{} // 是否需要su
|
||||||
PrivateKey interface{} // 密钥
|
PrivateKey interface{} // 私钥
|
||||||
|
Passphrase interface{} // 私钥密码
|
||||||
Description interface{} // 备注
|
Description interface{} // 备注
|
||||||
NodeId interface{} // 专有节点
|
NodeId interface{} // 专有节点
|
||||||
Role interface{} // 角色
|
Role interface{} // 角色
|
||||||
|
|||||||
@@ -102,8 +102,24 @@ func (this *NodeIPAddressDAO) FindAddressName(tx *dbs.Tx, id int64) (string, err
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAddressIsHealthy 判断IP地址是否健康
|
||||||
|
func (this *NodeIPAddressDAO) FindAddressIsHealthy(tx *dbs.Tx, addressId int64) (isHealthy bool, err error) {
|
||||||
|
if addressId <= 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(addressId).
|
||||||
|
Result("isHealthy").
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
var addr = one.(*NodeIPAddress)
|
||||||
|
return addr.IsHealthy == 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CreateAddress 创建IP地址
|
// CreateAddress 创建IP地址
|
||||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool) (addressId int64, err error) {
|
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool, groupId int64) (addressId int64, err error) {
|
||||||
if len(role) == 0 {
|
if len(role) == 0 {
|
||||||
role = nodeconfigs.NodeRoleNode
|
role = nodeconfigs.NodeRoleNode
|
||||||
}
|
}
|
||||||
@@ -115,6 +131,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId in
|
|||||||
op.Ip = ip
|
op.Ip = ip
|
||||||
op.CanAccess = canAccess
|
op.CanAccess = canAccess
|
||||||
op.IsUp = isUp
|
op.IsUp = isUp
|
||||||
|
op.GroupId = groupId
|
||||||
|
|
||||||
op.State = NodeIPAddressStateEnabled
|
op.State = NodeIPAddressStateEnabled
|
||||||
addressId, err = this.SaveInt64(tx, op)
|
addressId, err = this.SaveInt64(tx, op)
|
||||||
@@ -216,11 +233,11 @@ func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(tx *dbs.Tx, nodeId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
|
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
|
||||||
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (string, error) {
|
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (ip string, addrId int64, err error) {
|
||||||
if len(role) == 0 {
|
if len(role) == 0 {
|
||||||
role = nodeconfigs.NodeRoleNode
|
role = nodeconfigs.NodeRoleNode
|
||||||
}
|
}
|
||||||
return this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
State(NodeIPAddressStateEnabled).
|
State(NodeIPAddressStateEnabled).
|
||||||
@@ -229,8 +246,17 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId in
|
|||||||
Attr("isUp", true).
|
Attr("isUp", true).
|
||||||
Desc("order").
|
Desc("order").
|
||||||
AscPk().
|
AscPk().
|
||||||
Result("ip").
|
Result("id", "ip").
|
||||||
FindStringCol("")
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, err
|
||||||
|
}
|
||||||
|
if one == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr = one.(*NodeIPAddress)
|
||||||
|
return addr.Ip, int64(addr.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
|
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
|
||||||
@@ -400,6 +426,10 @@ func (this *NodeIPAddressDAO) UpdateAddressIsUp(tx *dbs.Tx, addressId int64, isU
|
|||||||
var err = this.Query(tx).
|
var err = this.Query(tx).
|
||||||
Pk(addressId).
|
Pk(addressId).
|
||||||
Set("isUp", isUp).
|
Set("isUp", isUp).
|
||||||
|
Set("countUp", 0).
|
||||||
|
Set("countDown", 0).
|
||||||
|
Set("backupIP", "").
|
||||||
|
Set("backupThresholdId", 0).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -413,6 +443,7 @@ func (this *NodeIPAddressDAO) UpdateAddressBackupIP(tx *dbs.Tx, addressId int64,
|
|||||||
return errors.New("invalid addressId")
|
return errors.New("invalid addressId")
|
||||||
}
|
}
|
||||||
var op = NewNodeIPAddressOperator()
|
var op = NewNodeIPAddressOperator()
|
||||||
|
op.IsUp = true // IP必须在线备用IP才会有用
|
||||||
op.Id = addressId
|
op.Id = addressId
|
||||||
op.BackupThresholdId = thresholdId
|
op.BackupThresholdId = thresholdId
|
||||||
op.BackupIP = ip
|
op.BackupIP = ip
|
||||||
@@ -423,6 +454,71 @@ func (this *NodeIPAddressDAO) UpdateAddressBackupIP(tx *dbs.Tx, addressId int64,
|
|||||||
return this.NotifyUpdate(tx, addressId)
|
return this.NotifyUpdate(tx, addressId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateAddressHealthCount 计算IP健康状态
|
||||||
|
func (this *NodeIPAddressDAO) UpdateAddressHealthCount(tx *dbs.Tx, addrId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
|
||||||
|
if addrId <= 0 {
|
||||||
|
return false, errors.New("invalid address id")
|
||||||
|
}
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(addrId).
|
||||||
|
Result("isHealthy", "countUp", "countDown").
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if one == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
oldIsHealthy := one.(*NodeIPAddress).IsHealthy == 1
|
||||||
|
|
||||||
|
// 如果新老状态一致,则不做任何事情
|
||||||
|
if oldIsHealthy == isUp {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
countUp := int(one.(*NodeIPAddress).CountUp)
|
||||||
|
countDown := int(one.(*NodeIPAddress).CountDown)
|
||||||
|
|
||||||
|
op := NewNodeIPAddressOperator()
|
||||||
|
op.Id = addrId
|
||||||
|
|
||||||
|
if isUp {
|
||||||
|
countUp++
|
||||||
|
countDown = 0
|
||||||
|
|
||||||
|
if countUp >= maxUp {
|
||||||
|
changed = true
|
||||||
|
//op.IsUp = true
|
||||||
|
op.IsHealthy = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
countDown++
|
||||||
|
countUp = 0
|
||||||
|
|
||||||
|
if countDown >= maxDown {
|
||||||
|
changed = true
|
||||||
|
//op.IsUp = false
|
||||||
|
op.IsHealthy = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op.CountUp = countUp
|
||||||
|
op.CountDown = countDown
|
||||||
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
err = this.NotifyUpdate(tx, addrId)
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
|
func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
|
||||||
address, err := this.Query(tx).
|
address, err := this.Query(tx).
|
||||||
|
|||||||
@@ -40,3 +40,25 @@ func TestNodeIPAddressDAO_LoopTasks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNodeIPAddressDAO_FindAddressIsHealthy(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
isHealthy, err := SharedNodeIPAddressDAO.FindAddressIsHealthy(tx, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("isHealthy:", isHealthy)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeIPAddressDAO_UpdateAddressHealthCount(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
isChanged, err := SharedNodeIPAddressDAO.UpdateAddressHealthCount(tx, 1, true, 3, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("isChanged:", isChanged)
|
||||||
|
}
|
||||||
|
|||||||
44
internal/db/models/node_ip_address_group_dao.go
Normal file
44
internal/db/models/node_ip_address_group_dao.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NodeIPAddressGroupDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewNodeIPAddressGroupDAO() *NodeIPAddressGroupDAO {
|
||||||
|
return dbs.NewDAO(&NodeIPAddressGroupDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeNodeIPAddressGroups",
|
||||||
|
Model: new(NodeIPAddressGroup),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*NodeIPAddressGroupDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedNodeIPAddressGroupDAO *NodeIPAddressGroupDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedNodeIPAddressGroupDAO = NewNodeIPAddressGroupDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindNodeIPAddressGroupName 根据主键查找名称
|
||||||
|
func (this *NodeIPAddressGroupDAO) FindNodeIPAddressGroupName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateGroup 创建分组
|
||||||
|
func (this *NodeIPAddressGroupDAO) CreateGroup(tx *dbs.Tx, name string, value string) (int64, error) {
|
||||||
|
var op = NewNodeIPAddressGroupOperator()
|
||||||
|
op.Name = name
|
||||||
|
op.Value = value
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
6
internal/db/models/node_ip_address_group_dao_test.go
Normal file
6
internal/db/models/node_ip_address_group_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
18
internal/db/models/node_ip_address_group_model.go
Normal file
18
internal/db/models/node_ip_address_group_model.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// NodeIPAddressGroup IP地址分组
|
||||||
|
type NodeIPAddressGroup struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 分组名
|
||||||
|
Value string `field:"value"` // IP值
|
||||||
|
}
|
||||||
|
|
||||||
|
type NodeIPAddressGroupOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Name interface{} // 分组名
|
||||||
|
Value interface{} // IP值
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNodeIPAddressGroupOperator() *NodeIPAddressGroupOperator {
|
||||||
|
return &NodeIPAddressGroupOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/node_ip_address_group_model_ext.go
Normal file
1
internal/db/models/node_ip_address_group_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -5,6 +5,7 @@ type NodeIPAddress struct {
|
|||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
Role string `field:"role"` // 节点角色
|
Role string `field:"role"` // 节点角色
|
||||||
|
GroupId uint32 `field:"groupId"` // 所属分组ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Ip string `field:"ip"` // IP地址
|
Ip string `field:"ip"` // IP地址
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
@@ -13,16 +14,20 @@ type NodeIPAddress struct {
|
|||||||
CanAccess uint8 `field:"canAccess"` // 是否可以访问
|
CanAccess uint8 `field:"canAccess"` // 是否可以访问
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
IsUp uint8 `field:"isUp"` // 是否上线
|
IsUp uint8 `field:"isUp"` // 是否上线
|
||||||
|
IsHealthy uint8 `field:"isHealthy"` // 是否健康
|
||||||
Thresholds string `field:"thresholds"` // 上线阈值
|
Thresholds string `field:"thresholds"` // 上线阈值
|
||||||
Connectivity string `field:"connectivity"` // 连通性状态
|
Connectivity string `field:"connectivity"` // 连通性状态
|
||||||
BackupIP string `field:"backupIP"` // 备用IP
|
BackupIP string `field:"backupIP"` // 备用IP
|
||||||
BackupThresholdId uint32 `field:"backupThresholdId"` // 触发备用IP的阈值
|
BackupThresholdId uint32 `field:"backupThresholdId"` // 触发备用IP的阈值
|
||||||
|
CountUp uint32 `field:"countUp"` // UP状态次数
|
||||||
|
CountDown uint32 `field:"countDown"` // DOWN状态次数
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeIPAddressOperator struct {
|
type NodeIPAddressOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
Role interface{} // 节点角色
|
Role interface{} // 节点角色
|
||||||
|
GroupId interface{} // 所属分组ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Ip interface{} // IP地址
|
Ip interface{} // IP地址
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
@@ -31,10 +36,13 @@ type NodeIPAddressOperator struct {
|
|||||||
CanAccess interface{} // 是否可以访问
|
CanAccess interface{} // 是否可以访问
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
IsUp interface{} // 是否上线
|
IsUp interface{} // 是否上线
|
||||||
|
IsHealthy interface{} // 是否健康
|
||||||
Thresholds interface{} // 上线阈值
|
Thresholds interface{} // 上线阈值
|
||||||
Connectivity interface{} // 连通性状态
|
Connectivity interface{} // 连通性状态
|
||||||
BackupIP interface{} // 备用IP
|
BackupIP interface{} // 备用IP
|
||||||
BackupThresholdId interface{} // 触发备用IP的阈值
|
BackupThresholdId interface{} // 触发备用IP的阈值
|
||||||
|
CountUp interface{} // UP状态次数
|
||||||
|
CountDown interface{} // DOWN状态次数
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeIPAddressOperator() *NodeIPAddressOperator {
|
func NewNodeIPAddressOperator() *NodeIPAddressOperator {
|
||||||
|
|||||||
@@ -41,8 +41,25 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLog 创建日志
|
// CreateLog 创建日志
|
||||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error {
|
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
|
||||||
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description)
|
// 修复以前同样的日志
|
||||||
|
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
||||||
|
err := this.Query(tx).
|
||||||
|
Attr("nodeId", nodeId).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("type", logType).
|
||||||
|
Attr("level", "error").
|
||||||
|
Attr("isFixed", 0).
|
||||||
|
JSONContains("params", string(paramsJSON)).
|
||||||
|
Set("isFixed", 1).
|
||||||
|
Set("isRead", 1).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description + "@" + string(paramsJSON))
|
||||||
|
|
||||||
// 检查是否在重复最后一条,避免重复创建
|
// 检查是否在重复最后一条,避免重复创建
|
||||||
lastLog, err := this.Query(tx).
|
lastLog, err := this.Query(tx).
|
||||||
@@ -75,10 +92,32 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
|
|||||||
op.Day = timeutil.FormatTime("Ymd", createdAt)
|
op.Day = timeutil.FormatTime("Ymd", createdAt)
|
||||||
op.Hash = hash
|
op.Hash = hash
|
||||||
op.Count = 1
|
op.Count = 1
|
||||||
|
op.IsRead = level != "error"
|
||||||
|
|
||||||
|
op.Type = logType
|
||||||
|
if len(paramsJSON) > 0 {
|
||||||
|
op.Params = paramsJSON
|
||||||
|
}
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteExpiredLogsWithLevel 清除超出一定日期的某级别日志
|
||||||
|
func (this *NodeLogDAO) DeleteExpiredLogsWithLevel(tx *dbs.Tx, level string, days int) error {
|
||||||
|
if days <= 0 {
|
||||||
|
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
|
||||||
|
}
|
||||||
|
date := time.Now().AddDate(0, 0, -days)
|
||||||
|
expireDay := timeutil.Format("Ymd", date)
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("level", level).
|
||||||
|
Where("day<=:day").
|
||||||
|
Param("day", expireDay).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteExpiredLogs 清除超出一定日期的日志
|
// DeleteExpiredLogs 清除超出一定日期的日志
|
||||||
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
||||||
if days <= 0 {
|
if days <= 0 {
|
||||||
@@ -94,9 +133,20 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountNodeLogs 计算节点日志数量
|
// CountNodeLogs 计算节点日志数量
|
||||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, originId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx,
|
||||||
query := this.Query(tx).
|
role string,
|
||||||
Attr("role", role)
|
nodeId int64,
|
||||||
|
serverId int64,
|
||||||
|
originId int64,
|
||||||
|
dayFrom string,
|
||||||
|
dayTo string,
|
||||||
|
keyword string,
|
||||||
|
level string,
|
||||||
|
isUnread bool) (int64, error) {
|
||||||
|
query := this.Query(tx)
|
||||||
|
if len(role) > 0 {
|
||||||
|
query.Attr("role", role)
|
||||||
|
}
|
||||||
if nodeId > 0 {
|
if nodeId > 0 {
|
||||||
query.Attr("nodeId", nodeId)
|
query.Attr("nodeId", nodeId)
|
||||||
} else {
|
} else {
|
||||||
@@ -128,6 +178,9 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
|
|||||||
if len(level) > 0 {
|
if len(level) > 0 {
|
||||||
query.Attr("level", level)
|
query.Attr("level", level)
|
||||||
}
|
}
|
||||||
|
if isUnread {
|
||||||
|
query.Attr("isRead", 0)
|
||||||
|
}
|
||||||
|
|
||||||
return query.Count()
|
return query.Count()
|
||||||
}
|
}
|
||||||
@@ -144,10 +197,13 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
|||||||
keyword string,
|
keyword string,
|
||||||
level string,
|
level string,
|
||||||
fixedState configutils.BoolState,
|
fixedState configutils.BoolState,
|
||||||
|
isUnread bool,
|
||||||
offset int64,
|
offset int64,
|
||||||
size int64) (result []*NodeLog, err error) {
|
size int64) (result []*NodeLog, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx)
|
||||||
Attr("role", role)
|
if len(role) > 0 {
|
||||||
|
query.Attr("role", role)
|
||||||
|
}
|
||||||
if nodeId > 0 {
|
if nodeId > 0 {
|
||||||
query.Attr("nodeId", nodeId)
|
query.Attr("nodeId", nodeId)
|
||||||
} else {
|
} else {
|
||||||
@@ -186,6 +242,9 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
|||||||
if len(level) > 0 {
|
if len(level) > 0 {
|
||||||
query.Attr("level", level)
|
query.Attr("level", level)
|
||||||
}
|
}
|
||||||
|
if isUnread {
|
||||||
|
query.Attr("isRead", 0)
|
||||||
|
}
|
||||||
_, err = query.
|
_, err = query.
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(size).
|
Limit(size).
|
||||||
@@ -217,6 +276,7 @@ func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
|
|||||||
Attr("hash", hash).
|
Attr("hash", hash).
|
||||||
Attr("isFixed", false).
|
Attr("isFixed", false).
|
||||||
Set("isFixed", true).
|
Set("isFixed", true).
|
||||||
|
Set("isRead", true).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -224,3 +284,32 @@ func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountAllUnreadNodeLogs 计算未读的日志数量
|
||||||
|
func (this *NodeLogDAO) CountAllUnreadNodeLogs(tx *dbs.Tx) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("isRead", false).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeLogsRead 设置日志为已读
|
||||||
|
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error {
|
||||||
|
for _, logId := range nodeLogIds {
|
||||||
|
err := this.Query(tx).
|
||||||
|
Pk(logId).
|
||||||
|
Set("isRead", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateAllNodeLogsRead 设置所有日志为已读
|
||||||
|
func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("isRead", false).
|
||||||
|
Set("isRead", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package models
|
|||||||
type NodeLog struct {
|
type NodeLog struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
Role string `field:"role"` // 节点角色
|
Role string `field:"role"` // 节点角色
|
||||||
|
Type string `field:"type"` // 类型
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
Tag string `field:"tag"` // 标签
|
Tag string `field:"tag"` // 标签
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
@@ -15,11 +16,14 @@ type NodeLog struct {
|
|||||||
Hash string `field:"hash"` // 信息内容Hash
|
Hash string `field:"hash"` // 信息内容Hash
|
||||||
Count uint32 `field:"count"` // 重复次数
|
Count uint32 `field:"count"` // 重复次数
|
||||||
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||||
|
IsRead uint8 `field:"isRead"` // 是否已读
|
||||||
|
Params string `field:"params"` // 参数
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeLogOperator struct {
|
type NodeLogOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
Role interface{} // 节点角色
|
Role interface{} // 节点角色
|
||||||
|
Type interface{} // 类型
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
Tag interface{} // 标签
|
Tag interface{} // 标签
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
@@ -31,6 +35,8 @@ type NodeLogOperator struct {
|
|||||||
Hash interface{} // 信息内容Hash
|
Hash interface{} // 信息内容Hash
|
||||||
Count interface{} // 重复次数
|
Count interface{} // 重复次数
|
||||||
IsFixed interface{} // 是否已处理
|
IsFixed interface{} // 是否已处理
|
||||||
|
IsRead interface{} // 是否已读
|
||||||
|
Params interface{} // 参数
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeLogOperator() *NodeLogOperator {
|
func NewNodeLogOperator() *NodeLogOperator {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableNodePriceItem 启用条目
|
||||||
func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
|
func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -44,7 +44,7 @@ func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableNodePriceItem 禁用条目
|
||||||
func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
|
func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -53,7 +53,7 @@ func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledNodePriceItem 查找启用中的条目
|
||||||
func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*NodePriceItem, error) {
|
func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*NodePriceItem, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -65,7 +65,7 @@ func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*N
|
|||||||
return result.(*NodePriceItem), err
|
return result.(*NodePriceItem), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindNodePriceItemName 根据主键查找名称
|
||||||
func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -73,7 +73,7 @@ func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (strin
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建价格
|
// CreateItem 创建价格
|
||||||
func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType string, bitsFrom, bitsTo int64) (int64, error) {
|
func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType string, bitsFrom, bitsTo int64) (int64, error) {
|
||||||
op := NewNodePriceItemOperator()
|
op := NewNodePriceItemOperator()
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -85,7 +85,7 @@ func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType strin
|
|||||||
return this.SaveInt64(tx, op)
|
return this.SaveInt64(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改价格
|
// UpdateItem 修改价格
|
||||||
func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, bitsFrom, bitsTo int64) error {
|
func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, bitsFrom, bitsTo int64) error {
|
||||||
if itemId <= 0 {
|
if itemId <= 0 {
|
||||||
return errors.New("invalid itemId")
|
return errors.New("invalid itemId")
|
||||||
@@ -98,7 +98,7 @@ func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string,
|
|||||||
return this.Save(tx, op)
|
return this.Save(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出某个区域的所有价格
|
// FindAllEnabledRegionPrices 列出某个区域的所有价格
|
||||||
func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("type", priceType).
|
Attr("type", priceType).
|
||||||
@@ -109,7 +109,7 @@ func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType s
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出某个区域的所有启用的价格
|
// FindAllEnabledAndOnRegionPrices 列出某个区域的所有启用的价格
|
||||||
func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("type", priceType).
|
Attr("type", priceType).
|
||||||
@@ -121,7 +121,7 @@ func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据字节查找付费项目
|
// SearchItemsWithBytes 根据字节查找付费项目
|
||||||
func (this *NodePriceItemDAO) SearchItemsWithBytes(items []*NodePriceItem, bytes int64) int64 {
|
func (this *NodePriceItemDAO) SearchItemsWithBytes(items []*NodePriceItem, bytes int64) int64 {
|
||||||
bytes *= 8
|
bytes *= 8
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableNodeRegion 启用条目
|
||||||
func (this *NodeRegionDAO) EnableNodeRegion(tx *dbs.Tx, id int64) error {
|
func (this *NodeRegionDAO) EnableNodeRegion(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -44,7 +44,7 @@ func (this *NodeRegionDAO) EnableNodeRegion(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableNodeRegion 禁用条目
|
||||||
func (this *NodeRegionDAO) DisableNodeRegion(tx *dbs.Tx, id int64) error {
|
func (this *NodeRegionDAO) DisableNodeRegion(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -53,7 +53,7 @@ func (this *NodeRegionDAO) DisableNodeRegion(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledNodeRegion 查找启用中的条目
|
||||||
func (this *NodeRegionDAO) FindEnabledNodeRegion(tx *dbs.Tx, id int64) (*NodeRegion, error) {
|
func (this *NodeRegionDAO) FindEnabledNodeRegion(tx *dbs.Tx, id int64) (*NodeRegion, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -65,7 +65,7 @@ func (this *NodeRegionDAO) FindEnabledNodeRegion(tx *dbs.Tx, id int64) (*NodeReg
|
|||||||
return result.(*NodeRegion), err
|
return result.(*NodeRegion), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindNodeRegionName 根据主键查找名称
|
||||||
func (this *NodeRegionDAO) FindNodeRegionName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *NodeRegionDAO) FindNodeRegionName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -73,7 +73,7 @@ func (this *NodeRegionDAO) FindNodeRegionName(tx *dbs.Tx, id int64) (string, err
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建区域
|
// CreateRegion 创建区域
|
||||||
func (this *NodeRegionDAO) CreateRegion(tx *dbs.Tx, adminId int64, name string, description string) (int64, error) {
|
func (this *NodeRegionDAO) CreateRegion(tx *dbs.Tx, adminId int64, name string, description string) (int64, error) {
|
||||||
op := NewNodeRegionOperator()
|
op := NewNodeRegionOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
@@ -84,7 +84,7 @@ func (this *NodeRegionDAO) CreateRegion(tx *dbs.Tx, adminId int64, name string,
|
|||||||
return this.SaveInt64(tx, op)
|
return this.SaveInt64(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改区域
|
// UpdateRegion 修改区域
|
||||||
func (this *NodeRegionDAO) UpdateRegion(tx *dbs.Tx, regionId int64, name string, description string, isOn bool) error {
|
func (this *NodeRegionDAO) UpdateRegion(tx *dbs.Tx, regionId int64, name string, description string, isOn bool) error {
|
||||||
if regionId <= 0 {
|
if regionId <= 0 {
|
||||||
return errors.New("invalid regionId")
|
return errors.New("invalid regionId")
|
||||||
@@ -97,7 +97,7 @@ func (this *NodeRegionDAO) UpdateRegion(tx *dbs.Tx, regionId int64, name string,
|
|||||||
return this.Save(tx, op)
|
return this.Save(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出所有区域
|
// FindAllEnabledRegions 列出所有区域
|
||||||
func (this *NodeRegionDAO) FindAllEnabledRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
func (this *NodeRegionDAO) FindAllEnabledRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeRegionStateEnabled).
|
State(NodeRegionStateEnabled).
|
||||||
@@ -108,7 +108,7 @@ func (this *NodeRegionDAO) FindAllEnabledRegions(tx *dbs.Tx) (result []*NodeRegi
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出所有价格
|
// FindAllEnabledRegionPrices 列出所有价格
|
||||||
func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeRegionStateEnabled).
|
State(NodeRegionStateEnabled).
|
||||||
@@ -120,7 +120,7 @@ func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*Nod
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出所有启用的区域
|
// FindAllEnabledAndOnRegions 列出所有启用的区域
|
||||||
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeRegionStateEnabled).
|
State(NodeRegionStateEnabled).
|
||||||
@@ -132,7 +132,7 @@ func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*Nod
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排序
|
// UpdateRegionOrders 排序
|
||||||
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
|
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
|
||||||
order := len(regionIds)
|
order := len(regionIds)
|
||||||
for _, regionId := range regionIds {
|
for _, regionId := range regionIds {
|
||||||
@@ -148,7 +148,7 @@ func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改价格项价格
|
// UpdateRegionItemPrice 修改价格项价格
|
||||||
func (this *NodeRegionDAO) UpdateRegionItemPrice(tx *dbs.Tx, regionId int64, itemId int64, price float32) error {
|
func (this *NodeRegionDAO) UpdateRegionItemPrice(tx *dbs.Tx, regionId int64, itemId int64, price float32) error {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(regionId).
|
Pk(regionId).
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateNodeTask 创建单个节点任务
|
// CreateNodeTask 创建单个节点任务
|
||||||
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType) error {
|
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType, version int64) error {
|
||||||
if clusterId <= 0 || nodeId <= 0 {
|
if clusterId <= 0 || nodeId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -66,6 +66,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
|
|||||||
"isDone": 0,
|
"isDone": 0,
|
||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": version,
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"updatedAt": updatedAt,
|
"updatedAt": updatedAt,
|
||||||
@@ -73,6 +74,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
|
|||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
"isNotified": 0,
|
"isNotified": 0,
|
||||||
|
"version": version,
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -97,12 +99,14 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId in
|
|||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"isNotified": 0,
|
"isNotified": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"updatedAt": updatedAt,
|
"updatedAt": updatedAt,
|
||||||
"isDone": 0,
|
"isDone": 0,
|
||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"isNotified": 0,
|
"isNotified": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -125,8 +129,9 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, tas
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var version = time.Now().UnixNano()
|
||||||
for _, nodeId := range nodeIds {
|
for _, nodeId := range nodeIds {
|
||||||
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType)
|
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -156,15 +161,16 @@ func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskT
|
|||||||
Attr("role", nodeconfigs.NodeRoleDNS).
|
Attr("role", nodeconfigs.NodeRoleDNS).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Param("clusterIdString", types.String(clusterId)).
|
Param("clusterIdString", types.String(clusterId)).
|
||||||
Where("nodeId> 0").
|
Where("nodeId > 0").
|
||||||
Attr("type", taskType).
|
Attr("type", taskType).
|
||||||
Delete()
|
Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var version = time.Now().UnixNano()
|
||||||
for _, nodeId := range nodeIds {
|
for _, nodeId := range nodeIds {
|
||||||
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType)
|
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -305,20 +311,30 @@ func (this *NodeTaskDAO) FindAllDoingNodeIds(tx *dbs.Tx, role string) ([]int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExistsDoingNodeTasks 检查是否有正在执行的任务
|
// ExistsDoingNodeTasks 检查是否有正在执行的任务
|
||||||
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx, role string, excludeTypes []NodeTaskType) (bool, error) {
|
||||||
return this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0)
|
||||||
Exist()
|
if len(excludeTypes) > 0 {
|
||||||
|
for _, excludeType := range excludeTypes {
|
||||||
|
query.Neq("type", excludeType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query.Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExistsErrorNodeTasks 是否有错误的任务
|
// ExistsErrorNodeTasks 是否有错误的任务
|
||||||
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx, role string, excludeTypes []NodeTaskType) (bool, error) {
|
||||||
return this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Where("(isDone=1 AND isOk=0)").
|
Where("(isDone=1 AND isOk=0)")
|
||||||
Exist()
|
if len(excludeTypes) > 0 {
|
||||||
|
for _, excludeType := range excludeTypes {
|
||||||
|
query.Neq("type", excludeType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query.Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteNodeTask 删除任务
|
// DeleteNodeTask 删除任务
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func TestNodeTaskDAO_CreateNodeTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged)
|
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type NodeTask struct {
|
|||||||
IsOk uint8 `field:"isOk"` // 是否已完成
|
IsOk uint8 `field:"isOk"` // 是否已完成
|
||||||
Error string `field:"error"` // 错误信息
|
Error string `field:"error"` // 错误信息
|
||||||
IsNotified uint8 `field:"isNotified"` // 是否已通知更新
|
IsNotified uint8 `field:"isNotified"` // 是否已通知更新
|
||||||
|
Version uint64 `field:"version"` // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeTaskOperator struct {
|
type NodeTaskOperator struct {
|
||||||
@@ -27,6 +28,7 @@ type NodeTaskOperator struct {
|
|||||||
IsOk interface{} // 是否已完成
|
IsOk interface{} // 是否已完成
|
||||||
Error interface{} // 错误信息
|
Error interface{} // 错误信息
|
||||||
IsNotified interface{} // 是否已通知更新
|
IsNotified interface{} // 是否已通知更新
|
||||||
|
Version interface{} // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeTaskOperator() *NodeTaskOperator {
|
func NewNodeTaskOperator() *NodeTaskOperator {
|
||||||
|
|||||||
@@ -55,13 +55,13 @@ func (this *NodeValueDAO) CreateValue(tx *dbs.Tx, clusterId int64, role nodeconf
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteExpiredValues 清除数据
|
// Clean 清除数据
|
||||||
func (this *NodeValueDAO) DeleteExpiredValues(tx *dbs.Tx) error {
|
func (this *NodeValueDAO) Clean(tx *dbs.Tx) error {
|
||||||
// 删除N天之前的所有数据
|
// 删除N天之前的所有数据
|
||||||
expiredDays := 100
|
expiredDays := 2
|
||||||
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
|
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Where("day<:day").
|
Where("day<=:day").
|
||||||
Param("day", day).
|
Param("day", day).
|
||||||
Delete()
|
Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -14,7 +13,6 @@ import (
|
|||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -78,7 +76,7 @@ func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NS
|
|||||||
fields["domainId"] = accessLog.NsDomainId
|
fields["domainId"] = accessLog.NsDomainId
|
||||||
fields["recordId"] = accessLog.NsRecordId
|
fields["recordId"] = accessLog.NsRecordId
|
||||||
fields["createdAt"] = accessLog.Timestamp
|
fields["createdAt"] = accessLog.Timestamp
|
||||||
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
|
fields["requestId"] = accessLog.RequestId
|
||||||
|
|
||||||
content, err := json.Marshal(accessLog)
|
content, err := json.Marshal(accessLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -259,7 +257,7 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
|
|
||||||
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
||||||
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
|
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
|
||||||
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
|
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
|
||||||
return nil, errors.New("invalid requestId")
|
return nil, errors.New("invalid requestId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -220,12 +220,12 @@ func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, add
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeOriginConfig 将源站信息转换为配置
|
// ComposeOriginConfig 将源站信息转换为配置
|
||||||
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap maps.Map) (*serverconfigs.OriginConfig, error) {
|
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap *utils.CacheMap) (*serverconfigs.OriginConfig, error) {
|
||||||
if cacheMap == nil {
|
if cacheMap == nil {
|
||||||
cacheMap = maps.Map{}
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
var cacheKey = this.Table + ":config:" + types.String(originId)
|
var cacheKey = this.Table + ":config:" + types.String(originId)
|
||||||
var cache = cacheMap.Get(cacheKey)
|
var cache, _ = cacheMap.Get(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*serverconfigs.OriginConfig), nil
|
return cache.(*serverconfigs.OriginConfig), nil
|
||||||
}
|
}
|
||||||
@@ -358,7 +358,9 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMap[cacheKey] = config
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
273
internal/db/models/plan_dao.go
Normal file
273
internal/db/models/plan_dao.go
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PlanStateEnabled = 1 // 已启用
|
||||||
|
PlanStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type PlanDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewPlanDAO() *PlanDAO {
|
||||||
|
return dbs.NewDAO(&PlanDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgePlans",
|
||||||
|
Model: new(Plan),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*PlanDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedPlanDAO *PlanDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedPlanDAO = NewPlanDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnablePlan 启用条目
|
||||||
|
func (this *PlanDAO) EnablePlan(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", PlanStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisablePlan 禁用条目
|
||||||
|
func (this *PlanDAO) DisablePlan(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", PlanStateDisabled).
|
||||||
|
Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledPlan 查找启用中的条目
|
||||||
|
func (this *PlanDAO) FindEnabledPlan(tx *dbs.Tx, id int64) (*Plan, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", PlanStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*Plan), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindPlanName 根据主键查找名称
|
||||||
|
func (this *PlanDAO) FindPlanName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatePlan 创建套餐
|
||||||
|
func (this *PlanDAO) CreatePlan(tx *dbs.Tx, name string, clusterId int64, trafficLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, trafficPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) (int64, error) {
|
||||||
|
var op = NewPlanOperator()
|
||||||
|
op.Name = name
|
||||||
|
op.ClusterId = clusterId
|
||||||
|
if len(trafficLimitJSON) > 0 {
|
||||||
|
op.TrafficLimit = trafficLimitJSON
|
||||||
|
}
|
||||||
|
if len(featuresJSON) > 0 {
|
||||||
|
op.Features = featuresJSON
|
||||||
|
}
|
||||||
|
op.PriceType = priceType
|
||||||
|
if len(trafficPriceJSON) > 0 {
|
||||||
|
op.TrafficPrice = trafficPriceJSON
|
||||||
|
}
|
||||||
|
if monthlyPrice >= 0 {
|
||||||
|
op.MonthlyPrice = monthlyPrice
|
||||||
|
}
|
||||||
|
if seasonallyPrice >= 0 {
|
||||||
|
op.SeasonallyPrice = seasonallyPrice
|
||||||
|
}
|
||||||
|
if yearlyPrice >= 0 {
|
||||||
|
op.YearlyPrice = yearlyPrice
|
||||||
|
}
|
||||||
|
op.IsOn = true
|
||||||
|
op.State = PlanStateEnabled
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePlan 修改套餐
|
||||||
|
func (this *PlanDAO) UpdatePlan(tx *dbs.Tx, planId int64, name string, isOn bool, clusterId int64, trafficLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, trafficPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) error {
|
||||||
|
if planId <= 0 {
|
||||||
|
return errors.New("invalid planId")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查集群有无变化
|
||||||
|
oldClusterId, err := this.Query(tx).
|
||||||
|
Pk(planId).
|
||||||
|
Result("clusterId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var op = NewPlanOperator()
|
||||||
|
op.Id = planId
|
||||||
|
op.Name = name
|
||||||
|
op.IsOn = isOn
|
||||||
|
op.ClusterId = clusterId
|
||||||
|
if len(trafficLimitJSON) > 0 {
|
||||||
|
op.TrafficLimit = trafficLimitJSON
|
||||||
|
}
|
||||||
|
if len(featuresJSON) > 0 {
|
||||||
|
op.Features = featuresJSON
|
||||||
|
}
|
||||||
|
op.PriceType = priceType
|
||||||
|
if len(trafficPriceJSON) > 0 {
|
||||||
|
op.TrafficPrice = trafficPriceJSON
|
||||||
|
}
|
||||||
|
if monthlyPrice >= 0 {
|
||||||
|
op.MonthlyPrice = monthlyPrice
|
||||||
|
} else {
|
||||||
|
op.MonthlyPrice = 0
|
||||||
|
}
|
||||||
|
if seasonallyPrice >= 0 {
|
||||||
|
op.SeasonallyPrice = seasonallyPrice
|
||||||
|
} else {
|
||||||
|
op.SeasonallyPrice = 0
|
||||||
|
}
|
||||||
|
if yearlyPrice >= 0 {
|
||||||
|
op.YearlyPrice = yearlyPrice
|
||||||
|
} else {
|
||||||
|
op.YearlyPrice = 0
|
||||||
|
}
|
||||||
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldClusterId != clusterId {
|
||||||
|
// 修改服务所属集群
|
||||||
|
err = SharedServerDAO.UpdateServersClusterIdWithPlanId(tx, planId, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SharedNodeClusterDAO.NotifyUpdate(tx, oldClusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, planId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountAllEnabledPlans 计算套餐的数量
|
||||||
|
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(PlanStateEnabled).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListEnabledPlans 列出单页套餐
|
||||||
|
func (this *PlanDAO) ListEnabledPlans(tx *dbs.Tx, offset int64, size int64) (result []*Plan, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(PlanStateEnabled).
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
Desc("order").
|
||||||
|
DescPk().
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledPlans 查找所有可用套餐
|
||||||
|
func (this *PlanDAO) FindAllEnabledPlans(tx *dbs.Tx) (result []*Plan, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(PlanStateEnabled).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortPlans 增加排序
|
||||||
|
func (this *PlanDAO) SortPlans(tx *dbs.Tx, planIds []int64) error {
|
||||||
|
if len(planIds) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var order = len(planIds)
|
||||||
|
for _, planId := range planIds {
|
||||||
|
err := this.Query(tx).
|
||||||
|
Pk(planId).
|
||||||
|
Set("order", order).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
order--
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledPlanTrafficLimit 获取套餐的流量限制
|
||||||
|
func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
|
||||||
|
var cacheKey = this.Table + ":FindEnabledPlanTrafficLimit:" + types.String(planId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, _ := cacheMap.Get(cacheKey)
|
||||||
|
if cache != nil {
|
||||||
|
return cache.(*serverconfigs.TrafficLimitConfig), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trafficLimit, err := this.Query(tx).
|
||||||
|
Pk(planId).
|
||||||
|
State(PlanStateEnabled).
|
||||||
|
Result("trafficLimit").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(trafficLimit) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var config = &serverconfigs.TrafficLimitConfig{}
|
||||||
|
err = json.Unmarshal([]byte(trafficLimit), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知变更
|
||||||
|
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64) error {
|
||||||
|
// 这里不要加入状态参数,因为需要适应删除后的更新
|
||||||
|
clusterId, err := this.Query(tx).
|
||||||
|
Pk(planId).
|
||||||
|
Result("clusterId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if clusterId > 0 {
|
||||||
|
return SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
6
internal/db/models/plan_dao_test.go
Normal file
6
internal/db/models/plan_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
38
internal/db/models/plan_model.go
Normal file
38
internal/db/models/plan_model.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// Plan 用户套餐
|
||||||
|
type Plan struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
|
Name string `field:"name"` // 套餐名
|
||||||
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
|
TrafficLimit string `field:"trafficLimit"` // 流量限制
|
||||||
|
Features string `field:"features"` // 允许的功能
|
||||||
|
TrafficPrice string `field:"trafficPrice"` // 流量价格设定
|
||||||
|
MonthlyPrice float64 `field:"monthlyPrice"` // 月付
|
||||||
|
SeasonallyPrice float64 `field:"seasonallyPrice"` // 季付
|
||||||
|
YearlyPrice float64 `field:"yearlyPrice"` // 年付
|
||||||
|
PriceType string `field:"priceType"` // 价格类型
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type PlanOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Name interface{} // 套餐名
|
||||||
|
ClusterId interface{} // 集群ID
|
||||||
|
TrafficLimit interface{} // 流量限制
|
||||||
|
Features interface{} // 允许的功能
|
||||||
|
TrafficPrice interface{} // 流量价格设定
|
||||||
|
MonthlyPrice interface{} // 月付
|
||||||
|
SeasonallyPrice interface{} // 季付
|
||||||
|
YearlyPrice interface{} // 年付
|
||||||
|
PriceType interface{} // 价格类型
|
||||||
|
Order interface{} // 排序
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPlanOperator() *PlanOperator {
|
||||||
|
return &PlanOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/plan_model_ext.go
Normal file
1
internal/db/models/plan_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -119,7 +120,7 @@ func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId in
|
|||||||
cityId, err := this.Query(tx).
|
cityId, err := this.Query(tx).
|
||||||
Attr("provinceId", provinceId).
|
Attr("provinceId", provinceId).
|
||||||
Where("JSON_CONTAINS(codes, :cityName)").
|
Where("JSON_CONTAINS(codes, :cityName)").
|
||||||
Param("cityName", "\""+cityName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("cityName", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/mozillazg/go-pinyin"
|
"github.com/mozillazg/go-pinyin"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +16,8 @@ const (
|
|||||||
RegionCountryStateDisabled = 0 // 已禁用
|
RegionCountryStateDisabled = 0 // 已禁用
|
||||||
)
|
)
|
||||||
|
|
||||||
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => int
|
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => id
|
||||||
|
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
|
||||||
|
|
||||||
type RegionCountryDAO dbs.DAO
|
type RegionCountryDAO dbs.DAO
|
||||||
|
|
||||||
@@ -38,7 +40,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableRegionCountry 启用条目
|
||||||
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -47,7 +49,7 @@ func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableRegionCountry 禁用条目
|
||||||
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -56,7 +58,7 @@ func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledRegionCountry 查找启用中的条目
|
||||||
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -68,15 +70,29 @@ func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*R
|
|||||||
return result.(*RegionCountry), err
|
return result.(*RegionCountry), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindRegionCountryName 根据主键查找名称
|
||||||
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
SharedCacheLocker.Lock()
|
||||||
|
defer SharedCacheLocker.Unlock()
|
||||||
|
|
||||||
|
name, ok := regionCountryIdAndNameCacheMap[id]
|
||||||
|
if ok {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
name, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
Result("name").
|
Result("name").
|
||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
regionCountryIdAndNameCacheMap[id] = name
|
||||||
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据数据ID查找国家
|
// FindCountryIdWithDataId 根据数据ID查找国家
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Attr("dataId", dataId).
|
Attr("dataId", dataId).
|
||||||
@@ -84,16 +100,16 @@ func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string)
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据国家名查找国家ID
|
// FindCountryIdWithName 根据国家名查找国家ID
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :countryName)").
|
Where("JSON_CONTAINS(codes, :countryName)").
|
||||||
Param("countryName", "\""+countryName+"\""). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("countryName", strconv.Quote(countryName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据国家名查找国家ID,并可使用缓存
|
// FindCountryIdWithNameCacheable 根据国家名查找国家ID,并可使用缓存
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
|
||||||
SharedCacheLocker.RLock()
|
SharedCacheLocker.RLock()
|
||||||
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
|
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
|
||||||
@@ -115,7 +131,7 @@ func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, country
|
|||||||
return countryId, nil
|
return countryId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据数据ID创建国家
|
// CreateCountry 根据数据ID创建国家
|
||||||
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
||||||
op := NewRegionCountryOperator()
|
op := NewRegionCountryOperator()
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -144,7 +160,7 @@ func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId stri
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找所有可用的国家
|
// FindAllEnabledCountriesOrderByPinyin 查找所有可用的国家
|
||||||
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(RegionCountryStateEnabled).
|
State(RegionCountryStateEnabled).
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user