Compare commits

...

3 Commits

Author SHA1 Message Date
Lykin 74a6b9b0e1 perf: optimize conflict connection name when duplicating 2024-02-18 14:32:13 +08:00
Lykin 09264134ec perf: support unix-socket via url string #142 2024-02-18 14:27:21 +08:00
Lykin a2331675d7 feat: support connect by unix-socket #142 2024-02-18 14:17:06 +08:00
8 changed files with 89 additions and 23 deletions

View File

@ -122,7 +122,6 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O
}
option := &redis.Options{
Addr: fmt.Sprintf("%s:%d", config.Addr, config.Port),
Username: config.Username,
Password: config.Password,
DialTimeout: time.Duration(config.ConnTimeout) * time.Second,
@ -130,6 +129,21 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O
WriteTimeout: time.Duration(config.ExecTimeout) * time.Second,
TLSConfig: tlsConfig,
}
if config.Network == "unix" {
option.Network = "unix"
if len(config.Sock) <= 0 {
option.Addr = "/tmp/redis.sock"
} else {
option.Addr = config.Sock
}
} else {
option.Network = "tcp"
if len(config.Addr) <= 0 {
option.Addr = fmt.Sprintf("127.0.0.1:%d", config.Port)
} else {
option.Addr = fmt.Sprintf("%s:%d", config.Addr, config.Port)
}
}
if config.LastDB > 0 {
option.DB = config.LastDB
}
@ -522,11 +536,19 @@ func (c *connectionService) ParseConnectURL(url string) (resp types.JSResp) {
return
}
addrPart := strings.Split(urlOpt.Addr, ":")
addr := addrPart[0]
port := 6379
if len(addrPart) > 1 {
port, _ = strconv.Atoi(addrPart[1])
var network, addr string
var port int
if urlOpt.Network == "unix" {
network = urlOpt.Network
addr = urlOpt.Addr
} else {
network = "tcp"
addrPart := strings.Split(urlOpt.Addr, ":")
addr = addrPart[0]
port = 6379
if len(addrPart) > 1 {
port, _ = strconv.Atoi(addrPart[1])
}
}
var sslServerName string
if urlOpt.TLSConfig != nil {
@ -534,6 +556,8 @@ func (c *connectionService) ParseConnectURL(url string) (resp types.JSResp) {
}
resp.Success = true
resp.Data = struct {
Network string `json:"network"`
Sock string `json:"sock"`
Addr string `json:"addr"`
Port int `json:"port"`
Username string `json:"username"`
@ -542,6 +566,7 @@ func (c *connectionService) ParseConnectURL(url string) (resp types.JSResp) {
ExecTimeout int64 `json:"execTimeout"`
SSLServerName string `json:"sslServerName,omitempty"`
}{
Network: network,
Addr: addr,
Port: port,
Username: urlOpt.Username,

View File

@ -27,6 +27,7 @@ func (c *ConnectionsStorage) defaultConnections() types.Connections {
func (c *ConnectionsStorage) defaultConnectionItem() types.ConnectionConfig {
return types.ConnectionConfig{
Name: "",
Network: "tcp",
Addr: "127.0.0.1",
Port: 6379,
Username: "",

View File

@ -6,6 +6,8 @@ type ConnectionConfig struct {
Name string `json:"name" yaml:"name"`
Group string `json:"group,omitempty" yaml:"-"`
LastDB int `json:"lastDB" yaml:"last_db"`
Network string `json:"network,omitempty" yaml:"network,omitempty"`
Sock string `json:"sock,omitempty" yaml:"sock,omitempty"`
Addr string `json:"addr,omitempty" yaml:"addr,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`

View File

@ -278,6 +278,7 @@ const pasteFromClipboard = async () => {
$message.error(i18n.t('dialogue.connection.parse_fail', { reason: e.message }))
return
}
generalForm.value.network = opt.network || 'tcp'
generalForm.value.name = generalForm.value.addr = opt.addr
generalForm.value.port = opt.port
generalForm.value.username = opt.username
@ -348,16 +349,33 @@ const pasteFromClipboard = async () => {
:render-label="({ label, value }) => (value === '' ? $t(label) : label)" />
</n-form-item-gi>
<n-form-item-gi :label="$t('dialogue.connection.addr')" :span="24" path="addr" required>
<n-input
v-model:value="generalForm.addr"
:placeholder="$t('dialogue.connection.addr_tip')" />
<n-text style="width: 40px; text-align: center">:</n-text>
<n-input-number
v-model:value="generalForm.port"
:max="65535"
:min="1"
:show-button="false"
style="width: 200px" />
<n-input-group>
<n-select
v-model:value="generalForm.network"
:options="[
{ value: 'tcp', label: 'TCP' },
{ value: 'unix', label: 'UNIX' },
]"
style="max-width: 100px" />
<template v-if="generalForm.network === 'unix'">
<n-input
v-model:value="generalForm.sock"
:placeholder="$t('dialogue.connection.sock_tip')" />
</template>
<template v-else>
<n-input
v-model:value="generalForm.addr"
:placeholder="$t('dialogue.connection.addr_tip')" />
<n-text style="width: 40px; text-align: center">:</n-text>
<n-input-number
v-model:value="generalForm.port"
:max="65535"
:min="1"
:show-button="false"
placeholder="6379"
style="width: 200px" />
</template>
</n-input-group>
</n-form-item-gi>
<n-form-item-gi :label="$t('dialogue.connection.pwd')" :span="12" path="password">
<n-input

View File

@ -193,6 +193,7 @@
"pwd": "Password",
"name_tip": "Connection name",
"addr_tip": "Redis server host",
"sock_tip": "Redis server unix socket file",
"usr_tip": "(Optional) Authentication username",
"pwd_tip": "(Optional) Authentication password (Redis > 6.0)",
"test": "Test Connection",

View File

@ -193,6 +193,7 @@
"pwd": "密码",
"name_tip": "连接名",
"addr_tip": "Redis服务地址",
"sock_tip": "Redis套接字文件",
"usr_tip": "(可选)Redis服务授权用户名",
"pwd_tip": "(可选)Redis服务授权密码 (Redis > 6.0)",
"test": "测试连接",

View File

@ -153,6 +153,8 @@ const useConnectionStore = defineStore('connections', {
return {
group: '',
name: name || '',
network: 'tcp',
sock: '/tmp/redis.sock',
addr: '127.0.0.1',
port: 6379,
username: '',

View File

@ -124,13 +124,29 @@ const useDialogStore = defineStore('dialog', {
async openDuplicateDialog(name) {
const connStore = useConnectionStore()
const profile = await connStore.getConnectionProfile(name)
if (profile != null) {
profile.name += '2'
this.connParam = profile
} else {
this.connParam = connStore.newDefaultConnection(name)
}
this.connParam = {}
let profile
let suffix = 1
do {
let profileName = name
if (suffix > 1) {
profileName += suffix
}
profile = await connStore.getConnectionProfile(profileName)
if (profile != null) {
suffix += 1
if (profileName === name) {
this.connParam = profile
}
} else {
this.connParam = connStore.mergeConnectionProfile(
connStore.newDefaultConnection(profileName),
this.connParam,
)
this.connParam.name = profileName
break
}
} while (true)
this.connType = ConnDialogType.NEW
this.connDialogVisible = true
},