Skip to content

Commit

Permalink
feat: support sentinel mode #16 #42
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-craft committed Oct 7, 2023
1 parent 477ed19 commit ee68d69
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 21 deletions.
15 changes: 15 additions & 0 deletions backend/services/connection_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func (c *connectionService) createRedisClient(config types.ConnectionConfig) (*r
option.ReadTimeout = -2
option.WriteTimeout = -2
}

if config.Sentinel.Enable {
sentinel := redis.NewSentinelClient(option)
addr, err := sentinel.GetMasterAddrByName(c.ctx, config.Sentinel.Master).Result()
if err != nil {
return nil, err
}
if len(addr) < 2 {
return nil, errors.New("cannot get master address")
}
option.Addr = fmt.Sprintf("%s:%s", addr[0], addr[1])
option.Username = config.Sentinel.Username
option.Password = config.Sentinel.Password
}

rdb := redis.NewClient(option)
return rdb, nil
}
Expand Down
3 changes: 3 additions & 0 deletions backend/storage/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (c *ConnectionsStorage) defaultConnectionItem() types.ConnectionConfig {
DBFilterType: "none",
DBFilterList: []int{},
MarkColor: "",
Sentinel: types.ConnectionSentinel{
Master: "mymaster",
},
}
}

Expand Down
36 changes: 22 additions & 14 deletions backend/types/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package types
type ConnectionCategory int

type ConnectionConfig struct {
Name string `json:"name" yaml:"name"`
Group string `json:"group,omitempty" yaml:"-"`
Addr string `json:"addr,omitempty" yaml:"addr,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
DefaultFilter string `json:"defaultFilter,omitempty" yaml:"default_filter,omitempty"`
KeySeparator string `json:"keySeparator,omitempty" yaml:"key_separator,omitempty"`
ConnTimeout int `json:"connTimeout,omitempty" yaml:"conn_timeout,omitempty"`
ExecTimeout int `json:"execTimeout,omitempty" yaml:"exec_timeout,omitempty"`
DBFilterType string `json:"dbFilterType" yaml:"db_filter_type,omitempty"`
DBFilterList []int `json:"dbFilterList" yaml:"db_filter_list,omitempty"`
MarkColor string `json:"markColor,omitempty" yaml:"mark_color,omitempty"`
SSH ConnectionSSH `json:"ssh,omitempty" yaml:"ssh,omitempty"`
Name string `json:"name" yaml:"name"`
Group string `json:"group,omitempty" yaml:"-"`
Addr string `json:"addr,omitempty" yaml:"addr,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
DefaultFilter string `json:"defaultFilter,omitempty" yaml:"default_filter,omitempty"`
KeySeparator string `json:"keySeparator,omitempty" yaml:"key_separator,omitempty"`
ConnTimeout int `json:"connTimeout,omitempty" yaml:"conn_timeout,omitempty"`
ExecTimeout int `json:"execTimeout,omitempty" yaml:"exec_timeout,omitempty"`
DBFilterType string `json:"dbFilterType" yaml:"db_filter_type,omitempty"`
DBFilterList []int `json:"dbFilterList" yaml:"db_filter_list,omitempty"`
MarkColor string `json:"markColor,omitempty" yaml:"mark_color,omitempty"`
SSH ConnectionSSH `json:"ssh,omitempty" yaml:"ssh,omitempty"`
Sentinel ConnectionSentinel `json:"sentinel,omitempty" yaml:"sentinel,omitempty"`
}

type Connection struct {
Expand Down Expand Up @@ -49,3 +50,10 @@ type ConnectionSSH struct {
PKFile string `json:"pkFile,omitempty" yaml:"pk_file,omitempty"`
Passphrase string `json:"passphrase,omitempty" yaml:"passphrase,omitempty"`
}

type ConnectionSentinel struct {
Enable bool `json:"enable" yaml:"enable"`
Master string `json:"master" yaml:"master"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
}
40 changes: 37 additions & 3 deletions frontend/src/components/dialogs/ConnectionDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const onClose = () => {
transform-origin="center">
<n-spin :show="closingConnection">
<n-tabs v-model:value="tab" animated type="line">
<!-- General pane -->
<n-tab-pane :tab="$t('dialogue.connection.general')" display-directive="show" name="general">
<n-form
ref="generalFormRef"
Expand Down Expand Up @@ -239,7 +240,8 @@ const onClose = () => {
</n-form>
</n-tab-pane>
<n-tab-pane :tab="$t('dialogue.connection.advanced')" display-directive="show" name="advanced">
<!-- Advance pane -->
<n-tab-pane :tab="$t('dialogue.connection.advn.title')" display-directive="show" name="advanced">
<n-form
ref="advanceFormRef"
:model="generalForm"
Expand Down Expand Up @@ -327,14 +329,14 @@ const onClose = () => {
</n-form>
</n-tab-pane>
<n-tab-pane :tab="$t('dialogue.connection.ssh.tunnel')" display-directive="show" name="ssh">
<!-- SSH pane -->
<n-tab-pane :tab="$t('dialogue.connection.ssh.title')" display-directive="show" name="ssh">
<n-form-item label-placement="left">
<n-checkbox v-model:checked="generalForm.ssh.enable" size="medium">
{{ $t('dialogue.connection.ssh.enable') }}
</n-checkbox>
</n-form-item>
<n-form
ref="sshFormRef"
:model="generalForm.ssh"
:show-require-mark="false"
:disabled="!generalForm.ssh.enable"
Expand Down Expand Up @@ -388,6 +390,38 @@ const onClose = () => {
</n-form>
</n-tab-pane>
<!-- Sentinel pane -->
<n-tab-pane :tab="$t('dialogue.connection.sentinel.title')" display-directive="show" name="sentinel">
<n-form-item label-placement="left">
<n-checkbox v-model:checked="generalForm.sentinel.enable" size="medium">
{{ $t('dialogue.connection.sentinel.enable') }}
</n-checkbox>
</n-form-item>
<n-form
:model="generalForm.sentinel"
:show-require-mark="false"
:disabled="!generalForm.sentinel.enable"
label-placement="top">
<n-form-item :label="$t('dialogue.connection.sentinel.master')">
<n-input
v-model:value="generalForm.sentinel.master"
:placeholder="$t('dialogue.connection.sentinel.master')" />
</n-form-item>
<n-form-item :label="$t('dialogue.connection.sentinel.password')">
<n-input
v-model:value="generalForm.sentinel.password"
:placeholder="$t('dialogue.connection.sentinel.pwd_tip')"
show-password-on="click"
type="password" />
</n-form-item>
<n-form-item :label="$t('dialogue.connection.sentinel.username')">
<n-input
v-model:value="generalForm.sentinel.username"
:placeholder="$t('dialogue.connection.sentinel.usr_tip')" />
</n-form-item>
</n-form>
</n-tab-pane>
<!-- TODO: SSL tab pane -->
<!-- TODO: Sentinel tab pane -->
<!-- TODO: Cluster tab pane -->
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
"new_title": "New Connection",
"edit_title": "Edit Connection",
"general": "General",
"advanced": "Advanced",
"no_group": "No Group",
"group": "Group",
"conn_name": "Name",
Expand All @@ -129,6 +128,7 @@
"test_succ": "Successful connection to redis-server",
"test_fail": "Fail Connection",
"advn": {
"title": "Advanced",
"filter": "Default Key Filter Pattern",
"filter_tip": "Pattern which defines loaded keys from redis server",
"separator": "Key Separator",
Expand All @@ -146,8 +146,8 @@
"mark_color": "Mark Color"
},
"ssh": {
"title": "SSH Tunnel",
"enable": "Enable SSH Tuntel",
"tunnel": "SSH Tunnel",
"login_type": "Login Type",
"pkfile": "Private Key File",
"passphrase": "Passphrase",
Expand All @@ -157,6 +157,15 @@
"pkfile_tip": "SSH Private Key File Path",
"passphrase_tip": "(Optional) Passphrase for Private Key",
"pkfile_selection_title": "Please Select Private Key File"
},
"sentinel": {
"title": "Sentinel",
"enable": "Serve as Sentinel Node",
"master": "Name of Master Node",
"password": "Password for Master Node",
"username": "Username for Master Node",
"pwd_tip": "(Optional) username for master node",
"usr_tip": "(Optional) authentication password for master node (Redis > 6.0)"
}
},
"group": {
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/langs/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
"new_title": "新建连接",
"edit_title": "编辑连接",
"general": "常规配置",
"advanced": "高级配置",
"no_group": "无分组",
"group": "分组",
"conn_name": "连接名",
Expand All @@ -129,6 +128,7 @@
"test_succ": "成功连接到Redis服务器",
"test_fail": "连接失败",
"advn": {
"title": "高级配置",
"filter": "默认键过滤表达式",
"filter_tip": "需要加载的键名表达式",
"separator": "键分隔符",
Expand All @@ -147,7 +147,7 @@
},
"ssh": {
"enable": "启用SSH隧道",
"tunnel": "SSH隧道",
"title": "SSH隧道",
"login_type": "登录类型",
"pkfile": "私钥文件",
"passphrase": "私钥密码",
Expand All @@ -157,6 +157,15 @@
"pkfile_tip": "SSH私钥文件路径",
"passphrase_tip": "(可选)SSH私钥密码",
"pkfile_selection_title": "请选择私钥文件"
},
"sentinel": {
"title": "哨兵模式",
"enable": "当前为哨兵节点",
"master": "主实例名",
"password": "主节点密码",
"username": "主节点用户名",
"pwd_tip": "(可选)主节点服务授权用户名",
"usr_tip": "(可选)主节点服务授权密码 (Redis > 6.0)"
}
},
"group": {
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/stores/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ const useConnectionStore = defineStore('connections', {
pkFile: '',
passphrase: '',
},
sentinel: {
enable: false,
master: 'mymaster',
username: '',
password: '',
},
}
},

Expand Down

0 comments on commit ee68d69

Please sign in to comment.