Add endpoint to switch active database
This commit is contained in:
parent
07ee8448e6
commit
7d08017c7f
@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
neturl "net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -118,6 +120,57 @@ func Connect(c *gin.Context) {
|
|||||||
c.JSON(200, info.Format()[0])
|
c.JSON(200, info.Format()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SwitchDb(c *gin.Context) {
|
||||||
|
if command.Opts.LockSession {
|
||||||
|
c.JSON(400, Error{"Session is locked"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := c.Request.URL.Query().Get("db")
|
||||||
|
if name == "" {
|
||||||
|
c.JSON(400, Error{"Database name is not provided"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := DB(c)
|
||||||
|
if conn == nil {
|
||||||
|
c.JSON(400, Error{"Not connected"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
currentUrl, err := neturl.Parse(conn.ConnectionString)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, Error{"Unable to parse current connection string"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newStr := strings.Replace(conn.ConnectionString, currentUrl.Path, "/"+name, 1)
|
||||||
|
|
||||||
|
cl, err := client.NewFromUrl(newStr, nil)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, Error{err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cl.Test()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, Error{err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := cl.Info()
|
||||||
|
if err == nil {
|
||||||
|
err = setClient(c, cl)
|
||||||
|
if err != nil {
|
||||||
|
cl.Close()
|
||||||
|
c.JSON(400, Error{err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, info.Format()[0])
|
||||||
|
}
|
||||||
|
|
||||||
func Disconnect(c *gin.Context) {
|
func Disconnect(c *gin.Context) {
|
||||||
if command.Opts.LockSession {
|
if command.Opts.LockSession {
|
||||||
c.JSON(400, Error{"Session is locked"})
|
c.JSON(400, Error{"Session is locked"})
|
||||||
|
@ -30,6 +30,7 @@ func SetupRoutes(router *gin.Engine) {
|
|||||||
api.GET("/info", GetInfo)
|
api.GET("/info", GetInfo)
|
||||||
api.POST("/connect", Connect)
|
api.POST("/connect", Connect)
|
||||||
api.POST("/disconnect", Disconnect)
|
api.POST("/disconnect", Disconnect)
|
||||||
|
api.POST("/switchdb", SwitchDb)
|
||||||
api.GET("/databases", GetDatabases)
|
api.GET("/databases", GetDatabases)
|
||||||
api.GET("/connection", GetConnectionInfo)
|
api.GET("/connection", GetConnectionInfo)
|
||||||
api.GET("/activity", GetActivity)
|
api.GET("/activity", GetActivity)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user