Make pgweb to use multiple sessions at once [wip]

This commit is contained in:
Dan Sosedoff 2016-01-08 19:10:11 -06:00
parent 73cb979721
commit ed94244741
5 changed files with 85 additions and 42 deletions

View File

@ -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

View File

@ -20,10 +20,27 @@ var extraMimeTypes = map[string]string{
".html": "text/html; charset-utf-8",
}
// Paths that dont require database connection
var allowedPaths = map[string]bool{
"/api/sessions": true,
"/api/info": true,
"/api/connect": true,
"/api/bookmarks": true,
"/api/history": true,
}
type Error struct {
Message string `json:"error"`
}
func getSessionId(c *gin.Context) string {
id := c.Request.Header.Get("x-session-id")
if id == "" {
id = c.Request.URL.Query().Get("_session_id")
}
return id
}
func getQueryParam(c *gin.Context, name string) string {
result := ""
q := c.Request.URL.Query()
@ -75,37 +92,28 @@ func NewError(err error) Error {
// Middleware function to check database connection status before running queries
func dbCheckMiddleware() gin.HandlerFunc {
allowedPaths := []string{
"/api/info",
"/api/connect",
"/api/bookmarks",
"/api/history",
}
return func(c *gin.Context) {
if DbClient != nil {
if allowedPaths[c.Request.URL.Path] == true {
c.Next()
return
}
currentPath := c.Request.URL.Path
allowed := false
for _, path := range allowedPaths {
if path == currentPath {
allowed = true
break
}
sessionId := getSessionId(c)
if sessionId == "" {
c.JSON(400, Error{"Session ID is required"})
c.Abort()
return
}
if allowed {
c.Next()
} else {
conn := DbSessions[sessionId]
if conn == nil {
c.JSON(400, Error{"Not connected"})
c.Abort()
return
}
return
c.Set("db", conn)
c.Next()
}
}
@ -113,7 +121,6 @@ func dbCheckMiddleware() gin.HandlerFunc {
func requestInspectMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
err := c.Request.ParseForm()
log.Println("Request params:", err, c.Request.Form)
}
}

View File

@ -22,6 +22,7 @@ func SetupRoutes(router *gin.Engine) {
SetupMiddlewares(api)
api.GET("/info", GetInfo)
api.GET("/sessions", GetSessions)
api.POST("/connect", Connect)
api.GET("/databases", GetDatabases)
api.GET("/connection", GetConnectionInfo)

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,22 @@ var filterOptions = {
"not_null": "IS NOT NULL"
};
function guid() {
function s4() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); }
return [s4(), s4(), "-", s4(), "-", s4(), "-", s4(), "-", s4(), s4(), s4()].join("");
}
function getSessionId() {
var id = localStorage.getItem("session_id");
if (!id) {
id = guid();
localStorage.setItem("session_id", id);
}
return id;
}
function setRowsLimit(num) {
localStorage.setItem("rows_limit", num);
}
@ -47,6 +63,9 @@ function apiCall(method, path, params, cb) {
method: method,
cache: false,
data: params,
headers: {
"x-session-id": getSessionId()
},
success: function(data) {
cb(data);
},