2017-05-17 05:28:07 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
2020-10-18 17:32:34 +03:00
|
|
|
"regexp"
|
2017-09-22 01:16:09 +03:00
|
|
|
"strings"
|
2022-11-15 00:43:34 +03:00
|
|
|
"syscall"
|
2022-12-02 23:20:20 +03:00
|
|
|
"time"
|
2017-05-17 05:28:07 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jessevdk/go-flags"
|
2022-12-01 21:35:02 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-17 05:28:07 +03:00
|
|
|
|
|
|
|
"github.com/sosedoff/pgweb/pkg/api"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/bookmarks"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/client"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/connection"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/shared"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/util"
|
|
|
|
)
|
|
|
|
|
2019-09-29 20:16:42 +03:00
|
|
|
var (
|
2022-12-02 22:36:31 +03:00
|
|
|
logger *logrus.Logger
|
2019-09-29 20:16:42 +03:00
|
|
|
options command.Options
|
|
|
|
|
|
|
|
readonlyWarning = `
|
|
|
|
------------------------------------------------------
|
|
|
|
SECURITY WARNING: You are running pgweb in read-only mode.
|
|
|
|
This mode is designed for environments where users could potentially delete / change data.
|
|
|
|
For proper read-only access please follow postgresql role management documentation.
|
|
|
|
------------------------------------------------------`
|
2020-10-18 17:32:34 +03:00
|
|
|
|
|
|
|
regexErrConnectionRefused = regexp.MustCompile(`(connection|actively) refused`)
|
|
|
|
regexErrAuthFailed = regexp.MustCompile(`authentication failed`)
|
2019-09-29 20:16:42 +03:00
|
|
|
)
|
2017-05-17 05:28:07 +03:00
|
|
|
|
2022-12-02 22:36:31 +03:00
|
|
|
func init() {
|
|
|
|
logger = logrus.New()
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
func exitWithMessage(message string) {
|
|
|
|
fmt.Println("Error:", message)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initClientUsingBookmark(bookmarkPath, bookmarkName string) (*client.Client, error) {
|
|
|
|
bookmark, err := bookmarks.GetBookmark(bookmarkPath, bookmarkName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := bookmark.ConvertToOptions()
|
|
|
|
var connStr string
|
|
|
|
|
2019-11-02 21:00:23 +03:00
|
|
|
if opt.URL != "" { // if the bookmark has url set, use it
|
|
|
|
connStr = opt.URL
|
2017-05-17 05:28:07 +03:00
|
|
|
} else {
|
2018-09-14 06:44:11 +03:00
|
|
|
connStr, err = connection.BuildStringFromOptions(opt)
|
2017-05-17 05:28:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error building connection string: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ssh *shared.SSHInfo
|
|
|
|
if !bookmark.SSHInfoIsEmpty() {
|
2019-11-02 21:00:23 +03:00
|
|
|
ssh = bookmark.SSH
|
2017-05-17 05:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewFromUrl(connStr, ssh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initClient() {
|
|
|
|
if connection.IsBlank(command.Opts) && options.Bookmark == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var cl *client.Client
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if options.Bookmark != "" {
|
|
|
|
cl, err = initClientUsingBookmark(bookmarks.Path(options.BookmarksDir), options.Bookmark)
|
|
|
|
} else {
|
|
|
|
cl, err = client.New()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
exitWithMessage(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if command.Opts.Debug {
|
|
|
|
fmt.Println("Server connection string:", cl.ConnectionString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Connecting to server...")
|
2019-01-25 19:29:24 +03:00
|
|
|
if err := cl.Test(); err != nil {
|
2018-11-28 02:23:55 +03:00
|
|
|
msg := err.Error()
|
|
|
|
|
|
|
|
// Check if we're trying to connect to the default database.
|
2019-11-02 21:00:23 +03:00
|
|
|
if command.Opts.DbName == "" && command.Opts.URL == "" {
|
2018-12-19 03:11:56 +03:00
|
|
|
// If database does not exist, allow user to connect from the UI.
|
2018-11-28 02:23:55 +03:00
|
|
|
if strings.Contains(msg, "database") && strings.Contains(msg, "does not exist") {
|
|
|
|
fmt.Println("Error:", msg)
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 23:54:12 +03:00
|
|
|
|
2018-12-19 03:11:56 +03:00
|
|
|
// Do not bail if local server is not running.
|
2020-10-18 17:32:34 +03:00
|
|
|
if regexErrConnectionRefused.MatchString(msg) {
|
2018-12-19 03:11:56 +03:00
|
|
|
fmt.Println("Error:", msg)
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 23:54:12 +03:00
|
|
|
|
|
|
|
// Do not bail if local auth is invalid
|
2020-10-18 17:32:34 +03:00
|
|
|
if regexErrAuthFailed.MatchString(msg) {
|
2020-04-23 23:54:12 +03:00
|
|
|
fmt.Println("Error:", msg)
|
|
|
|
return
|
|
|
|
}
|
2018-11-28 02:23:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exitWithMessage(msg)
|
2017-05-17 05:28:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:42:44 +03:00
|
|
|
if !command.Opts.Sessions {
|
2019-01-17 07:54:23 +03:00
|
|
|
fmt.Printf("Connected to %s\n", cl.ServerVersion())
|
2017-09-14 07:42:44 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
fmt.Println("Checking database objects...")
|
|
|
|
_, err = cl.Objects()
|
|
|
|
if err != nil {
|
|
|
|
exitWithMessage(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
api.DbClient = cl
|
|
|
|
}
|
|
|
|
|
|
|
|
func initOptions() {
|
2018-02-22 23:20:18 +03:00
|
|
|
opts, err := command.ParseOptions(os.Args)
|
2017-05-17 05:28:07 +03:00
|
|
|
if err != nil {
|
2022-11-23 01:09:01 +03:00
|
|
|
switch errVal := err.(type) {
|
2017-05-17 05:28:07 +03:00
|
|
|
case *flags.Error:
|
2022-11-23 01:09:01 +03:00
|
|
|
if errVal.Type == flags.ErrHelp {
|
|
|
|
fmt.Println("Available environment variables:")
|
|
|
|
fmt.Println(command.AvailableEnvVars())
|
|
|
|
}
|
2017-05-17 05:28:07 +03:00
|
|
|
// no need to print error, flags package already does that
|
|
|
|
default:
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-02-22 23:20:18 +03:00
|
|
|
command.Opts = opts
|
|
|
|
options = opts
|
2017-05-17 05:28:07 +03:00
|
|
|
|
|
|
|
if options.Version {
|
|
|
|
printVersion()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2022-12-06 21:09:21 +03:00
|
|
|
if err := configureLogger(opts); err != nil {
|
|
|
|
exitWithMessage(err.Error())
|
|
|
|
return
|
2022-12-02 22:36:31 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
if options.ReadOnly {
|
2019-09-29 20:16:42 +03:00
|
|
|
fmt.Println(readonlyWarning)
|
2017-05-17 05:28:07 +03:00
|
|
|
}
|
|
|
|
|
2021-12-29 20:03:50 +03:00
|
|
|
if options.BinaryCodec != "" {
|
|
|
|
if err := client.SetBinaryCodec(options.BinaryCodec); err != nil {
|
|
|
|
exitWithMessage(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
printVersion()
|
|
|
|
}
|
|
|
|
|
2022-12-06 21:09:21 +03:00
|
|
|
func configureLogger(opts command.Options) error {
|
|
|
|
if options.Debug {
|
|
|
|
logger.SetLevel(logrus.DebugLevel)
|
|
|
|
} else {
|
|
|
|
lvl, err := logrus.ParseLevel(opts.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.SetLevel(lvl)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch options.LogFormat {
|
|
|
|
case "text":
|
|
|
|
logger.SetFormatter(&logrus.TextFormatter{})
|
|
|
|
case "json":
|
|
|
|
logger.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid logger format: %v", options.LogFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
func printVersion() {
|
2022-12-06 20:27:47 +03:00
|
|
|
fmt.Println(command.VersionString())
|
2017-05-17 05:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func startServer() {
|
2022-12-01 21:49:24 +03:00
|
|
|
router := gin.New()
|
|
|
|
router.Use(api.RequestLogger(logger))
|
2022-12-01 21:35:02 +03:00
|
|
|
router.Use(gin.Recovery())
|
2017-05-17 05:28:07 +03:00
|
|
|
|
|
|
|
// Enable HTTP basic authentication only if both user and password are set
|
|
|
|
if options.AuthUser != "" && options.AuthPass != "" {
|
|
|
|
auth := map[string]string{options.AuthUser: options.AuthPass}
|
|
|
|
router.Use(gin.BasicAuth(auth))
|
|
|
|
}
|
|
|
|
|
2022-12-02 20:45:23 +03:00
|
|
|
api.SetLogger(logger)
|
2017-05-17 05:28:07 +03:00
|
|
|
api.SetupRoutes(router)
|
|
|
|
|
|
|
|
fmt.Println("Starting server...")
|
|
|
|
go func() {
|
2019-11-02 21:00:23 +03:00
|
|
|
err := router.Run(fmt.Sprintf("%v:%v", options.HTTPHost, options.HTTPPort))
|
2017-05-17 05:28:07 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cant start server:", err)
|
2017-09-22 01:16:09 +03:00
|
|
|
if strings.Contains(err.Error(), "address already in use") {
|
|
|
|
openPage()
|
|
|
|
}
|
2017-05-17 05:28:07 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSignals() {
|
|
|
|
c := make(chan os.Signal, 1)
|
2022-11-15 00:43:34 +03:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2017-05-17 05:28:07 +03:00
|
|
|
<-c
|
|
|
|
}
|
|
|
|
|
|
|
|
func openPage() {
|
2019-11-02 21:00:23 +03:00
|
|
|
url := fmt.Sprintf("http://%v:%v/%s", options.HTTPHost, options.HTTPPort, options.Prefix)
|
2017-05-17 05:28:07 +03:00
|
|
|
fmt.Println("To view database open", url, "in browser")
|
|
|
|
|
|
|
|
if options.SkipOpen {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := exec.Command("which", "open").Output()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:10:50 +03:00
|
|
|
_, err = exec.Command("open", url).Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to auto-open pgweb URL:", err)
|
|
|
|
}
|
2017-05-17 05:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Run() {
|
|
|
|
initOptions()
|
|
|
|
initClient()
|
|
|
|
|
|
|
|
if api.DbClient != nil {
|
|
|
|
defer api.DbClient.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !options.Debug {
|
|
|
|
gin.SetMode("release")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print memory usage every 30 seconds with debug flag
|
|
|
|
if options.Debug {
|
|
|
|
util.StartProfiler()
|
|
|
|
}
|
|
|
|
|
2017-09-23 06:44:32 +03:00
|
|
|
// Start session cleanup worker
|
2022-12-02 22:36:31 +03:00
|
|
|
if options.Sessions {
|
|
|
|
api.DbSessions = api.NewSessionManager(logger)
|
|
|
|
|
|
|
|
if !command.Opts.DisableConnectionIdleTimeout {
|
2022-12-02 23:20:20 +03:00
|
|
|
api.DbSessions.SetIdleTimeout(time.Minute * time.Duration(command.Opts.ConnectionIdleTimeout))
|
2022-12-02 22:36:31 +03:00
|
|
|
go api.DbSessions.RunPeriodicCleanup()
|
|
|
|
}
|
2017-09-23 06:44:32 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 05:28:07 +03:00
|
|
|
startServer()
|
|
|
|
openPage()
|
|
|
|
handleSignals()
|
|
|
|
}
|