Implement session locking with --lock-session option
This commit is contained in:
parent
97b612c1b3
commit
20da36416c
7
main.go
7
main.go
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@ -25,6 +26,7 @@ type Options struct {
|
||||
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
@ -475,6 +475,7 @@
|
||||
position: fixed;
|
||||
right: 8px;
|
||||
top: 10px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
#edit_connection, #close_connection {
|
||||
|
@ -1080,6 +1080,10 @@ $(document).ready(function() {
|
||||
|
||||
$("#current_database").text(resp.current_database);
|
||||
$("#main").show();
|
||||
|
||||
if (!resp.session_lock) {
|
||||
$(".connection-actions").show();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user