diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index 113f0062..461a8e77 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -87,6 +87,10 @@ func (this *RPCClient) APINodeRPC() pb.APINodeServiceClient { return pb.NewAPINodeServiceClient(this.pickConn()) } +func (this *RPCClient) DBNodeRPC() pb.DBNodeServiceClient { + return pb.NewDBNodeServiceClient(this.pickConn()) +} + func (this *RPCClient) OriginRPC() pb.OriginServiceClient { return pb.NewOriginServiceClient(this.pickConn()) } diff --git a/internal/web/actions/default/db/createPopup.go b/internal/web/actions/default/db/createPopup.go new file mode 100644 index 00000000..7ddfb022 --- /dev/null +++ b/internal/web/actions/default/db/createPopup.go @@ -0,0 +1,64 @@ +package db + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" +) + +type CreatePopupAction struct { + actionutils.ParentAction +} + +func (this *CreatePopupAction) Init() { + this.Nav("", "", "") +} + +func (this *CreatePopupAction) RunGet(params struct{}) { + this.Show() +} + +func (this *CreatePopupAction) RunPost(params struct { + Name string + Host string + Port int32 + Database string + Username string + Password string + + Description string + IsOn bool + + Must *actions.Must +}) { + params.Must. + Field("name", params.Name). + Require("请输入节点名称"). + Field("host", params.Host). + Require("请输入主机地址"). + Field("port", params.Port). + Gt(0, "请输入正确的数据库端口"). + Lt(65535, "请输入正确的数据库端口"). + Field("database", params.Database). + Require("请输入数据库名称"). + Field("username", params.Username). + Require("请输入连接数据库的用户名") + + _, err := this.RPC().DBNodeRPC().CreateDBNode(this.AdminContext(), &pb.CreateDBNodeRequest{ + IsOn: params.IsOn, + Name: params.Name, + Description: params.Description, + Host: params.Host, + Port: params.Port, + Database: params.Database, + Username: params.Username, + Password: params.Password, + Charset: "", // 暂时不能修改 + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/db/delete.go b/internal/web/actions/default/db/delete.go new file mode 100644 index 00000000..a5e2784d --- /dev/null +++ b/internal/web/actions/default/db/delete.go @@ -0,0 +1,22 @@ +package db + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +type DeleteAction struct { + actionutils.ParentAction +} + +func (this *DeleteAction) RunPost(params struct { + NodeId int64 +}) { + _, err := this.RPC().DBNodeRPC().DeleteDBNode(this.AdminContext(), &pb.DeleteDBNodeRequest{NodeId: params.NodeId}) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/db/index.go b/internal/web/actions/default/db/index.go index afe09374..44083214 100644 --- a/internal/web/actions/default/db/index.go +++ b/internal/web/actions/default/db/index.go @@ -1,6 +1,10 @@ package db -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/maps" +) type IndexAction struct { actionutils.ParentAction @@ -11,5 +15,36 @@ func (this *IndexAction) Init() { } func (this *IndexAction) RunGet(params struct{}) { + countResp, err := this.RPC().DBNodeRPC().CountAllEnabledDBNodes(this.AdminContext(), &pb.CountAllEnabledDBNodesRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + count := countResp.Count + + page := this.NewPage(count) + listResp, err := this.RPC().DBNodeRPC().ListEnabledDBNodes(this.AdminContext(), &pb.ListEnabledDBNodesRequest{ + Offset: page.Offset, + Size: page.Size, + }) + if err != nil { + this.ErrorPage(err) + return + } + nodeMaps := []maps.Map{} + for _, node := range listResp.Nodes { + nodeMaps = append(nodeMaps, maps.Map{ + "id": node.Id, + "isOn": node.IsOn, + "name": node.Name, + "host": node.Host, + "port": node.Port, + "database": node.Database, + }) + } + + this.Data["nodes"] = nodeMaps + this.Data["page"] = page.AsHTML() + this.Show() } diff --git a/internal/web/actions/default/db/init.go b/internal/web/actions/default/db/init.go index 5cecf4ba..3f353762 100644 --- a/internal/web/actions/default/db/init.go +++ b/internal/web/actions/default/db/init.go @@ -12,6 +12,9 @@ func init() { Helper(new(Helper)). Prefix("/db"). Get("", new(IndexAction)). + GetPost("/createPopup", new(CreatePopupAction)). + GetPost("/updatePopup", new(UpdatePopupAction)). + Post("/delete", new(DeleteAction)). EndAll() }) diff --git a/internal/web/actions/default/db/updatePopup.go b/internal/web/actions/default/db/updatePopup.go new file mode 100644 index 00000000..26291f05 --- /dev/null +++ b/internal/web/actions/default/db/updatePopup.go @@ -0,0 +1,94 @@ +package db + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/maps" +) + +type UpdatePopupAction struct { + actionutils.ParentAction +} + +func (this *UpdatePopupAction) Init() { + this.Nav("", "", "") +} + +func (this *UpdatePopupAction) RunGet(params struct { + NodeId int64 +}) { + nodeResp, err := this.RPC().DBNodeRPC().FindEnabledDBNode(this.AdminContext(), &pb.FindEnabledDBNodeRequest{NodeId: params.NodeId}) + if err != nil { + this.ErrorPage(err) + return + } + + node := nodeResp.Node + if node == nil { + this.NotFound("dbNode", params.NodeId) + return + } + + this.Data["node"] = maps.Map{ + "id": node.Id, + "isOn": node.IsOn, + "name": node.Name, + "description": node.Description, + "host": node.Host, + "port": node.Port, + "username": node.Username, + "password": node.Password, + "database": node.Database, + } + + this.Show() +} + +func (this *UpdatePopupAction) RunPost(params struct { + NodeId int64 + + Name string + Host string + Port int32 + Database string + Username string + Password string + + Description string + IsOn bool + + Must *actions.Must +}) { + params.Must. + Field("name", params.Name). + Require("请输入节点名称"). + Field("host", params.Host). + Require("请输入主机地址"). + Field("port", params.Port). + Gt(0, "请输入正确的数据库端口"). + Lt(65535, "请输入正确的数据库端口"). + Field("database", params.Database). + Require("请输入数据库名称"). + Field("username", params.Username). + Require("请输入连接数据库的用户名") + + _, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{ + NodeId: params.NodeId, + IsOn: params.IsOn, + Name: params.Name, + Description: params.Description, + Host: params.Host, + Port: params.Port, + Database: params.Database, + Username: params.Username, + Password: params.Password, + Charset: "", // 暂时不能修改 + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/helpers/user_must_auth.go b/internal/web/helpers/user_must_auth.go index d9eb56f0..06b2a7da 100644 --- a/internal/web/helpers/user_must_auth.go +++ b/internal/web/helpers/user_must_auth.go @@ -82,7 +82,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam }, { "code": "db", - "menuName": "数据库", + "menuName": "日志数据库", "icon": "database", }, { diff --git a/web/public/js/components/common/labels.js b/web/public/js/components/common/labels.js index 521231cc..c4bf19bf 100644 --- a/web/public/js/components/common/labels.js +++ b/web/public/js/components/common/labels.js @@ -1,4 +1,4 @@ Vue.component("label-on", { props: ["v-is-on"], - template: '
已启用已关闭
' + template: '
已启用已停用
' }) \ No newline at end of file diff --git a/web/views/@default/db/createPopup.html b/web/views/@default/db/createPopup.html new file mode 100644 index 00000000..05dcd8a5 --- /dev/null +++ b/web/views/@default/db/createPopup.html @@ -0,0 +1,71 @@ +{$layout "layout_popup"} + +

添加节点

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
节点名称 * + +

给节点起一个容易识别的名称。

+
主机地址(Host) * + +

连接数据库的主机地址,通常是域名或者IP。

+
数据库端口(Port) * + +

连接数据库的端口。

+
数据库名称(Database) * + +

要写入日志的数据库名称,需要有创建新表和读写数据的权限。

+
用户名(Username) * + +

连接数据库的用户名。

+
密码(Password) + +

连接数据库的密码。

+
详细描述 + +
是否启用 +
+ + +
+
+ +
\ No newline at end of file diff --git a/web/views/@default/db/createPopup.js b/web/views/@default/db/createPopup.js new file mode 100644 index 00000000..c8fe9515 --- /dev/null +++ b/web/views/@default/db/createPopup.js @@ -0,0 +1,3 @@ +Tea.context(function () { + this.success = NotifyPopup +}) \ No newline at end of file diff --git a/web/views/@default/db/index.html b/web/views/@default/db/index.html index a98dc909..6dcab805 100644 --- a/web/views/@default/db/index.html +++ b/web/views/@default/db/index.html @@ -1,3 +1,31 @@ {$layout} -

此功能暂未开放,敬请期待。

\ No newline at end of file + + [添加节点] + + +

暂时还没有数据库节点。

+ + + + + + + + + + + + + + + + + + +
节点名称连接地址数据库名状态操作
{{node.name}}{{node.host}}:{{node.port}}{{node.database}} + 修改   + 删除 +
+ +
\ No newline at end of file diff --git a/web/views/@default/db/index.js b/web/views/@default/db/index.js new file mode 100644 index 00000000..c927ada0 --- /dev/null +++ b/web/views/@default/db/index.js @@ -0,0 +1,37 @@ +Tea.context(function () { + // 添加节点 + this.createNode = function () { + teaweb.popup("/db/createPopup", { + height: "30em", + callback: function () { + teaweb.success("保存成功", function () { + window.location.reload() + }) + } + }) + } + + // 修改节点 + this.updateNode = function (nodeId) { + teaweb.popup("/db/updatePopup?nodeId=" + nodeId, { + height: "30em", + callback: function () { + teaweb.success("保存成功", function () { + window.location.reload() + }) + } + }) + } + + // 删除节点 + this.deleteNode = function (nodeId) { + let that = this + teaweb.confirm("确定要删除此数据库节点吗?", function () { + that.$post(".delete") + .params({ + nodeId: nodeId + }) + .refresh() + }) + } +}) \ No newline at end of file diff --git a/web/views/@default/db/updatePopup.html b/web/views/@default/db/updatePopup.html new file mode 100644 index 00000000..4ba79feb --- /dev/null +++ b/web/views/@default/db/updatePopup.html @@ -0,0 +1,72 @@ +{$layout "layout_popup"} + +

修改节点

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
节点名称 * + +

给节点起一个容易识别的名称。

+
主机地址(Host) * + +

连接数据库的主机地址,通常是域名或者IP。

+
数据库端口(Port) * + +

连接数据库的端口。

+
数据库名称(Database) * + +

要写入日志的数据库名称,需要有创建新表和读写数据的权限。

+
用户名(Username) * + +

连接数据库的用户名。

+
密码(Password) + +

连接数据库的密码。

+
详细描述 + +
是否启用 +
+ + +
+
+ +
\ No newline at end of file diff --git a/web/views/@default/db/updatePopup.js b/web/views/@default/db/updatePopup.js new file mode 100644 index 00000000..c8fe9515 --- /dev/null +++ b/web/views/@default/db/updatePopup.js @@ -0,0 +1,3 @@ +Tea.context(function () { + this.success = NotifyPopup +}) \ No newline at end of file