示例#1
0
文件: api.go 项目: jmptrader/pgweb
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 == "" {
		name = c.Request.FormValue("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
		}
	}

	conn.Close()

	c.JSON(200, info.Format()[0])
}
示例#2
0
文件: api.go 项目: jmptrader/pgweb
func Connect(c *gin.Context) {
	if command.Opts.LockSession {
		c.JSON(400, Error{"Session is locked"})
		return
	}

	var sshInfo *shared.SSHInfo
	url := c.Request.FormValue("url")

	if url == "" {
		c.JSON(400, Error{"Url parameter is required"})
		return
	}

	opts := command.Options{Url: url}
	url, err := connection.FormatUrl(opts)

	if err != nil {
		c.JSON(400, Error{err.Error()})
		return
	}

	if c.Request.FormValue("ssh") != "" {
		sshInfo = parseSshInfo(c)
	}

	cl, err := client.NewFromUrl(url, sshInfo)
	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])
}
示例#3
0
文件: api.go 项目: mavencode01/pgweb
func Connect(c *gin.Context) {
	url := c.Request.FormValue("url")

	if url == "" {
		c.JSON(400, Error{"Url parameter is required"})
		return
	}

	opts := command.Options{Url: url}
	url, err := connection.FormatUrl(opts)

	if err != nil {
		c.JSON(400, Error{err.Error()})
		return
	}

	cl, err := client.NewFromUrl(url)
	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])
}