diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index e20f267..0395b31 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -15,6 +15,7 @@ import ( "net" "os" "path" + "strconv" "strings" "sync" "time" @@ -512,3 +513,36 @@ func (c *connectionService) ImportConnections() (resp types.JSResp) { resp.Success = true return } + +// ParseConnectURL parse connection url string +func (c *connectionService) ParseConnectURL(url string) (resp types.JSResp) { + urlOpt, err := redis.ParseURL(url) + if err != nil { + resp.Msg = err.Error() + return + } + + addrPart := strings.Split(urlOpt.Addr, ":") + addr := addrPart[0] + port := 6379 + if len(addrPart) > 1 { + port, _ = strconv.Atoi(addrPart[1]) + } + resp.Success = true + resp.Data = struct { + Addr string `json:"addr"` + Port int `json:"port"` + Username string `json:"username"` + Password string `json:"password"` + ConnTimeout int64 `json:"connTimeout"` + ExecTimeout int64 `json:"execTimeout"` + }{ + Addr: addr, + Port: port, + Username: urlOpt.Username, + Password: urlOpt.Password, + ConnTimeout: int64(urlOpt.DialTimeout.Seconds()), + ExecTimeout: int64(urlOpt.ReadTimeout.Seconds()), + } + return +} diff --git a/frontend/src/components/dialogs/ConnectionDialog.vue b/frontend/src/components/dialogs/ConnectionDialog.vue index f1f9406..67d3a89 100644 --- a/frontend/src/components/dialogs/ConnectionDialog.vue +++ b/frontend/src/components/dialogs/ConnectionDialog.vue @@ -267,6 +267,29 @@ const onTestConnection = async () => { const onClose = () => { dialogStore.closeConnDialog() } + +const pasteFromClipboard = async () => { + // url example: + // redis://user:password@localhost:6789/3?dial_timeout=3&db=1&read_timeout=6s&max_retries=2 + let opt = {} + try { + opt = await connectionStore.parseUrlFromClipboard() + } catch (e) { + $message.error(i18n.t('dialogue.connection.parse_fail', { reason: e.message })) + return + } + generalForm.value.name = generalForm.value.addr = opt.addr + generalForm.value.port = opt.port + generalForm.value.username = opt.username + generalForm.value.password = opt.password + if (opt.connTimeout > 0) { + generalForm.value.connTimeout = opt.connTimeout + } + if (opt.execTimeout > 0) { + generalForm.value.execTimeout = opt.execTimeout + } + $message.success(i18n.t('dialogue.connection.parse_pass', { url: opt.url })) +}