Implement session locking with --lock-session option

This commit is contained in:
Dan Sosedoff 2016-11-05 21:23:37 -05:00
parent 97b612c1b3
commit 20da36416c
6 changed files with 59 additions and 22 deletions

View File

@ -7,6 +7,7 @@ import (
"os/signal" "os/signal"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/sosedoff/pgweb/pkg/api" "github.com/sosedoff/pgweb/pkg/api"
"github.com/sosedoff/pgweb/pkg/client" "github.com/sosedoff/pgweb/pkg/client"
@ -54,6 +55,12 @@ func initClient() {
func initOptions() { func initOptions() {
err := command.ParseOptions() err := command.ParseOptions()
if err != nil { if err != nil {
switch err.(type) {
case *flags.Error:
// no need to print error, flags package already does that
default:
fmt.Println(err.Error())
}
os.Exit(1) os.Exit(1)
} }

View File

@ -68,6 +68,11 @@ func GetSessions(c *gin.Context) {
} }
func Connect(c *gin.Context) { func Connect(c *gin.Context) {
if command.Opts.LockSession {
c.JSON(400, Error{"Session is locked"})
return
}
var sshInfo *shared.SSHInfo var sshInfo *shared.SSHInfo
url := c.Request.FormValue("url") url := c.Request.FormValue("url")
@ -114,6 +119,11 @@ func Connect(c *gin.Context) {
} }
func Disconnect(c *gin.Context) { func Disconnect(c *gin.Context) {
if command.Opts.LockSession {
c.JSON(400, Error{"Session is locked"})
return
}
conn := DB(c) conn := DB(c)
if conn == nil { if conn == nil {
@ -261,7 +271,10 @@ func GetConnectionInfo(c *gin.Context) {
return return
} }
c.JSON(200, res.Format()[0]) info := res.Format()[0]
info["session_lock"] = command.Opts.LockSession
c.JSON(200, info)
} }
func GetActivity(c *gin.Context) { func GetActivity(c *gin.Context) {

View File

@ -1,6 +1,7 @@
package command package command
import ( import (
"fmt"
"os" "os"
"strings" "strings"
@ -25,6 +26,7 @@ type Options struct {
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"` Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
Prefix string `long:"prefix" description:"Add a url prefix"` Prefix string `long:"prefix" description:"Add a url prefix"`
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"` ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
LockSession bool `long:"lock-session" description:"Lock session to a single database connection" default:"false"`
} }
var Opts Options var Opts Options
@ -43,6 +45,16 @@ func ParseOptions() error {
Opts.Sessions = true Opts.Sessions = true
} }
if os.Getenv("LOCK_SESSION") != "" {
Opts.LockSession = true
Opts.Sessions = false
}
// When session is locked, connection UI is not displayed.
if Opts.LockSession && Opts.Url == "" {
return fmt.Errorf("Please provide connection url")
}
if Opts.Prefix != "" && !strings.Contains(Opts.Prefix, "/") { if Opts.Prefix != "" && !strings.Contains(Opts.Prefix, "/") {
Opts.Prefix = Opts.Prefix + "/" Opts.Prefix = Opts.Prefix + "/"
} }

File diff suppressed because one or more lines are too long

View File

@ -475,6 +475,7 @@
position: fixed; position: fixed;
right: 8px; right: 8px;
top: 10px; top: 10px;
display: none;
} }
#edit_connection, #close_connection { #edit_connection, #close_connection {

View File

@ -1080,6 +1080,10 @@ $(document).ready(function() {
$("#current_database").text(resp.current_database); $("#current_database").text(resp.current_database);
$("#main").show(); $("#main").show();
if (!resp.session_lock) {
$(".connection-actions").show();
}
} }
}); });
}); });