pgweb/main.go

195 lines
4.0 KiB
Go
Raw Normal View History

2014-10-09 06:26:57 +04:00
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
2014-11-11 08:10:05 +03:00
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
2015-04-30 19:47:07 +03:00
"github.com/sosedoff/pgweb/pkg/api"
"github.com/sosedoff/pgweb/pkg/bookmarks"
2015-04-30 19:47:07 +03:00
"github.com/sosedoff/pgweb/pkg/client"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/connection"
"github.com/sosedoff/pgweb/pkg/shared"
2015-04-30 19:47:07 +03:00
"github.com/sosedoff/pgweb/pkg/util"
2014-10-09 06:26:57 +04:00
)
2015-04-30 19:47:07 +03:00
var options command.Options
2014-10-10 09:03:03 +04: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, err := bookmark.ConvertToOptions()
if err != nil {
return nil, err
}
var connStr string
if opt.Url != "" { // if the bookmark has url set, use it
connStr = opt.Url
} else {
connStr, err = connection.BuildString(opt)
if err != nil {
return nil, fmt.Errorf("error building connection string: %v", err)
}
}
var ssh *shared.SSHInfo
if !bookmark.SSHInfoIsEmpty() {
ssh = &bookmark.Ssh
}
return client.NewFromUrl(connStr, ssh)
}
2014-10-09 06:26:57 +04:00
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.Bookmark)
if err != nil {
exitWithMessage(err.Error())
}
} else {
cl, err = client.New()
if err != nil {
exitWithMessage(err.Error())
}
2014-10-09 06:26:57 +04:00
}
2015-04-30 19:47:07 +03:00
if command.Opts.Debug {
fmt.Println("Server connection string:", cl.ConnectionString)
}
2014-10-12 07:38:32 +04:00
fmt.Println("Connecting to server...")
2015-04-30 19:47:07 +03:00
err = cl.Test()
if err != nil {
exitWithMessage(err.Error())
}
2016-01-13 06:33:44 +03:00
fmt.Println("Checking database objects...")
_, err = cl.Objects()
if err != nil {
exitWithMessage(err.Error())
}
2015-04-30 19:47:07 +03:00
api.DbClient = cl
2014-10-09 06:26:57 +04:00
}
func initOptions() {
2015-04-30 19:47:07 +03:00
err := command.ParseOptions()
2014-10-09 06:26:57 +04:00
if err != nil {
switch err.(type) {
case *flags.Error:
// no need to print error, flags package already does that
default:
fmt.Println(err.Error())
}
2014-10-11 02:20:14 +04:00
os.Exit(1)
2014-10-09 06:26:57 +04:00
}
2014-10-22 17:54:47 +04:00
2015-04-30 19:47:07 +03:00
options = command.Opts
2014-10-22 17:54:47 +04:00
if options.Version {
printVersion()
2014-10-27 23:49:43 +03:00
os.Exit(0)
2014-10-22 17:54:47 +04:00
}
2015-04-30 19:47:07 +03:00
2016-11-06 04:50:56 +03:00
if options.ReadOnly {
msg := `------------------------------------------------------
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.
------------------------------------------------------`
fmt.Println(msg)
}
printVersion()
}
func printVersion() {
str := fmt.Sprintf("Pgweb v%s", command.VERSION)
if command.GitCommit != "" {
str += fmt.Sprintf(" (git: %s)", command.GitCommit)
}
fmt.Println(str)
2014-10-09 06:26:57 +04:00
}
func startServer() {
2014-10-09 06:26:57 +04:00
router := gin.Default()
2014-10-10 04:05:51 +04:00
2014-10-30 03:45:12 +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))
}
2015-04-30 19:47:07 +03:00
api.SetupRoutes(router)
2014-10-10 04:05:51 +04:00
2014-10-14 03:40:17 +04:00
fmt.Println("Starting server...")
go func() {
err := router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort))
if err != nil {
fmt.Println("Cant start server:", err)
os.Exit(1)
}
}()
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}
func openPage() {
url := fmt.Sprintf("http://%v:%v/%s", options.HttpHost, options.HttpPort, options.Prefix)
2014-10-26 19:47:15 +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
}
2014-10-26 19:47:15 +03:00
exec.Command("open", url).Output()
}
func main() {
initOptions()
initClient()
2015-04-30 19:47:07 +03:00
if api.DbClient != nil {
defer api.DbClient.Close()
}
if !options.Debug {
gin.SetMode("release")
}
2015-04-30 19:47:07 +03:00
// Print memory usage every 30 seconds with debug flag
if options.Debug {
2015-04-30 19:47:07 +03:00
util.StartProfiler()
}
startServer()
openPage()
handleSignals()
2014-10-09 06:26:57 +04:00
}