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"
|
"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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -8,23 +9,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Version bool `short:"v" long:"version" description:"Print version"`
|
Version bool `short:"v" long:"version" description:"Print version"`
|
||||||
Debug bool `short:"d" long:"debug" description:"Enable debugging mode" default:"false"`
|
Debug bool `short:"d" long:"debug" description:"Enable debugging mode" default:"false"`
|
||||||
Url string `long:"url" description:"Database connection string"`
|
Url string `long:"url" description:"Database connection string"`
|
||||||
Host string `long:"host" description:"Server hostname or IP"`
|
Host string `long:"host" description:"Server hostname or IP"`
|
||||||
Port int `long:"port" description:"Server port" default:"5432"`
|
Port int `long:"port" description:"Server port" default:"5432"`
|
||||||
User string `long:"user" description:"Database user"`
|
User string `long:"user" description:"Database user"`
|
||||||
Pass string `long:"pass" description:"Password for user"`
|
Pass string `long:"pass" description:"Password for user"`
|
||||||
DbName string `long:"db" description:"Database name"`
|
DbName string `long:"db" description:"Database name"`
|
||||||
Ssl string `long:"ssl" description:"SSL option"`
|
Ssl string `long:"ssl" description:"SSL option"`
|
||||||
HttpHost string `long:"bind" description:"HTTP server host" default:"localhost"`
|
HttpHost string `long:"bind" description:"HTTP server host" default:"localhost"`
|
||||||
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8081"`
|
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8081"`
|
||||||
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
|
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
|
||||||
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
|
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
|
||||||
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
|
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
|
||||||
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
@ -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 {
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user