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"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/sosedoff/pgweb/pkg/api"
"github.com/sosedoff/pgweb/pkg/client"
@ -54,6 +55,12 @@ func initClient() {
func initOptions() {
err := command.ParseOptions()
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)
}

View File

@ -68,6 +68,11 @@ func GetSessions(c *gin.Context) {
}
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")
@ -114,6 +119,11 @@ func Connect(c *gin.Context) {
}
func Disconnect(c *gin.Context) {
if command.Opts.LockSession {
c.JSON(400, Error{"Session is locked"})
return
}
conn := DB(c)
if conn == nil {
@ -261,7 +271,10 @@ func GetConnectionInfo(c *gin.Context) {
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) {

View File

@ -1,6 +1,7 @@
package command
import (
"fmt"
"os"
"strings"
@ -8,23 +9,24 @@ import (
)
type Options struct {
Version bool `short:"v" long:"version" description:"Print version"`
Debug bool `short:"d" long:"debug" description:"Enable debugging mode" default:"false"`
Url string `long:"url" description:"Database connection string"`
Host string `long:"host" description:"Server hostname or IP"`
Port int `long:"port" description:"Server port" default:"5432"`
User string `long:"user" description:"Database user"`
Pass string `long:"pass" description:"Password for user"`
DbName string `long:"db" description:"Database name"`
Ssl string `long:"ssl" description:"SSL option"`
HttpHost string `long:"bind" description:"HTTP server host" default:"localhost"`
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8081"`
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
Prefix string `long:"prefix" description:"Add a url prefix"`
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
Version bool `short:"v" long:"version" description:"Print version"`
Debug bool `short:"d" long:"debug" description:"Enable debugging mode" default:"false"`
Url string `long:"url" description:"Database connection string"`
Host string `long:"host" description:"Server hostname or IP"`
Port int `long:"port" description:"Server port" default:"5432"`
User string `long:"user" description:"Database user"`
Pass string `long:"pass" description:"Password for user"`
DbName string `long:"db" description:"Database name"`
Ssl string `long:"ssl" description:"SSL option"`
HttpHost string `long:"bind" description:"HTTP server host" default:"localhost"`
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8081"`
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
Prefix string `long:"prefix" description:"Add a url prefix"`
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
@ -43,6 +45,16 @@ func ParseOptions() error {
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, "/") {
Opts.Prefix = Opts.Prefix + "/"
}

File diff suppressed because one or more lines are too long

View File

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

View File

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