Make pgweb to use multiple sessions at once [wip]
This commit is contained in:
@@ -15,6 +15,11 @@ import (
|
||||
)
|
||||
|
||||
var DbClient *client.Client
|
||||
var DbSessions = map[string]*client.Client{}
|
||||
|
||||
func DB(c *gin.Context) *client.Client {
|
||||
return DbSessions[getSessionId(c)]
|
||||
}
|
||||
|
||||
func GetHome(c *gin.Context) {
|
||||
serveStaticAsset("/index.html", c)
|
||||
@@ -24,6 +29,10 @@ func GetAsset(c *gin.Context) {
|
||||
serveStaticAsset(c.Params.ByName("path"), c)
|
||||
}
|
||||
|
||||
func GetSessions(c *gin.Context) {
|
||||
c.JSON(200, DbSessions)
|
||||
}
|
||||
|
||||
func Connect(c *gin.Context) {
|
||||
url := c.Request.FormValue("url")
|
||||
|
||||
@@ -32,6 +41,12 @@ func Connect(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionId := getSessionId(c)
|
||||
if sessionId == "" {
|
||||
c.JSON(400, Error{"Session ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
opts := command.Options{Url: url}
|
||||
url, err := connection.FormatUrl(opts)
|
||||
|
||||
@@ -55,18 +70,19 @@ func Connect(c *gin.Context) {
|
||||
info, err := cl.Info()
|
||||
|
||||
if err == nil {
|
||||
if DbClient != nil {
|
||||
DbClient.Close()
|
||||
db := DbSessions[sessionId]
|
||||
if db != nil {
|
||||
db.Close()
|
||||
}
|
||||
|
||||
DbClient = cl
|
||||
DbSessions[sessionId] = cl
|
||||
}
|
||||
|
||||
c.JSON(200, info.Format()[0])
|
||||
}
|
||||
|
||||
func GetDatabases(c *gin.Context) {
|
||||
names, err := DbClient.Databases()
|
||||
names, err := DB(c).Databases()
|
||||
serveResult(names, err, c)
|
||||
}
|
||||
|
||||
@@ -93,17 +109,17 @@ func ExplainQuery(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetSchemas(c *gin.Context) {
|
||||
names, err := DbClient.Schemas()
|
||||
names, err := DB(c).Schemas()
|
||||
serveResult(names, err, c)
|
||||
}
|
||||
|
||||
func GetTables(c *gin.Context) {
|
||||
names, err := DbClient.Tables()
|
||||
names, err := DB(c).Tables()
|
||||
serveResult(names, err, c)
|
||||
}
|
||||
|
||||
func GetTable(c *gin.Context) {
|
||||
res, err := DbClient.Table(c.Params.ByName("table"))
|
||||
res, err := DB(c).Table(c.Params.ByName("table"))
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
@@ -128,13 +144,13 @@ func GetTableRows(c *gin.Context) {
|
||||
Where: c.Request.FormValue("where"),
|
||||
}
|
||||
|
||||
res, err := DbClient.TableRows(c.Params.ByName("table"), opts)
|
||||
res, err := DB(c).TableRows(c.Params.ByName("table"), opts)
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
}
|
||||
|
||||
countRes, err := DbClient.TableRowsCount(c.Params.ByName("table"), opts)
|
||||
countRes, err := DB(c).TableRowsCount(c.Params.ByName("table"), opts)
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
@@ -160,7 +176,7 @@ func GetTableRows(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetTableInfo(c *gin.Context) {
|
||||
res, err := DbClient.TableInfo(c.Params.ByName("table"))
|
||||
res, err := DB(c).TableInfo(c.Params.ByName("table"))
|
||||
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
@@ -171,11 +187,11 @@ func GetTableInfo(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetHistory(c *gin.Context) {
|
||||
c.JSON(200, DbClient.History)
|
||||
c.JSON(200, DB(c).History)
|
||||
}
|
||||
|
||||
func GetConnectionInfo(c *gin.Context) {
|
||||
res, err := DbClient.Info()
|
||||
res, err := DB(c).Info()
|
||||
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
@@ -186,22 +202,22 @@ func GetConnectionInfo(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetSequences(c *gin.Context) {
|
||||
res, err := DbClient.Sequences()
|
||||
res, err := DB(c).Sequences()
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
func GetActivity(c *gin.Context) {
|
||||
res, err := DbClient.Activity()
|
||||
res, err := DB(c).Activity()
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
func GetTableIndexes(c *gin.Context) {
|
||||
res, err := DbClient.TableIndexes(c.Params.ByName("table"))
|
||||
res, err := DB(c).TableIndexes(c.Params.ByName("table"))
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
func GetTableConstraints(c *gin.Context) {
|
||||
res, err := DbClient.TableConstraints(c.Params.ByName("table"))
|
||||
res, err := DB(c).TableConstraints(c.Params.ByName("table"))
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
@@ -211,7 +227,7 @@ func HandleQuery(query string, c *gin.Context) {
|
||||
query = string(rawQuery)
|
||||
}
|
||||
|
||||
result, err := DbClient.Query(query)
|
||||
result, err := DB(c).Query(query)
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user